RajaThor commited on
Commit
e1ebce1
Β·
verified Β·
1 Parent(s): 71acc83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -45
app.py CHANGED
@@ -63,35 +63,34 @@ def create_user(email, password):
63
  return False, None
64
 
65
  # Update load_and_encode function to return encodings for all detected faces
66
- def load_and_encode(image_file):
67
- try:
68
- # Read the uploaded file as bytes
69
- image_bytes = image_file.read()
70
-
71
- # Convert the bytes to a NumPy array
72
- nparr = np.frombuffer(image_bytes, np.uint8)
73
-
74
- # Decode the NumPy array as an image
75
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
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):
@@ -147,24 +146,22 @@ def add_person(name, image_path, instagram_handle, email=None):
147
  return f"Failed to add person: {str(e)}"
148
 
149
  # Update recognize_face function to handle multiple face encodings
150
- def recognize_face(image_path):
151
- if not image_path:
152
- return "Please upload an image."
153
 
154
  try:
155
- unknown_encodings = load_and_encode(image_path)
156
- if not unknown_encodings:
157
- return "No face found in the provided image."
158
-
159
  matches = []
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)
@@ -261,10 +258,14 @@ def add_person_ui():
261
  # Streamlit interface for recognizing face
262
  def recognize_face_ui():
263
  st.title("πŸ” Recognize Face")
264
- image_path = st.file_uploader("Upload Image", help="Upload an image for face recognition")
265
  if st.button("Recognize Face"):
266
- result = recognize_face(image_path)
267
- st.write(result, unsafe_allow_html=True)
 
 
 
 
268
 
269
  def recognize_face_optimal_ui():
270
  st.title("πŸ” Recognize Face (Optimal)")
 
63
  return False, None
64
 
65
  # Update load_and_encode function to return encodings for all detected faces
66
+ def load_and_encode(image_paths):
67
+ encodings_list = []
68
+ for image_path in image_paths:
69
+ try:
70
+ # Read the uploaded file as bytes
71
+ image_bytes = image_path.read()
72
+
73
+ # Convert the bytes to a NumPy array
74
+ nparr = np.frombuffer(image_bytes, np.uint8)
75
+
76
+ # Decode the NumPy array as an image
77
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
78
+
79
+ aligned_faces = detect_and_align_faces(image)
80
+
81
+ if aligned_faces is not None:
82
+ encodings = []
83
+ for aligned_face in aligned_faces:
84
+ encoding = face_recognition.face_encodings(aligned_face)
85
+ if encoding:
86
+ encodings.append(encoding[0])
87
+
88
+ if encodings:
89
+ encodings_list.append(encodings)
90
+ except Exception as e:
91
+ print(f"Error loading and encoding image: {str(e)}")
92
+
93
+ return encodings_list
 
94
 
95
  # Modify detect_and_align_faces function to detect and align multiple faces
96
  def detect_and_align_faces(image):
 
146
  return f"Failed to add person: {str(e)}"
147
 
148
  # Update recognize_face function to handle multiple face encodings
149
+ def recognize_face(encodings_list):
150
+ if not encodings_list:
151
+ return "Please upload images."
152
 
153
  try:
 
 
 
 
154
  matches = []
155
+ for unknown_encodings in encodings_list:
156
  face_matches = []
157
  for name, data in ref.get().items():
158
+ known_encodings = data["encodings"]
159
+ for known_encoding in known_encodings:
160
+ for unknown_encoding in unknown_encodings:
161
+ if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
162
+ info = data["info"]
163
+ email = info.get("email", "Email not provided")
164
+ face_matches.append((name, info["instagram_handle"], email))
165
 
166
  if face_matches:
167
  matches.extend(face_matches)
 
258
  # Streamlit interface for recognizing face
259
  def recognize_face_ui():
260
  st.title("πŸ” Recognize Face")
261
+ image_paths = st.file_uploader("Upload Image(s)", accept_multiple_files=True, help="Upload image(s) for face recognition")
262
  if st.button("Recognize Face"):
263
+ if not image_paths:
264
+ st.error("Please upload at least one image.")
265
+ else:
266
+ result = recognize_face(image_paths)
267
+ st.write(result, unsafe_allow_html=True)
268
+
269
 
270
  def recognize_face_optimal_ui():
271
  st.title("πŸ” Recognize Face (Optimal)")