RajaThor commited on
Commit
1eb4ee2
·
verified ·
1 Parent(s): 50227eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -119,18 +119,18 @@ def detect_and_align_faces(image):
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}/"
@@ -146,6 +146,7 @@ def add_person(name, image_path, instagram_handle, email=None):
146
  except Exception as e:
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:
@@ -160,11 +161,11 @@ def recognize_face(image_path):
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)
@@ -248,14 +249,16 @@ def send_feedback(feedback_data):
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
 
119
  return aligned_faces
120
 
121
  # Add person to database
122
+ def add_person(name, image_paths, instagram_handle, email=None):
123
  try:
124
+ encodings = []
125
+ for image_path in image_paths:
126
+ encoding = load_and_encode(image_path)
127
+ if not encoding:
128
+ return f"No face found in {image_path}."
129
+ encodings.append(encoding[0].tolist())
130
 
131
  # Save data to Firebase Realtime Database
132
  person_data = {
133
+ "encodings": encodings,
134
  "info": {
135
  "instagram_handle": instagram_handle,
136
  "instagram_link": f"https://www.instagram.com/{instagram_handle}/"
 
146
  except Exception as e:
147
  return f"Failed to add person: {str(e)}"
148
 
149
+ # Update recognize_face function to handle multiple face encodings
150
  # Update recognize_face function to handle multiple face encodings
151
  def recognize_face(image_path):
152
  if not image_path:
 
161
  for unknown_encoding in unknown_encodings:
162
  face_matches = []
163
  for name, data in ref.get().items():
164
+ for known_encoding in data["encodings"]:
165
+ if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
166
+ info = data["info"]
167
+ email = info.get("email", "Email not provided")
168
+ face_matches.append((name, info["instagram_handle"], email))
169
 
170
  if face_matches:
171
  matches.extend(face_matches)
 
249
  def add_person_ui():
250
  st.title("😎 Add Person")
251
  name = st.text_input("Enter Name", help="Enter the name of the person")
252
+ image_paths = st.file_uploader("Upload Images", accept_multiple_files=True, help="Upload 1 to 3 images containing the person's face")
253
  email = st.text_input("Enter Email (Optional)", help="Enter the person's email address (optional)")
254
  instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
255
  if st.button("Add Person"):
256
+ if not name or not image_paths or not instagram_handle:
257
  st.error("Please fill all the required fields.")
258
+ elif len(image_paths) < 1 or len(image_paths) > 3:
259
+ st.error("Please upload 1 to 3 images.")
260
  else:
261
+ result = add_person(name, image_paths, instagram_handle, email)
262
  st.success(result)
263
 
264
  # Streamlit interface for recognizing face