Update app.py
Browse files
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(
|
| 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 |
|
| 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,
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
# Save data to Firebase Realtime Database
|
| 131 |
person_data = {
|
| 132 |
-
"
|
| 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(
|
| 150 |
-
if not
|
| 151 |
-
return "Please upload
|
| 152 |
|
| 153 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
matches = []
|
| 155 |
-
for
|
| 156 |
face_matches = []
|
| 157 |
for name, data in ref.get().items():
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 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 |
-
|
| 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
|
| 253 |
st.error("Please fill all the required fields.")
|
| 254 |
else:
|
| 255 |
-
result = add_person(name,
|
| 256 |
st.success(result)
|
| 257 |
|
| 258 |
# Streamlit interface for recognizing face
|
| 259 |
def recognize_face_ui():
|
| 260 |
st.title("π Recognize Face")
|
| 261 |
-
|
| 262 |
if st.button("Recognize Face"):
|
| 263 |
-
|
| 264 |
-
|
| 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()
|
|
|