leandrodevai commited on
Commit
51d742f
·
verified ·
1 Parent(s): 88cf4ae

Sync from GitHub via hub-sync

Browse files
app.py DELETED
@@ -1,9 +0,0 @@
1
- import sys
2
- from pathlib import Path
3
-
4
- sys.path.insert(0, str(Path(__file__).parent / "src"))
5
-
6
- from faceverification.interfaces.gradio_app import FV_gr
7
-
8
- if __name__ == "__main__":
9
- FV_gr.launch()
 
 
 
 
 
 
 
 
 
 
docker/constraints.txt DELETED
@@ -1,2 +0,0 @@
1
- torch==2.11.0+cpu
2
- torchvision==0.26.0+cpu
 
 
 
src/faceverification/interfaces/gradio_app.py CHANGED
@@ -5,47 +5,160 @@ from faceverification.core.image_processor import FaceNotDetectedError
5
  from faceverification.services.face_verification import add_person, verify_person
6
 
7
 
8
- def add_person_ui(image: Image.Image, name: str) -> Image.Image:
9
  try:
10
- return add_person(image, name)
 
 
 
 
 
11
  except FaceNotDetectedError as exc:
12
  raise gr.Error(str(exc)) from exc
13
  except Exception as exc:
14
  raise gr.Error(str(exc)) from exc
15
 
16
 
17
- def verify_person_ui(image: Image.Image) -> tuple[str, Image.Image]:
18
  try:
19
- return verify_person(image)
 
 
 
 
20
  except FaceNotDetectedError as exc:
21
  raise gr.Error(str(exc)) from exc
22
  except Exception as exc:
23
  raise gr.Error(str(exc)) from exc
24
 
25
 
26
- with gr.Blocks() as FV_gr:
27
- gr.Markdown(
28
- "# Face Verification Tool\n"
29
- "Upload an image and assign a name to add it to the embeddings database."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  )
 
 
 
 
 
 
 
 
 
 
31
  with gr.Tabs():
32
- with gr.TabItem("Add Person"):
33
- with gr.Row():
34
- input_image = gr.Image(label="Upload an image with a clear face", type="pil")
35
- input_name = gr.Textbox(label="Person name", placeholder="Example: John Doe")
36
- output_image = gr.Image(label="Detection result")
37
- submit_btn = gr.Button("Add to database")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  submit_btn.click(
39
  fn=add_person_ui,
40
  inputs=[input_image, input_name],
41
  outputs=output_image,
42
  )
43
- with gr.TabItem("Verify Identity"):
44
- with gr.Row():
45
- verify_image = gr.Image(label="Upload an image to verify", type="pil")
46
- verify_btn = gr.Button("Verify identity")
47
- verify_output_name = gr.Textbox(label="Verification result")
48
- verify_output_image = gr.Image(label="Detected face")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  verify_btn.click(
50
  fn=verify_person_ui,
51
  inputs=verify_image,
@@ -54,7 +167,12 @@ with gr.Blocks() as FV_gr:
54
 
55
 
56
  def main():
57
- FV_gr.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
58
 
59
 
60
  if __name__ == "__main__":
 
5
  from faceverification.services.face_verification import add_person, verify_person
6
 
7
 
8
+ def add_person_ui(image: Image.Image | None, name: str) -> Image.Image:
9
  try:
10
+ if image is None:
11
+ raise gr.Error("Upload an image before adding a person.")
12
+ if not name or not name.strip():
13
+ raise gr.Error("Enter a name before adding the person.")
14
+
15
+ return add_person(image, name.strip())
16
  except FaceNotDetectedError as exc:
17
  raise gr.Error(str(exc)) from exc
18
  except Exception as exc:
19
  raise gr.Error(str(exc)) from exc
20
 
21
 
22
+ def verify_person_ui(image: Image.Image | None) -> tuple[str, Image.Image]:
23
  try:
24
+ if image is None:
25
+ raise gr.Error("Upload an image before verifying an identity.")
26
+
27
+ name, annotated_image = verify_person(image)
28
+ return f"Result: {name}", annotated_image
29
  except FaceNotDetectedError as exc:
30
  raise gr.Error(str(exc)) from exc
31
  except Exception as exc:
32
  raise gr.Error(str(exc)) from exc
33
 
34
 
35
+ APP_CSS = """
36
+ .app-shell {
37
+ max-width: 1040px;
38
+ margin: 0 auto;
39
+ }
40
+
41
+ .hero {
42
+ padding: 18px 2px 8px;
43
+ }
44
+
45
+ .hero h1 {
46
+ margin-bottom: 8px;
47
+ }
48
+
49
+ .hero p {
50
+ max-width: 760px;
51
+ color: var(--body-text-color-subdued);
52
+ font-size: 1rem;
53
+ }
54
+
55
+ .hint {
56
+ color: var(--body-text-color-subdued);
57
+ font-size: 0.94rem;
58
+ line-height: 1.5;
59
+ }
60
+
61
+ .result-box textarea {
62
+ font-size: 1.15rem !important;
63
+ font-weight: 650 !important;
64
+ }
65
+
66
+ .primary-action {
67
+ margin-top: 8px;
68
+ }
69
+ """
70
+
71
+ APP_THEME = gr.themes.Soft(primary_hue="blue", secondary_hue="green")
72
+
73
+
74
+ with (
75
+ gr.Blocks(
76
+ title="Face Verification Demo",
77
+ ) as FV_gr,
78
+ gr.Column(elem_classes="app-shell"),
79
+ ):
80
+ gr.HTML(
81
+ """
82
+ <div class="hero">
83
+ <h1>Face Verification Demo</h1>
84
+ <p>
85
+ Register a known face, then upload another image to check whether it
86
+ matches an identity stored in the temporary embeddings database.
87
+ </p>
88
+ </div>
89
+ """
90
  )
91
+
92
+ gr.HTML(
93
+ """
94
+ <span class="hint">
95
+ Use clear, front-facing photos with one visible face. The demo stores embeddings
96
+ only for this running session, so the database may reset when the Space restarts.
97
+ </span>
98
+ """
99
+ )
100
+
101
  with gr.Tabs():
102
+ with gr.TabItem("1. Add Person"):
103
+ with gr.Row(equal_height=True):
104
+ with gr.Column(scale=1):
105
+ input_image = gr.Image(
106
+ label="Reference image",
107
+ type="pil",
108
+ height=360,
109
+ )
110
+ input_name = gr.Textbox(
111
+ label="Person name",
112
+ placeholder="Example: Ada Lovelace",
113
+ )
114
+ submit_btn = gr.Button(
115
+ "Add person to database",
116
+ variant="primary",
117
+ elem_classes="primary-action",
118
+ )
119
+ with gr.Column(scale=1):
120
+ output_image = gr.Image(
121
+ label="Detected face",
122
+ height=420,
123
+ )
124
+ gr.HTML(
125
+ """
126
+ <span class="hint">
127
+ If a face is detected, the annotated image confirms what face was stored.
128
+ </span>
129
+ """
130
+ )
131
+
132
  submit_btn.click(
133
  fn=add_person_ui,
134
  inputs=[input_image, input_name],
135
  outputs=output_image,
136
  )
137
+
138
+ with gr.TabItem("2. Verify Identity"):
139
+ with gr.Row(equal_height=True):
140
+ with gr.Column(scale=1):
141
+ verify_image = gr.Image(
142
+ label="Image to verify",
143
+ type="pil",
144
+ height=360,
145
+ )
146
+ verify_btn = gr.Button(
147
+ "Verify identity",
148
+ variant="primary",
149
+ elem_classes="primary-action",
150
+ )
151
+ with gr.Column(scale=1):
152
+ verify_output_name = gr.Textbox(
153
+ label="Verification result",
154
+ interactive=False,
155
+ elem_classes="result-box",
156
+ )
157
+ verify_output_image = gr.Image(
158
+ label="Detected face",
159
+ height=360,
160
+ )
161
+
162
  verify_btn.click(
163
  fn=verify_person_ui,
164
  inputs=verify_image,
 
167
 
168
 
169
  def main():
170
+ FV_gr.launch(
171
+ server_name="0.0.0.0",
172
+ server_port=7860,
173
+ theme=APP_THEME,
174
+ css=APP_CSS,
175
+ )
176
 
177
 
178
  if __name__ == "__main__":