Update app.py
Browse files
app.py
CHANGED
|
@@ -107,7 +107,7 @@ def detect_and_align_faces(image_path):
|
|
| 107 |
return aligned_face
|
| 108 |
|
| 109 |
# Add person to database
|
| 110 |
-
def add_person(name, image_path, instagram_handle):
|
| 111 |
try:
|
| 112 |
encoding = load_and_encode(image_path)
|
| 113 |
if not encoding:
|
|
@@ -117,13 +117,17 @@ def add_person(name, image_path, instagram_handle):
|
|
| 117 |
encoding = encoding[0].tolist()
|
| 118 |
|
| 119 |
# Save data to Firebase Realtime Database
|
| 120 |
-
|
| 121 |
"encoding": encoding,
|
| 122 |
"info": {
|
| 123 |
"instagram_handle": instagram_handle,
|
| 124 |
"instagram_link": f"https://www.instagram.com/{instagram_handle}/"
|
| 125 |
}
|
| 126 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return f"Success: {name} added to the database!"
|
| 129 |
except Exception as e:
|
|
@@ -143,15 +147,16 @@ def recognize_face(image_path):
|
|
| 143 |
for name, data in ref.get().items():
|
| 144 |
known_encoding = np.array(data["encoding"])
|
| 145 |
if face_recognition.compare_faces([known_encoding], unknown_encoding[0])[0]:
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
|
| 148 |
if matches:
|
| 149 |
results = []
|
| 150 |
-
for name,
|
| 151 |
-
|
| 152 |
-
insta_link = info["instagram_link"]
|
| 153 |
insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
|
| 154 |
-
results.append(f"- It's a picture of {name}! Insta handle: {insta_link_html}")
|
| 155 |
return "\n".join(results)
|
| 156 |
else:
|
| 157 |
return "Face not found in the database."
|
|
@@ -209,12 +214,13 @@ def add_person_ui():
|
|
| 209 |
st.title("Add Person")
|
| 210 |
name = st.text_input("Enter Name", help="Enter the name of the person")
|
| 211 |
image_path = st.file_uploader("Upload Image", help="Upload an image containing the person's face")
|
|
|
|
| 212 |
instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
|
| 213 |
if st.button("Add Person"):
|
| 214 |
if not name or not image_path or not instagram_handle:
|
| 215 |
-
st.error("Please fill all the fields.")
|
| 216 |
else:
|
| 217 |
-
result = add_person(name, image_path, instagram_handle)
|
| 218 |
st.success(result)
|
| 219 |
|
| 220 |
# Streamlit interface for recognizing face
|
|
@@ -225,6 +231,13 @@ def recognize_face_ui():
|
|
| 225 |
result = recognize_face(image_path)
|
| 226 |
st.write(result, unsafe_allow_html=True)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
# Streamlit interface for recognizing face with optimal ID
|
| 229 |
def recognize_face_optimal_ui():
|
| 230 |
st.title("Recognize Face (Optimal)")
|
|
@@ -234,7 +247,12 @@ def recognize_face_optimal_ui():
|
|
| 234 |
if "not found" in result.lower(): # Check if "not found" is in the result message
|
| 235 |
st.error(result)
|
| 236 |
else:
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# Streamlit interface for deleting a person
|
| 240 |
def delete_person_ui():
|
|
|
|
| 107 |
return aligned_face
|
| 108 |
|
| 109 |
# Add person to database
|
| 110 |
+
def add_person(name, image_path, instagram_handle, email=None):
|
| 111 |
try:
|
| 112 |
encoding = load_and_encode(image_path)
|
| 113 |
if not encoding:
|
|
|
|
| 117 |
encoding = encoding[0].tolist()
|
| 118 |
|
| 119 |
# Save data to Firebase Realtime Database
|
| 120 |
+
person_data = {
|
| 121 |
"encoding": encoding,
|
| 122 |
"info": {
|
| 123 |
"instagram_handle": instagram_handle,
|
| 124 |
"instagram_link": f"https://www.instagram.com/{instagram_handle}/"
|
| 125 |
}
|
| 126 |
+
}
|
| 127 |
+
if email:
|
| 128 |
+
person_data["info"]["email"] = email
|
| 129 |
+
|
| 130 |
+
ref.child(name).set(person_data)
|
| 131 |
|
| 132 |
return f"Success: {name} added to the database!"
|
| 133 |
except Exception as e:
|
|
|
|
| 147 |
for name, data in ref.get().items():
|
| 148 |
known_encoding = np.array(data["encoding"])
|
| 149 |
if face_recognition.compare_faces([known_encoding], unknown_encoding[0])[0]:
|
| 150 |
+
info = data["info"]
|
| 151 |
+
email = info.get("email", "Email not provided")
|
| 152 |
+
matches.append((name, info["instagram_handle"], email))
|
| 153 |
|
| 154 |
if matches:
|
| 155 |
results = []
|
| 156 |
+
for name, insta_handle, email in matches:
|
| 157 |
+
insta_link = f"https://www.instagram.com/{insta_handle}/"
|
|
|
|
| 158 |
insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
|
| 159 |
+
results.append(f"- It's a picture of {name}! Insta handle: {insta_link_html}, Email: {email}")
|
| 160 |
return "\n".join(results)
|
| 161 |
else:
|
| 162 |
return "Face not found in the database."
|
|
|
|
| 214 |
st.title("Add Person")
|
| 215 |
name = st.text_input("Enter Name", help="Enter the name of the person")
|
| 216 |
image_path = st.file_uploader("Upload Image", help="Upload an image containing the person's face")
|
| 217 |
+
email = st.text_input("Enter Email (Optional)", help="Enter the person's email address (optional)")
|
| 218 |
instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
|
| 219 |
if st.button("Add Person"):
|
| 220 |
if not name or not image_path or not instagram_handle:
|
| 221 |
+
st.error("Please fill all the required fields.")
|
| 222 |
else:
|
| 223 |
+
result = add_person(name, image_path, instagram_handle, email)
|
| 224 |
st.success(result)
|
| 225 |
|
| 226 |
# Streamlit interface for recognizing face
|
|
|
|
| 231 |
result = recognize_face(image_path)
|
| 232 |
st.write(result, unsafe_allow_html=True)
|
| 233 |
|
| 234 |
+
if "It's a picture of" in result:
|
| 235 |
+
# Extract email from the result
|
| 236 |
+
email_start = result.find("Insta handle: ") + len("Insta handle: ")
|
| 237 |
+
email_end = result.find("</font></a>", email_start)
|
| 238 |
+
email = result[email_start:email_end]
|
| 239 |
+
st.write(f"Email: {email}")
|
| 240 |
+
|
| 241 |
# Streamlit interface for recognizing face with optimal ID
|
| 242 |
def recognize_face_optimal_ui():
|
| 243 |
st.title("Recognize Face (Optimal)")
|
|
|
|
| 247 |
if "not found" in result.lower(): # Check if "not found" is in the result message
|
| 248 |
st.error(result)
|
| 249 |
else:
|
| 250 |
+
if "email" in result:
|
| 251 |
+
name, similarity_score, email = result.split(",")
|
| 252 |
+
st.write(f"Best match: {name} with a similarity score of {1 - float(similarity_score):.2%}. Email: {email}")
|
| 253 |
+
else:
|
| 254 |
+
name, similarity_score = result.split(",")
|
| 255 |
+
st.write(f"Best match: {name} with a similarity score of {1 - float(similarity_score):.2%}.")
|
| 256 |
|
| 257 |
# Streamlit interface for deleting a person
|
| 258 |
def delete_person_ui():
|