Update app.py
Browse files
app.py
CHANGED
|
@@ -62,17 +62,17 @@ def create_user(email, password):
|
|
| 62 |
print(f"User creation error: {str(e)}")
|
| 63 |
return False, None
|
| 64 |
|
| 65 |
-
# Update load_and_encode function to
|
| 66 |
@st_cache
|
| 67 |
def load_and_encode(image_path):
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
|
| 71 |
-
if
|
| 72 |
-
|
| 73 |
|
| 74 |
-
if
|
| 75 |
-
return
|
| 76 |
else:
|
| 77 |
return None
|
| 78 |
else:
|
|
@@ -81,7 +81,7 @@ def load_and_encode(image_path):
|
|
| 81 |
print(f"Error loading and encoding image: {str(e)}")
|
| 82 |
return None
|
| 83 |
|
| 84 |
-
#
|
| 85 |
def detect_and_align_faces(image_path):
|
| 86 |
image = face_recognition.load_image_file(image_path)
|
| 87 |
|
|
@@ -99,14 +99,14 @@ def detect_and_align_faces(image_path):
|
|
| 99 |
if not faces:
|
| 100 |
return None
|
| 101 |
|
| 102 |
-
|
| 103 |
-
face
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
landmarks = shape_predictor(gray, face)
|
| 107 |
-
aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256) # Adjust the size as needed
|
| 108 |
-
|
| 109 |
-
return aligned_face
|
| 110 |
|
| 111 |
# Add person to database
|
| 112 |
@st_cache
|
|
@@ -137,24 +137,31 @@ def add_person(name, image_path, instagram_handle, email=None):
|
|
| 137 |
except Exception as e:
|
| 138 |
return f"Failed to add person: {str(e)}"
|
| 139 |
|
| 140 |
-
#
|
| 141 |
@st_cache
|
| 142 |
def recognize_face(image_path):
|
| 143 |
if not image_path:
|
| 144 |
return "Please upload an image."
|
| 145 |
|
| 146 |
try:
|
| 147 |
-
|
| 148 |
-
if not
|
| 149 |
return "No face found in the provided image."
|
| 150 |
|
| 151 |
matches = []
|
| 152 |
-
for
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
if matches:
|
| 160 |
results = []
|
|
@@ -169,38 +176,42 @@ def recognize_face(image_path):
|
|
| 169 |
except Exception as e:
|
| 170 |
return f"Failed to recognize face: {str(e)}"
|
| 171 |
|
| 172 |
-
#
|
| 173 |
@st_cache
|
| 174 |
def recognize_face_optimal(image_path):
|
| 175 |
if not image_path:
|
| 176 |
return "Please upload an image."
|
| 177 |
|
| 178 |
try:
|
| 179 |
-
|
| 180 |
-
if not
|
| 181 |
return "No face found in the provided image."
|
| 182 |
|
| 183 |
matches = []
|
| 184 |
-
for
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
-
best_match = min(matches, key=lambda x: x[1])
|
| 193 |
-
best_name, best_score = best_match
|
| 194 |
-
info = ref.child(best_name).child("info").get()
|
| 195 |
-
insta_handle = info["instagram_handle"]
|
| 196 |
-
insta_link = info["instagram_link"]
|
| 197 |
-
insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
|
| 198 |
-
return f"Best match: {best_name} with a similarity score of {1 - best_score:.2%}. Insta handle: {insta_link_html}"
|
| 199 |
-
else:
|
| 200 |
-
return "Face not found in the database."
|
| 201 |
except Exception as e:
|
| 202 |
return f"Failed to recognize face: {str(e)}"
|
| 203 |
-
|
| 204 |
# Delete person from database
|
| 205 |
@st_cache
|
| 206 |
def delete_person(name):
|
|
@@ -249,22 +260,12 @@ def recognize_face_ui():
|
|
| 249 |
result = recognize_face(image_path)
|
| 250 |
st.write(result, unsafe_allow_html=True)
|
| 251 |
|
| 252 |
-
if "It's a picture of" in result:
|
| 253 |
-
# Extract email from the result
|
| 254 |
-
email_start = result.find("Insta handle: ") + len("Insta handle: ")
|
| 255 |
-
email_end = result.find("</font></a>", email_start)
|
| 256 |
-
email = result[email_start:email_end]
|
| 257 |
-
st.write(f"Email: {email}")
|
| 258 |
-
|
| 259 |
def recognize_face_optimal_ui():
|
| 260 |
st.title("🔍 Recognize Face (Optimal)")
|
| 261 |
image_path = st.file_uploader("Upload Image", help="Upload an image for optimal face recognition")
|
| 262 |
if st.button("Recognize Face (Optimal)"):
|
| 263 |
result = recognize_face_optimal(image_path)
|
| 264 |
-
|
| 265 |
-
st.error(result)
|
| 266 |
-
else:
|
| 267 |
-
st.write(result, unsafe_allow_html=True)
|
| 268 |
|
| 269 |
# Streamlit interface for deleting a person
|
| 270 |
def delete_person_ui():
|
|
|
|
| 62 |
print(f"User creation error: {str(e)}")
|
| 63 |
return False, None
|
| 64 |
|
| 65 |
+
# Update load_and_encode function to return encodings for all detected faces
|
| 66 |
@st_cache
|
| 67 |
def load_and_encode(image_path):
|
| 68 |
try:
|
| 69 |
+
aligned_faces = detect_and_align_faces(image_path)
|
| 70 |
|
| 71 |
+
if aligned_faces:
|
| 72 |
+
encodings = face_recognition.face_encodings(aligned_faces)
|
| 73 |
|
| 74 |
+
if encodings:
|
| 75 |
+
return encodings
|
| 76 |
else:
|
| 77 |
return None
|
| 78 |
else:
|
|
|
|
| 81 |
print(f"Error loading and encoding image: {str(e)}")
|
| 82 |
return None
|
| 83 |
|
| 84 |
+
# Modify detect_and_align_faces function to detect and align multiple faces
|
| 85 |
def detect_and_align_faces(image_path):
|
| 86 |
image = face_recognition.load_image_file(image_path)
|
| 87 |
|
|
|
|
| 99 |
if not faces:
|
| 100 |
return None
|
| 101 |
|
| 102 |
+
aligned_faces = []
|
| 103 |
+
for face in faces:
|
| 104 |
+
# Use dlib for face alignment
|
| 105 |
+
landmarks = shape_predictor(gray, face)
|
| 106 |
+
aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256) # Adjust the size as needed
|
| 107 |
+
aligned_faces.append(aligned_face)
|
| 108 |
|
| 109 |
+
return aligned_faces
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Add person to database
|
| 112 |
@st_cache
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
return f"Failed to add person: {str(e)}"
|
| 139 |
|
| 140 |
+
# Update recognize_face function to handle multiple face encodings
|
| 141 |
@st_cache
|
| 142 |
def recognize_face(image_path):
|
| 143 |
if not image_path:
|
| 144 |
return "Please upload an image."
|
| 145 |
|
| 146 |
try:
|
| 147 |
+
unknown_encodings = load_and_encode(image_path)
|
| 148 |
+
if not unknown_encodings:
|
| 149 |
return "No face found in the provided image."
|
| 150 |
|
| 151 |
matches = []
|
| 152 |
+
for unknown_encoding in unknown_encodings:
|
| 153 |
+
face_matches = []
|
| 154 |
+
for name, data in ref.get().items():
|
| 155 |
+
known_encoding = np.array(data["encoding"])
|
| 156 |
+
if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
|
| 157 |
+
info = data["info"]
|
| 158 |
+
email = info.get("email", "Email not provided")
|
| 159 |
+
face_matches.append((name, info["instagram_handle"], email))
|
| 160 |
+
|
| 161 |
+
if face_matches:
|
| 162 |
+
matches.extend(face_matches)
|
| 163 |
+
else:
|
| 164 |
+
matches.append(("Unknown", "Unknown", "Unknown"))
|
| 165 |
|
| 166 |
if matches:
|
| 167 |
results = []
|
|
|
|
| 176 |
except Exception as e:
|
| 177 |
return f"Failed to recognize face: {str(e)}"
|
| 178 |
|
| 179 |
+
# Update recognize_face_optimal function to handle multiple face encodings
|
| 180 |
@st_cache
|
| 181 |
def recognize_face_optimal(image_path):
|
| 182 |
if not image_path:
|
| 183 |
return "Please upload an image."
|
| 184 |
|
| 185 |
try:
|
| 186 |
+
unknown_encodings = load_and_encode(image_path)
|
| 187 |
+
if not unknown_encodings:
|
| 188 |
return "No face found in the provided image."
|
| 189 |
|
| 190 |
matches = []
|
| 191 |
+
for unknown_encoding in unknown_encodings:
|
| 192 |
+
face_matches = []
|
| 193 |
+
for name, data in ref.get().items():
|
| 194 |
+
known_encoding = np.array(data["encoding"])
|
| 195 |
+
similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding)[0]
|
| 196 |
+
if similarity_score > 0.50: # Only consider matches above 50.00% similarity
|
| 197 |
+
continue
|
| 198 |
+
face_matches.append((name, similarity_score))
|
| 199 |
+
|
| 200 |
+
if face_matches:
|
| 201 |
+
best_match = min(face_matches, key=lambda x: x[1])
|
| 202 |
+
best_name, best_score = best_match
|
| 203 |
+
info = ref.child(best_name).child("info").get()
|
| 204 |
+
insta_handle = info["instagram_handle"]
|
| 205 |
+
insta_link = info["instagram_link"]
|
| 206 |
+
insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
|
| 207 |
+
matches.append(f"Best match: {best_name} with a similarity score of {1 - best_score:.2%}. Insta handle: {insta_link_html}")
|
| 208 |
+
else:
|
| 209 |
+
matches.append("Face not found in the database.")
|
| 210 |
|
| 211 |
+
return "\n".join(matches)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
except Exception as e:
|
| 213 |
return f"Failed to recognize face: {str(e)}"
|
| 214 |
+
|
| 215 |
# Delete person from database
|
| 216 |
@st_cache
|
| 217 |
def delete_person(name):
|
|
|
|
| 260 |
result = recognize_face(image_path)
|
| 261 |
st.write(result, unsafe_allow_html=True)
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
def recognize_face_optimal_ui():
|
| 264 |
st.title("🔍 Recognize Face (Optimal)")
|
| 265 |
image_path = st.file_uploader("Upload Image", help="Upload an image for optimal face recognition")
|
| 266 |
if st.button("Recognize Face (Optimal)"):
|
| 267 |
result = recognize_face_optimal(image_path)
|
| 268 |
+
st.write(result, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
# Streamlit interface for deleting a person
|
| 271 |
def delete_person_ui():
|