RajaThor commited on
Commit
e59dbba
Β·
verified Β·
1 Parent(s): 931fe2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -59
app.py CHANGED
@@ -63,34 +63,35 @@ 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_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):
@@ -118,18 +119,18 @@ def detect_and_align_faces(image):
118
  return aligned_faces
119
 
120
  # Add person to database
121
- def add_person(name, image_paths, instagram_handle, email=None):
122
  try:
123
- encodings = []
124
- for image_path in image_paths:
125
- encoding = load_and_encode(image_path)
126
- if not encoding:
127
- return f"No face found in the image: {image_path}"
128
- encodings.append(encoding[0].tolist())
129
 
130
  # Save data to Firebase Realtime Database
131
  person_data = {
132
- "encodings": encodings,
133
  "info": {
134
  "instagram_handle": instagram_handle,
135
  "instagram_link": f"https://www.instagram.com/{instagram_handle}/"
@@ -146,22 +147,24 @@ def add_person(name, image_paths, instagram_handle, email=None):
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)
@@ -245,27 +248,23 @@ def send_feedback(feedback_data):
245
  def add_person_ui():
246
  st.title("😎 Add Person")
247
  name = st.text_input("Enter Name", help="Enter the name of the person")
248
- image_paths = st.file_uploader("Upload Image(s)", accept_multiple_files=True, help="Upload 1 to 3 images containing the person's face")
249
  email = st.text_input("Enter Email (Optional)", help="Enter the person's email address (optional)")
250
  instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
251
  if st.button("Add Person"):
252
- if not name or not image_paths or not instagram_handle:
253
  st.error("Please fill all the required fields.")
254
  else:
255
- result = add_person(name, image_paths, instagram_handle, email)
256
  st.success(result)
257
 
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)")
@@ -540,5 +539,4 @@ def main():
540
 
541
  # Run the tour guide
542
  if __name__ == "__main__":
543
- authenticate_user_ui()
544
-
 
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):
 
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}/"
 
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)
 
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
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)")
 
539
 
540
  # Run the tour guide
541
  if __name__ == "__main__":
542
+ authenticate_user_ui()