Update app.py
Browse files
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,
|
| 123 |
try:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
# Save data to Firebase Realtime Database
|
| 132 |
person_data = {
|
| 133 |
-
"
|
| 134 |
"info": {
|
| 135 |
"instagram_handle": instagram_handle,
|
| 136 |
"instagram_link": f"https://www.instagram.com/{instagram_handle}/"
|
|
@@ -146,7 +146,6 @@ def add_person(name, image_paths, 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 |
# Update recognize_face function to handle multiple face encodings
|
| 151 |
def recognize_face(image_path):
|
| 152 |
if not image_path:
|
|
@@ -161,11 +160,11 @@ def recognize_face(image_path):
|
|
| 161 |
for unknown_encoding in unknown_encodings:
|
| 162 |
face_matches = []
|
| 163 |
for name, data in ref.get().items():
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
|
| 170 |
if face_matches:
|
| 171 |
matches.extend(face_matches)
|
|
@@ -249,16 +248,14 @@ def send_feedback(feedback_data):
|
|
| 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 |
-
|
| 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
|
| 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,
|
| 262 |
st.success(result)
|
| 263 |
|
| 264 |
# Streamlit interface for recognizing face
|
|
@@ -542,5 +539,4 @@ def main():
|
|
| 542 |
|
| 543 |
# Run the tour guide
|
| 544 |
if __name__ == "__main__":
|
| 545 |
-
authenticate_user_ui()
|
| 546 |
-
|
|
|
|
| 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 |
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 |
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
|
|
|
|
| 539 |
|
| 540 |
# Run the tour guide
|
| 541 |
if __name__ == "__main__":
|
| 542 |
+
authenticate_user_ui()
|
|
|