RajaThor commited on
Commit
71acc83
·
verified ·
1 Parent(s): 7bc1af3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -76,16 +76,22 @@ def load_and_encode(image_file):
76
 
77
  aligned_faces = detect_and_align_faces(image)
78
 
79
- encodings = []
80
- for aligned_face in aligned_faces:
81
- encoding = face_recognition.face_encodings(aligned_face)
82
- if encoding:
83
- encodings.append(encoding[0])
84
-
85
- return encodings
 
 
 
 
 
 
86
  except Exception as e:
87
  print(f"Error loading and encoding image: {str(e)}")
88
- return []
89
 
90
  # Modify detect_and_align_faces function to detect and align multiple faces
91
  def detect_and_align_faces(image):
@@ -111,20 +117,20 @@ def detect_and_align_faces(image):
111
  aligned_faces.append(aligned_face)
112
 
113
  return aligned_faces
114
-
115
  # Add person to database
116
- def add_person(name, image_paths, instagram_handle, email=None):
117
  try:
118
- encodings = []
119
- for image_path in image_paths:
120
- encoding = load_and_encode(image_path)
121
- if not encoding:
122
- return f"No face found in {image_path}."
123
- encodings.append(encoding[0].tolist())
124
 
125
  # Save data to Firebase Realtime Database
126
  person_data = {
127
- "encodings": encodings,
128
  "info": {
129
  "instagram_handle": instagram_handle,
130
  "instagram_link": f"https://www.instagram.com/{instagram_handle}/"
@@ -154,11 +160,11 @@ def recognize_face(image_path):
154
  for unknown_encoding in unknown_encodings:
155
  face_matches = []
156
  for name, data in ref.get().items():
157
- for known_encoding in data["encodings"]:
158
- if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
159
- info = data["info"]
160
- email = info.get("email", "Email not provided")
161
- face_matches.append((name, info["instagram_handle"], email))
162
 
163
  if face_matches:
164
  matches.extend(face_matches)
@@ -192,11 +198,11 @@ def recognize_face_optimal(image_path):
192
  for unknown_encoding in unknown_encodings:
193
  face_matches = []
194
  for name, data in ref.get().items():
195
- for known_encoding in data["encodings"]:
196
- similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding)[0]
197
- if similarity_score > 0.50: # Only consider matches above 50.00% similarity
198
- continue
199
- face_matches.append((name, similarity_score))
200
 
201
  if face_matches:
202
  best_match = min(face_matches, key=lambda x: x[1])
@@ -238,19 +244,18 @@ def send_feedback(feedback_data):
238
  except Exception as e:
239
  st.error(f"Failed to submit feedback: {str(e)}")
240
 
 
241
  def add_person_ui():
242
  st.title("😎 Add Person")
243
  name = st.text_input("Enter Name", help="Enter the name of the person")
244
- image_paths = st.file_uploader("Upload Images", accept_multiple_files=True, help="Upload 1 to 3 images containing the person's face")
245
  email = st.text_input("Enter Email (Optional)", help="Enter the person's email address (optional)")
246
  instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
247
  if st.button("Add Person"):
248
- if not name or not image_paths or not instagram_handle:
249
  st.error("Please fill all the required fields.")
250
- elif len(image_paths) < 1 or len(image_paths) > 3:
251
- st.error("Please upload 1 to 3 images.")
252
  else:
253
- result = add_person(name, image_paths, instagram_handle, email)
254
  st.success(result)
255
 
256
  # Streamlit interface for recognizing face
 
76
 
77
  aligned_faces = detect_and_align_faces(image)
78
 
79
+ if aligned_faces is not None:
80
+ encodings = []
81
+ for aligned_face in aligned_faces:
82
+ encoding = face_recognition.face_encodings(aligned_face)
83
+ if encoding:
84
+ encodings.append(encoding[0])
85
+
86
+ if encodings:
87
+ return encodings
88
+ else:
89
+ return None
90
+ else:
91
+ return None
92
  except Exception as e:
93
  print(f"Error loading and encoding image: {str(e)}")
94
+ return None
95
 
96
  # Modify detect_and_align_faces function to detect and align multiple faces
97
  def detect_and_align_faces(image):
 
117
  aligned_faces.append(aligned_face)
118
 
119
  return aligned_faces
120
+
121
  # Add person to database
122
+ def add_person(name, image_path, instagram_handle, email=None):
123
  try:
124
+ encoding = load_and_encode(image_path)
125
+ if not encoding:
126
+ return "No face found in the provided image."
127
+
128
+ # Convert NumPy arrays to lists for JSON serialization
129
+ encoding = encoding[0].tolist()
130
 
131
  # Save data to Firebase Realtime Database
132
  person_data = {
133
+ "encoding": encoding,
134
  "info": {
135
  "instagram_handle": instagram_handle,
136
  "instagram_link": f"https://www.instagram.com/{instagram_handle}/"
 
160
  for unknown_encoding in unknown_encodings:
161
  face_matches = []
162
  for name, data in ref.get().items():
163
+ known_encoding = np.array(data["encoding"])
164
+ if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
165
+ info = data["info"]
166
+ email = info.get("email", "Email not provided")
167
+ face_matches.append((name, info["instagram_handle"], email))
168
 
169
  if face_matches:
170
  matches.extend(face_matches)
 
198
  for unknown_encoding in unknown_encodings:
199
  face_matches = []
200
  for name, data in ref.get().items():
201
+ known_encoding = np.array(data["encoding"])
202
+ similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding)[0]
203
+ if similarity_score > 0.50: # Only consider matches above 50.00% similarity
204
+ continue
205
+ face_matches.append((name, similarity_score))
206
 
207
  if face_matches:
208
  best_match = min(face_matches, key=lambda x: x[1])
 
244
  except Exception as e:
245
  st.error(f"Failed to submit feedback: {str(e)}")
246
 
247
+ # Streamlit interface for adding a person
248
  def add_person_ui():
249
  st.title("😎 Add Person")
250
  name = st.text_input("Enter Name", help="Enter the name of the person")
251
+ image_path = st.file_uploader("Upload Image", help="Upload an image containing the person's face")
252
  email = st.text_input("Enter Email (Optional)", help="Enter the person's email address (optional)")
253
  instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
254
  if st.button("Add Person"):
255
+ if not name or not image_path or not instagram_handle:
256
  st.error("Please fill all the required fields.")
 
 
257
  else:
258
+ result = add_person(name, image_path, instagram_handle, email)
259
  st.success(result)
260
 
261
  # Streamlit interface for recognizing face