Doom008 commited on
Commit
b7724a3
·
verified ·
1 Parent(s): 75f9750

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import os
4
+ import cv2
5
+ import pandas as pd
6
+ from deepface import DeepFace
7
+
8
+ # --- Setup: Create known_faces directory ---
9
+ known_faces_dir = "known_faces"
10
+ if not os.path.exists(known_faces_dir):
11
+ os.makedirs(known_faces_dir)
12
+ # DeepFace creates a .pkl file for representations. We can ignore it.
13
+ if os.path.exists(os.path.join(known_faces_dir, "representations_vgg_face.pkl")):
14
+ os.remove(os.path.join(known_faces_dir, "representations_vgg_face.pkl"))
15
+
16
+
17
+ # --- The Recognition Function using DeepFace ---
18
+ def recognize_face_deepface(uploaded_image_pil):
19
+ # Check if there are any known faces to compare against
20
+ if not os.listdir(known_faces_dir):
21
+ return "No known faces found. Please upload images to the 'known_faces' directory in your Space."
22
+
23
+ # DeepFace works with file paths, so we save the uploaded image temporarily
24
+ temp_image_path = "temp_uploaded_image.jpg"
25
+ uploaded_image_pil.save(temp_image_path)
26
+
27
+ try:
28
+ # Use DeepFace.find() to search for the face in our database directory
29
+ # It will return a list of pandas DataFrames. If the list is empty, no match was found.
30
+ # model_name='VGG-Face' is the default and works well.
31
+ # distance_metric='cosine' is also a standard choice.
32
+ dfs = DeepFace.find(
33
+ img_path=temp_image_path,
34
+ db_path=known_faces_dir,
35
+ model_name='VGG-Face',
36
+ enforce_detection=True # Set to False if you want it to work even if the face alignment is poor
37
+ )
38
+
39
+ # The result 'dfs' is a list of DataFrames. We check the first one.
40
+ if not dfs or dfs[0].empty:
41
+ return "Unknown (No similar face found in the database)"
42
+
43
+ # If we have a result, extract the path of the most similar face
44
+ most_similar_face_path = dfs[0].iloc[0]['identity']
45
+
46
+ # Get the name from the filename
47
+ # e.g., 'known_faces/susan.jpg' -> 'susan'
48
+ filename = os.path.basename(most_similar_face_path)
49
+ name = os.path.splitext(filename)[0]
50
+
51
+ return f"Recognized: {name}"
52
+
53
+ except ValueError as e:
54
+ # This error is often thrown by DeepFace if no face is detected in the uploaded image
55
+ return "No face detected in the uploaded image. Please try another one."
56
+ except Exception as e:
57
+ return f"An error occurred: {str(e)}"
58
+ finally:
59
+ # Clean up the temporary image file
60
+ if os.path.exists(temp_image_path):
61
+ os.remove(temp_image_path)
62
+
63
+
64
+ # --- Create the Gradio Interface ---
65
+ demo = gr.Interface(
66
+ fn=recognize_face_deepface,
67
+ inputs=gr.Image(type="pil", label="Upload an image to recognize"),
68
+ outputs=gr.Textbox(label="Recognition Result", api_name="recognize")
69
+ )
70
+
71
+ # Launch the app
72
+ demo.launch()