awacke1 commited on
Commit
ae60ab6
·
verified ·
1 Parent(s): f66e643

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -427
app.py CHANGED
@@ -3,473 +3,126 @@ import gradio as gr
3
  import requests
4
  import json
5
  from PIL import Image
 
 
6
 
7
- css = """
8
- .example-image img{
9
- display: flex; /* Use flexbox to align items */
10
- justify-content: center; /* Center the image horizontally */
11
- align-items: center; /* Center the image vertically */
12
- height: 300px; /* Set the height of the container */
13
- object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
14
- }
15
 
16
- .example-image{
17
- display: flex; /* Use flexbox to align items */
18
- justify-content: center; /* Center the image horizontally */
19
- align-items: center; /* Center the image vertically */
20
- height: 350px; /* Set the height of the container */
21
- object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
22
- }
23
-
24
- .face-row {
25
- display: flex;
26
- justify-content: space-around; /* Distribute space evenly between elements */
27
- align-items: center; /* Align items vertically */
28
- width: 100%; /* Set the width of the row to 100% */
29
- }
30
-
31
- .face-image{
32
- justify-content: center; /* Center the image horizontally */
33
- align-items: center; /* Center the image vertically */
34
- height: 160px; /* Set the height of the container */
35
- width: 160px;
36
- object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
37
- }
38
-
39
- .face-image img{
40
- justify-content: center; /* Center the image horizontally */
41
- align-items: center; /* Center the image vertically */
42
- height: 160px; /* Set the height of the container */
43
- object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
44
- }
45
-
46
- .markdown-success-container {
47
- background-color: #F6FFED;
48
- padding: 20px;
49
- margin: 20px;
50
- border-radius: 1px;
51
- border: 2px solid green;
52
- text-align: center;
53
- }
54
-
55
- .markdown-fail-container {
56
- background-color: #FFF1F0;
57
- padding: 20px;
58
- margin: 20px;
59
- border-radius: 1px;
60
- border: 2px solid red;
61
- text-align: center;
62
- }
63
-
64
- .markdown-attribute-container {
65
- display: flex;
66
- justify-content: space-around; /* Distribute space evenly between elements */
67
- align-items: center; /* Align items vertically */
68
- padding: 10px;
69
- margin: 10px;
70
- }
71
-
72
- .block-background {
73
- # background-color: #202020; /* Set your desired background color */
74
- border-radius: 5px;
75
- }
76
-
77
- """
78
-
79
- def convert_fun(input_str):
80
- # Remove line breaks and extra whitespaces
81
- return ' '.join(input_str.split())
82
-
83
- def get_attributes(frame):
84
  url = "https://recognito.p.rapidapi.com/api/analyze_face"
85
  try:
86
- files = {'image': open(frame, 'rb')}
 
 
 
 
 
 
87
  headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
88
-
89
  r = requests.post(url=url, files=files, headers=headers)
90
- except:
91
- raise gr.Error("Please select images file!")
92
-
93
- faces = None
94
- face_crop, one_line_attribute = None, ""
95
- try:
96
- image = Image.open(frame)
97
-
98
- face = Image.new('RGBA',(150, 150), (80,80,80,0))
99
-
100
- res = r.json().get('image')
101
- if res is not None and res:
102
- face = res.get('detection')
103
- x1 = face.get('x')
104
- y1 = face.get('y')
105
- x2 = x1 + face.get('w')
106
- y2 = y1 + face.get('h')
107
-
108
- if x1 < 0:
109
- x1 = 0
110
- if y1 < 0:
111
- y1 = 0
112
- if x2 >= image.width:
113
- x2 = image.width - 1
114
- if y2 >= image.height:
115
- y2 = image.height - 1
116
-
117
- face_crop = image.crop((x1, y1, x2, y2))
118
- face_image_ratio = face_crop.width / float(face_crop.height)
119
- resized_w = int(face_image_ratio * 150)
120
- resized_h = 150
121
-
122
- face_crop = face_crop.resize((int(resized_w), int(resized_h)))
123
-
124
- attr = res.get('attribute')
125
-
126
- age = attr.get('age')
127
- gender = attr.get('gender')
128
- emotion = attr.get('emotion')
129
- ethnicity = attr.get('ethnicity')
130
-
131
- mask = attr.get('face_mask')
132
- glass = 'No Glasses'
133
- if attr.get('glasses') == 'USUAL':
134
- glass = 'Glasses'
135
- if attr.get('glasses') == 'DARK':
136
- glass = 'Sunglasses'
137
-
138
- open_eye_thr = 0.3
139
- left_eye = 'Close'
140
- if attr.get('eye_left') >= open_eye_thr:
141
- left_eye = 'Open'
142
-
143
- right_eye = 'Close'
144
- if attr.get('eye_right') >= open_eye_thr:
145
- right_eye = 'Open'
146
-
147
- facehair = attr.get('facial_hair')
148
- haircolor = attr.get('hair_color')
149
- hairtype = attr.get('hair_type')
150
- headwear = attr.get('headwear')
151
-
152
- pitch = attr.get('pitch')
153
- roll = attr.get('roll')
154
- yaw = attr.get('yaw')
155
- quality = attr.get('quality')
156
-
157
- attribute = f"""
158
- <br/>
159
- <div class="markdown-attribute-container">
160
- <table>
161
- <tr>
162
- <th style="text-align: center;">Attribute</th>
163
- <th style="text-align: center;">Result</th>
164
- <th style="text-align: center;">Score</th>
165
- <th style="text-align: center;">Threshold</th>
166
- </tr>
167
- <tr>
168
- <td>Gender</td>
169
- <td>{gender}</td>
170
- <td></td><td></td>
171
- </tr>
172
- <tr>
173
- <td>Age</td>
174
- <td>{int(age)}</td>
175
- <td></td><td></td>
176
- </tr>
177
- <tr>
178
- <td>Pitch</td>
179
- <td>{"{:.4f}".format(pitch)}</td>
180
- <td></td><td></td>
181
- </tr>
182
- <tr>
183
- <td>Yaw</td>
184
- <td>{"{:.4f}".format(yaw)}</td>
185
- <td></td><td></td>
186
- </tr>
187
- <tr>
188
- <td>Roll</td>
189
- <td>{"{:.4f}".format(roll)}</td>
190
- <td></td><td></td>
191
- </tr>
192
- <tr>
193
- <td>Emotion</td>
194
- <td>{emotion}</td>
195
- <td></td><td></td>
196
- </tr>
197
- <tr>
198
- <td>Left Eye</td>
199
- <td>{left_eye}</td>
200
- <td>{"{:.4f}".format(attr.get('eye_left'))}</td>
201
- <td>{open_eye_thr}</td>
202
- </tr>
203
- <tr>
204
- <td>Right Eye</td>
205
- <td>{right_eye}</td>
206
- <td>{"{:.4f}".format(attr.get('eye_right'))}</td>
207
- <td>{open_eye_thr}</td>
208
- </tr>
209
- <tr>
210
- <td>Mask</td>
211
- <td>{mask}</td>
212
- <td></td><td></td>
213
- </tr>
214
- <tr>
215
- <td>Glass</td>
216
- <td>{glass}</td>
217
- <td></td><td></td>
218
- </tr>
219
- <tr>
220
- <td>FaceHair</td>
221
- <td>{facehair}</td>
222
- <td></td><td></td>
223
- </tr>
224
- <tr>
225
- <td>HairColor</td>
226
- <td>{haircolor}</td>
227
- <td></td><td></td>
228
- </tr>
229
- <tr>
230
- <td>HairType</td>
231
- <td>{hairtype}</td>
232
- <td></td><td></td>
233
- </tr>
234
- <tr>
235
- <td>HeadWear</td>
236
- <td>{headwear}</td>
237
- <td></td><td></td>
238
- </tr>
239
- <tr>
240
- <td>Image Quality</td>
241
- <td>{"{:.4f}".format(quality)}</td>
242
- <td></td><td></td>
243
- </tr>
244
- </table>
245
- </div>
246
- """
247
- one_line_attribute = convert_fun(attribute)
248
- except:
249
- pass
250
 
 
 
251
  return face_crop, one_line_attribute
252
 
253
  def check_liveness(frame):
 
 
254
 
255
  url = "https://recognito-faceliveness.p.rapidapi.com/api/check_liveness"
256
  try:
257
- files = {'image': open(frame, 'rb')}
 
 
 
 
 
258
  headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
259
-
260
  r = requests.post(url=url, files=files, headers=headers)
261
- except:
262
- raise gr.Error("Please select images file!")
263
-
264
- faces = None
265
-
266
- face_crop, liveness_result, liveness_score = None, "", -200
267
- try:
268
- image = Image.open(frame)
269
-
270
- face = Image.new('RGBA',(150, 150), (80,80,80,0))
271
- res = r.json().get('data')
272
- if res is not None and res:
273
- face = res.get('face_rect')
274
- x1 = face.get('x')
275
- y1 = face.get('y')
276
- x2 = x1 + face.get('w')
277
- y2 = y1 + face.get('h')
278
-
279
- if x1 < 0:
280
- x1 = 0
281
- if y1 < 0:
282
- y1 = 0
283
- if x2 >= image.width:
284
- x2 = image.width - 1
285
- if y2 >= image.height:
286
- y2 = image.height - 1
287
-
288
- face_crop = image.crop((x1, y1, x2, y2))
289
- face_image_ratio = face_crop.width / float(face_crop.height)
290
- resized_w = int(face_image_ratio * 150)
291
- resized_h = 150
292
-
293
- face_crop = face_crop.resize((int(resized_w), int(resized_h)))
294
- liveness_score = res.get('liveness_score')
295
- liveness = res.get('result')
296
-
297
- if liveness == 'REAL':
298
- liveness_result = f"""<br/><div class="markdown-success-container"><p style="text-align: center; font-size: 20px; color: green;">Liveness Check: REAL<br/>Score: {liveness_score}</p></div>"""
299
- else:
300
- liveness_result = f"""<br/><div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check: {liveness}<br/>Score: {liveness_score}</p></div>"""
301
-
302
- except:
303
- pass
304
 
 
 
305
  return face_crop, liveness_result, liveness_score
306
 
307
  def analyze_face(frame):
 
 
 
308
  face_crop_1, liveness_result, liveness_score = check_liveness(frame)
309
  face_crop_2, attribute = get_attributes(frame)
310
-
311
- face_crop = face_crop_1 if (face_crop_1 is not None) else face_crop_2
312
- return [face_crop, liveness_result, attribute]
313
-
314
-
315
- def compare_face(frame1, frame2):
316
- url = "https://recognito.p.rapidapi.com/api/compare_face"
317
- try:
318
- files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
319
- headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
320
-
321
- r = requests.post(url=url, files=files, headers=headers)
322
- except:
323
- raise gr.Error("Please select images files!")
324
-
325
- faces = None
326
-
327
- try:
328
- image1 = Image.open(frame1)
329
- image2 = Image.open(frame2)
330
-
331
- face1 = Image.new('RGBA',(150, 150), (80,80,80,0))
332
- face2 = Image.new('RGBA',(150, 150), (80,80,80,0))
333
-
334
- res1 = r.json().get('image1')
335
-
336
- if res1 is not None and res1:
337
- face = res1.get('detection')
338
- x1 = face.get('x')
339
- y1 = face.get('y')
340
- x2 = x1 + face.get('w')
341
- y2 = y1 + face.get('h')
342
- if x1 < 0:
343
- x1 = 0
344
- if y1 < 0:
345
- y1 = 0
346
- if x2 >= image1.width:
347
- x2 = image1.width - 1
348
- if y2 >= image1.height:
349
- y2 = image1.height - 1
350
-
351
- face1 = image1.crop((x1, y1, x2, y2))
352
- face_image_ratio = face1.width / float(face1.height)
353
- resized_w = int(face_image_ratio * 150)
354
- resized_h = 150
355
-
356
- face1 = face1.resize((int(resized_w), int(resized_h)))
357
-
358
- res2 = r.json().get('image2')
359
- if res2 is not None and res2:
360
- face = res2.get('detection')
361
- x1 = face.get('x')
362
- y1 = face.get('y')
363
- x2 = x1 + face.get('w')
364
- y2 = y1 + face.get('h')
365
-
366
- if x1 < 0:
367
- x1 = 0
368
- if y1 < 0:
369
- y1 = 0
370
- if x2 >= image2.width:
371
- x2 = image2.width - 1
372
- if y2 >= image2.height:
373
- y2 = image2.height - 1
374
-
375
- face2 = image2.crop((x1, y1, x2, y2))
376
- face_image_ratio = face2.width / float(face2.height)
377
- resized_w = int(face_image_ratio * 150)
378
- resized_h = 150
379
-
380
- face2 = face2.resize((int(resized_w), int(resized_h)))
381
- except:
382
- pass
383
-
384
- matching_result = Image.open("icons/blank.png")
385
- similarity_score = ""
386
- if face1 is not None and face2 is not None:
387
- matching_score = r.json().get('matching_score')
388
- if matching_score is not None:
389
- str_score = str("{:.4f}".format(matching_score))
390
- if matching_score >= 0.7:
391
- matching_result = Image.open("icons/same.png")
392
- similarity_score = f"""<br/><div class="markdown-success-container"><p style="text-align: center; font-size: 20px; color: green;">Similarity score: {str_score}</p></div>"""
393
- else:
394
- matching_result = Image.open("icons/different.png")
395
- similarity_score = f"""<br/><div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Similarity score: {str_score}</p></div>"""
396
 
397
- return [face1, face2, matching_result, similarity_score]
398
-
399
-
400
- def image_change_callback(image_data):
401
- # This function will be called whenever a new image is set for the gr.Image component
402
- print("New image set:", image_data)
403
 
 
404
  with gr.Blocks(css=css) as demo:
405
  gr.Markdown(
406
- """
407
- <a href="https://recognito.vision" style="display: flex; align-items: center;">
408
- <img src="https://recognito.vision/wp-content/uploads/2024/03/Recognito-modified.png" style="width: 8%; margin-right: 15px;"/>
409
- <div>
410
- <p style="font-size: 32px; font-weight: bold; margin: 0;">Recognito</p>
411
- <p style="font-size: 18px; margin: 0;">www.recognito.vision</p>
412
- </div>
413
- </a>
414
-
415
- <p style="font-size: 20px; font-weight: bold;">✨ NIST FRVT Top #1 Face Recognition Algorithm Developer</p>
416
- <div style="display: flex; align-items: center;">
417
- &emsp;&emsp;<a href="https://pages.nist.gov/frvt/html/frvt11.html"> <p style="font-size: 14px;">👉🏻 Latest NIST FRVT Report</p></a>
418
- </div>
419
- <p style="font-size: 20px; font-weight: bold;">🤝 Contact us for our on-premise SDKs deployment</p>
420
- </div><div style="display: flex; align-items: center;">
421
- &emsp;&emsp;<a target="_blank" href="mailto:[email protected]"><img src="https://img.shields.io/badge/[email protected]?logo=gmail " alt="www.recognito.vision"></a>
422
- &nbsp;&nbsp;&nbsp;&nbsp;<a target="_blank" href="https://wa.me/+14158003112"><img src="https://img.shields.io/badge/whatsapp-recognito-blue.svg?logo=whatsapp " alt="www.recognito.vision"></a>
423
- &nbsp;&nbsp;&nbsp;&nbsp;<a target="_blank" href="https://t.me/recognito_vision"><img src="https://img.shields.io/badge/[email protected]?logo=telegram " alt="www.recognito.vision"></a>
424
- &nbsp;&nbsp;&nbsp;&nbsp;<a target="_blank" href="https://join.slack.com/t/recognito-workspace/shared_invite/zt-2d4kscqgn-"><img src="https://img.shields.io/badge/slack-recognito-blue.svg?logo=slack " alt="www.recognito.vision"></a>
425
- </div>
426
- <br/><br/><br/>
427
- """
428
  )
429
 
430
  with gr.Tabs():
431
  with gr.Tab("Face Recognition"):
432
- with gr.Row():
433
- with gr.Column(scale=2):
434
- with gr.Row():
435
- with gr.Column(scale=1):
436
- compare_face_input1 = gr.Image(label="Image1", type='filepath', elem_classes="example-image")
437
- gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg', 'examples/4.jpg'],
438
- inputs=compare_face_input1)
439
- with gr.Column(scale=1):
440
- compare_face_input2 = gr.Image(label="Image2", type='filepath', elem_classes="example-image")
441
- gr.Examples(['examples/5.jpg', 'examples/6.jpg', 'examples/7.jpg', 'examples/8.jpg'],
442
- inputs=compare_face_input2)
443
-
444
- with gr.Blocks():
445
- with gr.Column(scale=1, min_width=400, elem_classes="block-background"):
446
- compare_face_button = gr.Button("Compare Face", variant="primary", size="lg")
447
- with gr.Row(elem_classes="face-row"):
448
- face_output1 = gr.Image(value="icons/face.jpg", label="Face 1", scale=0, elem_classes="face-image")
449
- compare_result = gr.Image(value="icons/blank.png", min_width=30, scale=0, show_download_button=False, show_label=False)
450
- face_output2 = gr.Image(value="icons/face.jpg", label="Face 2", scale=0, elem_classes="face-image")
451
- similarity_markdown = gr.Markdown("")
452
-
453
- compare_face_button.click(compare_face, inputs=[compare_face_input1, compare_face_input2], outputs=[face_output1, face_output2, compare_result, similarity_markdown])
454
-
455
  with gr.Tab("Face Liveness, Analysis"):
456
  with gr.Row():
457
  with gr.Column(scale=1):
458
- face_input = gr.Image(label="Image", type='filepath', elem_classes="example-image")
459
- gr.Examples(['examples/att_1.jpg', 'examples/att_2.jpg', 'examples/att_3.jpg', 'examples/att_4.jpg', 'examples/att_5.jpg', 'examples/att_6.jpg', 'examples/att_7.jpg', 'examples/att_8.jpg', 'examples/att_9.jpg', 'examples/att_10.jpg'],
460
- inputs=face_input)
 
 
 
 
 
 
 
 
 
461
 
462
  with gr.Blocks():
463
  with gr.Column(scale=1, elem_classes="block-background"):
464
  analyze_face_button = gr.Button("Analyze Face", variant="primary", size="lg")
465
  with gr.Row(elem_classes="face-row"):
466
- face_output = gr.Image(value="icons/face.jpg", label="Face", scale=0, elem_classes="face-image")
 
 
 
 
 
467
 
468
  liveness_result = gr.Markdown("")
469
  attribute_result = gr.Markdown("")
470
 
471
- analyze_face_button.click(analyze_face, inputs=face_input, outputs=[face_output, liveness_result, attribute_result])
472
-
473
- gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FRecognito%2FFaceRecognition-LivenessDetection-FaceAnalysis"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FRecognito%2FFaceRecognition-LivenessDetection-FaceAnalysis&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
 
475
  demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
 
3
  import requests
4
  import json
5
  from PIL import Image
6
+ import cv2
7
+ import numpy as np
8
 
9
+ # Your existing CSS remains unchanged
10
+ # I'll keep your helper functions (convert_fun, get_attributes, check_liveness) mostly the same
11
+ # but add better error handling
 
 
 
 
 
12
 
13
+ def get_attributes(frame):
14
+ if frame is None:
15
+ return None, "No image provided"
16
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  url = "https://recognito.p.rapidapi.com/api/analyze_face"
18
  try:
19
+ # If frame is a numpy array from webcam, convert to bytes
20
+ if isinstance(frame, np.ndarray):
21
+ _, buffer = cv2.imencode('.jpg', frame)
22
+ files = {'image': ('image.jpg', buffer.tobytes(), 'image/jpeg')}
23
+ else:
24
+ files = {'image': open(frame, 'rb')}
25
+
26
  headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
 
27
  r = requests.post(url=url, files=files, headers=headers)
28
+ except Exception as e:
29
+ return None, f"Error processing image: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # Rest of your get_attributes function remains the same
32
+ # ... (keeping your existing logic)
33
  return face_crop, one_line_attribute
34
 
35
  def check_liveness(frame):
36
+ if frame is None:
37
+ return None, "No image provided", -200
38
 
39
  url = "https://recognito-faceliveness.p.rapidapi.com/api/check_liveness"
40
  try:
41
+ if isinstance(frame, np.ndarray):
42
+ _, buffer = cv2.imencode('.jpg', frame)
43
+ files = {'image': ('image.jpg', buffer.tobytes(), 'image/jpeg')}
44
+ else:
45
+ files = {'image': open(frame, 'rb')}
46
+
47
  headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
 
48
  r = requests.post(url=url, files=files, headers=headers)
49
+ except Exception as e:
50
+ return None, f"Error processing image: {str(e)}", -200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ # Rest of your check_liveness function remains the same
53
+ # ... (keeping your existing logic)
54
  return face_crop, liveness_result, liveness_score
55
 
56
  def analyze_face(frame):
57
+ if frame is None:
58
+ return [None, "No image provided", "Please provide an image or enable webcam"]
59
+
60
  face_crop_1, liveness_result, liveness_score = check_liveness(frame)
61
  face_crop_2, attribute = get_attributes(frame)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ face_crop = face_crop_1 if face_crop_1 is not None else face_crop_2
64
+ return [face_crop, liveness_result, attribute]
 
 
 
 
65
 
66
+ # Modified interface with webcam support
67
  with gr.Blocks(css=css) as demo:
68
  gr.Markdown(
69
+ # Your existing header markdown
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  )
71
 
72
  with gr.Tabs():
73
  with gr.Tab("Face Recognition"):
74
+ # Keeping your existing Face Recognition tab unchanged
75
+ # ...
76
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  with gr.Tab("Face Liveness, Analysis"):
78
  with gr.Row():
79
  with gr.Column(scale=1):
80
+ # Modified input to include webcam
81
+ face_input = gr.Image(
82
+ label="Image (Upload or Webcam)",
83
+ sources=["upload", "webcam"],
84
+ type='filepath', # Will be None for webcam frames
85
+ elem_classes="example-image",
86
+ streaming=True # Enable continuous webcam streaming
87
+ )
88
+ gr.Examples(
89
+ ['examples/att_1.jpg', 'examples/att_2.jpg', 'examples/att_3.jpg'],
90
+ inputs=face_input
91
+ )
92
 
93
  with gr.Blocks():
94
  with gr.Column(scale=1, elem_classes="block-background"):
95
  analyze_face_button = gr.Button("Analyze Face", variant="primary", size="lg")
96
  with gr.Row(elem_classes="face-row"):
97
+ face_output = gr.Image(
98
+ value="icons/face.jpg",
99
+ label="Face",
100
+ scale=0,
101
+ elem_classes="face-image"
102
+ )
103
 
104
  liveness_result = gr.Markdown("")
105
  attribute_result = gr.Markdown("")
106
 
107
+ # Updated event handler for both upload and webcam
108
+ analyze_face_button.click(
109
+ analyze_face,
110
+ inputs=face_input,
111
+ outputs=[face_output, liveness_result, attribute_result]
112
+ )
113
+ # Add streaming event for webcam
114
+ face_input.stream(
115
+ analyze_face,
116
+ inputs=face_input,
117
+ outputs=[face_output, liveness_result, attribute_result],
118
+ _js="""(image) => {
119
+ if (image && image.data) return image;
120
+ return null;
121
+ }"""
122
+ )
123
+
124
+ gr.HTML(
125
+ # Your existing footer HTML
126
+ )
127
 
128
  demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)