File size: 3,762 Bytes
a9640c3
 
 
49591f1
a9640c3
 
 
 
49591f1
 
 
 
a9640c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49591f1
 
a9640c3
49591f1
 
 
b03c53a
e7c8d5d
 
 
49591f1
 
 
 
 
 
 
 
 
 
 
 
 
 
a9640c3
49591f1
 
 
 
 
 
 
 
 
e7c8d5d
 
 
49591f1
 
 
 
 
 
a9640c3
 
 
 
 
49591f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from model_utils import detect_face
import gradio as gr
import numpy as np
import os

# Function to run the app


def set_image(image):
    return gr.Image.update(value=image[0][0])


def run_model(image: np.ndarray):
    return gr.Image.update(value=detect_face(image))


def interface() -> None:
    """
    Create and launch the graphical user interface face detection app.
    """

    # Create the blocks for the interface
    with gr.Blocks() as app:
        # Add a title and opening HTML element
        gr.HTML(
            """
            <div style="text-align: center; max-width: 650px; margin: 0 auto; padding-top: 7px;">
              <div
                style="
                  display: inline-flex;
                  align-items: center;
                  gap: 0.8rem;
                  font-size: 1.85rem;
                "
              >
                <h1 style="font-weight: 900; margin-bottom: 7px;">
                  Face Detection App 👤
                </h1>
              </div>
            </div>
        """
        )
        with gr.Group():
            with gr.Tabs():
                with gr.TabItem("Image input 🖼️"):
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                image_in = gr.Image(
                                    label="Image input", interactive=True)
                            with gr.Row():
                                detect_image_button = gr.Button(
                                    value="Detect face 👤")
                            with gr.Row():
                                paths = [["examples/" + example]
                                         for example in os.listdir("examples")]
                                example_images = gr.Dataset(components=([image_in]), label="Example images", samples=[[path]
                                                                                                                      for path in paths])
                        with gr.Column():
                            with gr.Row():
                                face_detected_image_out = gr.Image(
                                    label="Face detected", interactive=False)

                            example_images.click(fn=set_image, inputs=[
                                                 example_images], outputs=[image_in])
                            detect_image_button.click(fn=run_model, inputs=[
                                image_in], outputs=face_detected_image_out)

                with gr.TabItem("Webcam input 📷"):
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                webcam_image_in = gr.Webcam(
                                    label="Webcam input")
                            with gr.Row():
                                detect_button = gr.Button(
                                    value="Detect face 👤")
                            with gr.Row():
                                gr.Text(
                                    label="⚠️ Reminder ", value="Do not forget to click the camera button to freeze and get the webcam image 📷!", interactive=False)
                        with gr.Column():
                            with gr.Row():
                                face_detected_webcam_out = gr.Image(
                                    label="Face detected", interactive=False)
                            detect_button.click(fn=run_model, inputs=[
                                webcam_image_in], outputs=face_detected_webcam_out)

        app.launch()


if __name__ == '__main__':
    interface()  # Run the interface