Spaces:
Build error
Build error
Wire up the app with the actual face detection model
Browse files- app.py +17 -14
- examples/01.jpg +2 -2
- examples/02.jpg +3 -0
- examples/03.jpg +3 -0
app.py
CHANGED
@@ -13,7 +13,16 @@ mp_drawing = mp.solutions.drawing_utils
|
|
13 |
demo = gr.Blocks()
|
14 |
|
15 |
def detect_faces(image, model_selection, min_detection_confidence):
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
with demo:
|
19 |
gr.Markdown(
|
@@ -24,7 +33,6 @@ with demo:
|
|
24 |
|
25 |
with gr.Row():
|
26 |
with gr.Column():
|
27 |
-
|
28 |
gr.Markdown("## Step 1: Configure the model")
|
29 |
with gr.Group():
|
30 |
model_selection = gr.Radio(label="Model selection", value=0, choices=[0, 1])
|
@@ -35,23 +43,18 @@ with demo:
|
|
35 |
gr.Textbox(show_label=False, value="Minimum confidence value ([0.0, 1.0]) from the face detection model for the detection to be considered successful.")
|
36 |
|
37 |
gr.Markdown("## Step 2: Select an image")
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
gr.Examples(examples=[["examples/01.jpg"]], inputs=[upload_image])
|
42 |
-
upload_image_btn = gr.Button(value="Detect faces")
|
43 |
-
with gr.TabItem(label="Take a photo"):
|
44 |
-
webcam_image = gr.Image(label="Input image", source="webcam")
|
45 |
-
webcam_image_btn = gr.Button(value="Detect faces")
|
46 |
|
47 |
with gr.Column():
|
48 |
gr.Markdown("## Step 3: View the results")
|
49 |
with gr.Group():
|
50 |
-
|
51 |
output_image = gr.Image(label="Output image")
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
demo.launch()
|
|
|
13 |
demo = gr.Blocks()
|
14 |
|
15 |
def detect_faces(image, model_selection, min_detection_confidence):
|
16 |
+
with mp_face_detection.FaceDetection(model_selection=model_selection, min_detection_confidence=min_detection_confidence) as face_detection:
|
17 |
+
results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
18 |
+
if not results.detections:
|
19 |
+
logger.info('No faces detected')
|
20 |
+
return image, 0
|
21 |
+
logger.info(f"{len(results.detections)} faces detected")
|
22 |
+
annotated_image = image.copy()
|
23 |
+
for detection in results.detections:
|
24 |
+
mp_drawing.draw_detection(annotated_image, detection)
|
25 |
+
return annotated_image, len(results.detections)
|
26 |
|
27 |
with demo:
|
28 |
gr.Markdown(
|
|
|
33 |
|
34 |
with gr.Row():
|
35 |
with gr.Column():
|
|
|
36 |
gr.Markdown("## Step 1: Configure the model")
|
37 |
with gr.Group():
|
38 |
model_selection = gr.Radio(label="Model selection", value=0, choices=[0, 1])
|
|
|
43 |
gr.Textbox(show_label=False, value="Minimum confidence value ([0.0, 1.0]) from the face detection model for the detection to be considered successful.")
|
44 |
|
45 |
gr.Markdown("## Step 2: Select an image")
|
46 |
+
upload_image = gr.Image(label="Input image", type="numpy")
|
47 |
+
gr.Examples(examples=[["examples/01.jpg", 1], ["examples/02.jpg", 1], ["examples/03.jpg", 0]], inputs=[upload_image, model_selection])
|
48 |
+
upload_image_btn = gr.Button(value="Detect faces")
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
with gr.Column():
|
51 |
gr.Markdown("## Step 3: View the results")
|
52 |
with gr.Group():
|
53 |
+
num_faces_detected = gr.Number(label="Number of faces detected", value=0)
|
54 |
output_image = gr.Image(label="Output image")
|
55 |
|
56 |
+
model_config_inputs = [model_selection, min_detection_confidence]
|
57 |
+
outputs = [output_image, num_faces_detected]
|
58 |
+
upload_image_btn.click(fn=detect_faces, inputs=[upload_image, *model_config_inputs], outputs=outputs)
|
59 |
|
60 |
demo.launch()
|
examples/01.jpg
CHANGED
Git LFS Details
|
Git LFS Details
|
examples/02.jpg
ADDED
Git LFS Details
|
examples/03.jpg
ADDED
Git LFS Details
|