Update app.py
Browse files
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(
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 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(
|
| 151 |
-
if not
|
| 152 |
-
return "Please upload
|
| 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
|
| 161 |
face_matches = []
|
| 162 |
for name, data in ref.get().items():
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 265 |
if st.button("Recognize Face"):
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)")
|