kristyc commited on
Commit
484693e
1 Parent(s): fc9190a

Wire up the app with the actual face detection model

Browse files
Files changed (4) hide show
  1. app.py +17 -14
  2. examples/01.jpg +2 -2
  3. examples/02.jpg +3 -0
  4. 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
- return {}, image
 
 
 
 
 
 
 
 
 
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
- with gr.Tabs():
39
- with gr.TabItem(label="Upload an image"):
40
- upload_image = gr.Image(label="Input image")
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
- raw_results = gr.JSON(label="Raw results")
51
  output_image = gr.Image(label="Output image")
52
 
53
- model_config_inputs = [model_selection, min_detection_confidence]
54
- outputs = [raw_results, output_image]
55
- upload_image_btn.click(fn=detect_faces, inputs=[upload_image, *model_config_inputs], outputs=outputs)
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

  • SHA256: c11527cf0e64df27f133eee6649a933058c7fe14210c9723a7a0d90e36bd08ce
  • Pointer size: 132 Bytes
  • Size of remote file: 1.78 MB

Git LFS Details

  • SHA256: f7ab532e28ab2519cb3d474b3ac484bcedef7a40d330b5b6caffe0d89d83dbc6
  • Pointer size: 130 Bytes
  • Size of remote file: 60.8 kB
examples/02.jpg ADDED

Git LFS Details

  • SHA256: 07979ab4b8bb3c42b58b589fa540585c5f6bf6643f09d6f24a5357d5ea82ff13
  • Pointer size: 130 Bytes
  • Size of remote file: 97.4 kB
examples/03.jpg ADDED

Git LFS Details

  • SHA256: 8f35b1fa1b80025779b6f773098d4f82a12650e8b85aadf8e1dea9a6e6f43fbc
  • Pointer size: 132 Bytes
  • Size of remote file: 1.13 MB