songhieng commited on
Commit
f06634b
·
verified ·
1 Parent(s): 0e92ff9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -75,16 +75,26 @@ def update_dataset_with_verified_image(image, encodings_file, label, num_augment
75
  images_to_encode = [image] + augmented_images
76
  for img in images_to_encode:
77
  img_array = np.array(img)
78
- encoding = face_recognition.face_encodings(img_array)[0]
79
- known_encodings = np.append(known_encodings, [encoding], axis=0)
80
- known_labels.append(label)
 
81
  save_encodings(known_encodings, known_labels, encodings_file)
82
 
83
  def verify_face_with_faiss(image, encodings_file, similarity_threshold=70, num_augmented=5):
84
  aligned_face = image.convert("RGB")
85
- target_encoding = face_recognition.face_encodings(np.array(aligned_face))[0].reshape(1, -1)
 
 
 
 
 
86
 
87
  known_encodings, known_labels = load_encodings(encodings_file)
 
 
 
 
88
  known_encodings = np.array(known_encodings)
89
 
90
  index = create_faiss_index(known_encodings)
@@ -103,17 +113,26 @@ def verify_face_with_faiss(image, encodings_file, similarity_threshold=70, num_a
103
  else:
104
  return False, "No match found."
105
 
106
- # Define the Gradio interface
107
  def gradio_interface(image, similarity_threshold=70):
108
- encodings_file = "face_encoding.pkl"
109
- result, message = verify_face_with_faiss(image, encodings_file, similarity_threshold=similarity_threshold)
110
- return message
 
 
 
 
 
 
 
111
 
112
- # Launch the Gradio interface
113
- iface = gr.Interface(fn=gradio_interface,
114
- inputs=[gr.Image(type="pil"), gr.Slider(0, 100, value=70, label="Similarity Threshold")],
115
- outputs="text",
116
- title="Face Recognition with MTCNN and FAISS",
117
- description="Upload an image to see if it matches any face in the database.")
 
 
118
 
119
- iface.launch()
 
75
  images_to_encode = [image] + augmented_images
76
  for img in images_to_encode:
77
  img_array = np.array(img)
78
+ encoding = face_recognition.face_encodings(img_array)
79
+ if encoding:
80
+ known_encodings = np.append(known_encodings, [encoding[0]], axis=0)
81
+ known_labels.append(label)
82
  save_encodings(known_encodings, known_labels, encodings_file)
83
 
84
  def verify_face_with_faiss(image, encodings_file, similarity_threshold=70, num_augmented=5):
85
  aligned_face = image.convert("RGB")
86
+ target_encodings = face_recognition.face_encodings(np.array(aligned_face))
87
+
88
+ if not target_encodings:
89
+ return False, "No face detected in the provided image. Please try with another image."
90
+
91
+ target_encoding = target_encodings[0].reshape(1, -1)
92
 
93
  known_encodings, known_labels = load_encodings(encodings_file)
94
+
95
+ if len(known_encodings) == 0:
96
+ return False, "No known faces in the database. Please add some faces first."
97
+
98
  known_encodings = np.array(known_encodings)
99
 
100
  index = create_faiss_index(known_encodings)
 
113
  else:
114
  return False, "No match found."
115
 
116
+ # Gradio Interface Function
117
  def gradio_interface(image, similarity_threshold=70):
118
+ if image is None:
119
+ return "Error: No image provided. Please upload an image or take one using the webcam."
120
+
121
+ encodings_file = "./face_encoding.pkl"
122
+
123
+ try:
124
+ result, message = verify_face_with_faiss(image, encodings_file, similarity_threshold=similarity_threshold)
125
+ return message
126
+ except Exception as e:
127
+ return f"An error occurred: {str(e)}"
128
 
129
+ # Gradio Interface Setup
130
+ iface = gr.Interface(
131
+ fn=gradio_interface,
132
+ inputs=[gr.Image(type="pil"), gr.Slider(0, 100, value=70, label="Similarity Threshold")],
133
+ outputs="text",
134
+ title="Face Recognition with MTCNN and FAISS",
135
+ description="Upload an image to see if it matches any face in the database."
136
+ )
137
 
138
+ iface.launch(share=True)