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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import firebase_admin
4
+ from firebase_admin import credentials, db, auth, firestore
5
+ import face_recognition
6
+ from PIL import Image
7
+ import numpy as np
8
+ import cv2
9
+ import dlib
10
+ from io import BytesIO
11
+
12
+ # Get the current working directory
13
+ current_directory = os.path.dirname(os.path.abspath(__file__))
14
+ skp_path = os.path.join(current_directory, "projectinsta-s-firebase-adminsdk-y6vlu-9a1345f468.json")
15
+
16
+ # Check if the app is already initialized
17
+ if not firebase_admin._apps:
18
+ # Initialize Firebase Admin SDK
19
+ cred = credentials.Certificate(skp_path)
20
+ firebase_admin.initialize_app(cred, {
21
+ 'databaseURL': 'https://projectinsta-s-default-rtdb.firebaseio.com/',
22
+ 'projectId': 'projectinsta-s'
23
+ })
24
+
25
+ # Reference to the root of your Firebase Realtime Database
26
+ ref = db.reference('/')
27
+
28
+ # Initialize Firestore client
29
+ db_firestore = firestore.client()
30
+
31
+ # Streamlit session state
32
+ if "auth_state" not in st.session_state:
33
+ st.session_state.auth_state = {
34
+ "user": None,
35
+ "signed_in": False,
36
+ }
37
+
38
+ # Add the shape predictor model for face alignment
39
+ shape_predictor_path = "shape_predictor_68_face_landmarks.dat"
40
+ detector = dlib.get_frontal_face_detector()
41
+ shape_predictor = dlib.shape_predictor(shape_predictor_path)
42
+
43
+ # Firebase Authentication
44
+ def authenticate_user(email, password):
45
+ try:
46
+ user = auth.get_user_by_email(email)
47
+ # The user is successfully fetched, meaning the email and password are valid.
48
+ return True, user
49
+ except auth.AuthError as e:
50
+ print(f"Authentication error: {str(e)}")
51
+ return False, None
52
+
53
+ # Sign-up Functionality
54
+ def create_user(email, password):
55
+ try:
56
+ user = auth.create_user(
57
+ email=email,
58
+ password=password
59
+ )
60
+ return True, user.uid
61
+ except Exception as e:
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
+ 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):
98
+ # Resize the image to a fixed width (you can adjust the width as needed)
99
+ target_width = 800
100
+ aspect_ratio = image.shape[1] / image.shape[0]
101
+ target_height = int(target_width / aspect_ratio)
102
+ resized_image = cv2.resize(image, (target_width, target_height))
103
+
104
+ gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
105
+
106
+ # Detect faces using dlib
107
+ faces = detector(gray)
108
+
109
+ if not faces:
110
+ return None
111
+
112
+ aligned_faces = []
113
+ for face in faces:
114
+ # Use dlib for face alignment
115
+ landmarks = shape_predictor(gray, face)
116
+ aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256) # Adjust the size as needed
117
+ aligned_faces.append(aligned_face)
118
+
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}/"
137
+ }
138
+ }
139
+ if email:
140
+ person_data["info"]["email"] = email
141
+
142
+ ref.child(name).set(person_data)
143
+
144
+ log_action(st.session_state.auth_state["user"].email, "Added person")
145
+ return f"Success: {name} added to the database!"
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:
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)
171
+ else:
172
+ matches.append(("Unknown", "Unknown", "Unknown"))
173
+
174
+ if matches:
175
+ results = []
176
+ for name, insta_handle, email in matches:
177
+ insta_link = f"https://www.instagram.com/{insta_handle}/"
178
+ insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
179
+ results.append(f"- It's a picture of {name}! Insta handle: {insta_link_html}, Email: {email}")
180
+ log_action(st.session_state.auth_state["user"].email, "Recognized face")
181
+ return "\n".join(results)
182
+ else:
183
+ return "Face not found in the database."
184
+ except Exception as e:
185
+ return f"Failed to recognize face: {str(e)}"
186
+
187
+ # Update recognize_face_optimal function to handle multiple face encodings
188
+ def recognize_face_optimal(image_path):
189
+ if not image_path:
190
+ return "Please upload an image."
191
+
192
+ try:
193
+ unknown_encodings = load_and_encode(image_path)
194
+ if not unknown_encodings:
195
+ return "No face found in the provided image."
196
+
197
+ matches = []
198
+ for unknown_encoding in unknown_encodings:
199
+ face_matches = []
200
+ for name, data in ref.get().items():
201
+ known_encoding = np.array(data["encoding"])
202
+ similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding)[0]
203
+ if similarity_score > 0.50: # Only consider matches above 50.00% similarity
204
+ continue
205
+ face_matches.append((name, similarity_score))
206
+
207
+ if face_matches:
208
+ best_match = min(face_matches, key=lambda x: x[1])
209
+ best_name, best_score = best_match
210
+ info = ref.child(best_name).child("info").get()
211
+ insta_handle = info["instagram_handle"]
212
+ insta_link = info["instagram_link"]
213
+ insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
214
+ matches.append(f"Best match: {best_name} with a similarity score of {1 - best_score:.2%}. Insta handle: {insta_link_html}")
215
+ else:
216
+ matches.append("Face not found in the database.")
217
+
218
+ return "\n".join(matches)
219
+ except Exception as e:
220
+ return f"Failed to recognize face: {str(e)}"
221
+
222
+ # Delete person from database
223
+ def delete_person(name):
224
+ try:
225
+ ref.child(name).delete()
226
+ log_action(st.session_state.auth_state["user"].email, "Deleted person")
227
+ return f"{name} deleted from the database!"
228
+ except Exception as e:
229
+ return f"Failed to delete person: {str(e)}"
230
+
231
+ # Delete user from Firebase Authentication
232
+ def delete_user(email):
233
+ try:
234
+ user = auth.get_user_by_email(email)
235
+ auth.delete_user(user.uid)
236
+ return "User deleted successfully!"
237
+ except Exception as e:
238
+ return f"Failed to delete user: {str(e)}"
239
+
240
+ # Send feedback to Firebase
241
+ def send_feedback(feedback_data):
242
+ try:
243
+ db_firestore.collection('feedback').add(feedback_data)
244
+ except Exception as e:
245
+ st.error(f"Failed to submit feedback: {str(e)}")
246
+
247
+ # Streamlit interface for adding a person
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)")
271
+ image_path = st.file_uploader("Upload Image", help="Upload an image for optimal face recognition")
272
+ if st.button("Recognize Face (Optimal)"):
273
+ result = recognize_face_optimal(image_path)
274
+ st.write(result, unsafe_allow_html=True)
275
+
276
+ # Streamlit interface for deleting a person
277
+ def delete_person_ui():
278
+ st.title("🗑️ Delete Person")
279
+ name = st.text_input("Enter Name", help="Enter the name of the person to delete")
280
+ if st.button("Delete Person"):
281
+ if not name:
282
+ st.error("Please enter a name.")
283
+ else:
284
+ result = delete_person(name)
285
+ st.success(result)
286
+
287
+ # Streamlit interface for feedback
288
+ def feedback_ui():
289
+ st.title("✍️ Feedback")
290
+ st.write("Your feedback is important to us! Please fill out the form below:")
291
+
292
+ name = st.text_input("Name (optional)")
293
+ email = st.text_input("Email (optional)")
294
+ category = st.selectbox("Category", ["Bug Report", "Feature Request", "General Feedback"])
295
+ message = st.text_area("Feedback Message")
296
+
297
+ if st.button("Submit Feedback"):
298
+ if not message:
299
+ st.error("Please enter your feedback message.")
300
+ else:
301
+ feedback_data = {
302
+ "name": name,
303
+ "email": email,
304
+ "category": category,
305
+ "message": message,
306
+ }
307
+ send_feedback(feedback_data)
308
+ st.success("Feedback submitted successfully! Thank you for your feedback.")
309
+
310
+ #section for messaging
311
+
312
+ # Function to send a message
313
+ def send_message(sender_email, receiver_email, message_content):
314
+ try:
315
+ # Add message to Firestore
316
+ db_firestore.collection('messages').add({
317
+ 'sender_email': sender_email,
318
+ 'receiver_email': receiver_email,
319
+ 'message_content': message_content,
320
+ 'timestamp': firestore.SERVER_TIMESTAMP
321
+ })
322
+ return "Message sent successfully!"
323
+ except Exception as e:
324
+ return f"Failed to send message: {str(e)}"
325
+
326
+ # Function to retrieve messages for a user
327
+ def get_messages(user_email):
328
+ try:
329
+ messages = db_firestore.collection('messages').where('receiver_email', '==', user_email).order_by('timestamp', direction=firestore.Query.DESCENDING).stream()
330
+ return messages
331
+ except Exception as e:
332
+ return None
333
+
334
+ # Streamlit interface for messaging
335
+ def messaging_ui():
336
+ st.title("💬 Messaging")
337
+
338
+ if st.session_state.auth_state["signed_in"]:
339
+ sender_email = st.session_state.auth_state["user"].email
340
+ receiver_email = st.text_input("Receiver's Email", help="Enter the receiver's email address")
341
+ message_content = st.text_area("Message Content")
342
+
343
+ if st.button("Send Message"):
344
+ result = send_message(sender_email, receiver_email, message_content)
345
+ st.write(result)
346
+
347
+ messages = get_messages(sender_email)
348
+ if messages:
349
+ st.write("Your messages:")
350
+ for message in messages:
351
+ message_data = message.to_dict()
352
+ st.write(f"From: {message_data['sender_email']}")
353
+ st.write(f"Message: {message_data['message_content']}")
354
+ st.write("---")
355
+ else:
356
+ st.write("Please sign in to send and view messages.")
357
+
358
+ #end of messaging section
359
+
360
+ # History section
361
+ def log_action(user_email, action):
362
+ try:
363
+ db_firestore.collection('history').add({
364
+ 'user_email': user_email,
365
+ 'action': action,
366
+ 'timestamp': firestore.SERVER_TIMESTAMP
367
+ })
368
+ except Exception as e:
369
+ st.error(f"Failed to log action: {str(e)}")
370
+
371
+ # Display history of actions taken by the user
372
+ def display_history(user_email):
373
+ st.title("📜 History")
374
+ st.write("Here is the history of actions taken by you:")
375
+
376
+ try:
377
+ history = db_firestore.collection('history').where('user_email', '==', user_email).order_by('timestamp', direction=firestore.Query.DESCENDING).stream()
378
+ for entry in history:
379
+ entry_data = entry.to_dict()
380
+ action = entry_data['action']
381
+ timestamp = entry_data['timestamp']
382
+ st.write(f"- {action} at {timestamp}")
383
+ except Exception as e:
384
+ st.error(f"Failed to retrieve history: {str(e)}")
385
+ #End of history section
386
+
387
+ # Streamlit interface for Deleting user
388
+ def delete_user_ui():
389
+ st.title("Delete User")
390
+ email = st.text_input("Enter User's Email", help="Enter the email of the user to delete")
391
+ if st.button("Delete User"):
392
+ if not email:
393
+ st.error("Please enter a user's email.")
394
+ else:
395
+ result = delete_user(email)
396
+ st.success(result)
397
+
398
+ def tour_guide_ui():
399
+ st.title("🗺️ Tour Guide")
400
+ st.markdown("This tour will guide you through the application.")
401
+
402
+ with st.expander("Welcome"):
403
+ st.write("This is a tour guide to help you navigate through the application.")
404
+
405
+ with st.expander("Options Sidebar"):
406
+ st.write("Here you can select different options such as adding a person, recognizing a face, deleting a person, or recognizing a face with optimal identification.")
407
+
408
+ with st.expander("Main Interface"):
409
+ st.write("This is where the main functionality of the application is displayed.")
410
+
411
+ with st.expander("Upload Image"):
412
+ st.write("You can upload an image here for face recognition or adding a person.")
413
+
414
+ with st.expander("Text Input"):
415
+ st.write("Enter text here such as the person's name or Instagram handle.")
416
+
417
+ with st.expander("Buttons"):
418
+ st.write("Click on these buttons to perform actions like adding a person or recognizing a face.")
419
+
420
+ def authenticate_user_ui():
421
+ # Display logo and title
422
+ c30, c31, c32 = st.columns([0.2, 0.1, 3])
423
+ with c30:
424
+ st.caption("")
425
+ # Display the logo
426
+ logo_path = os.path.join(current_directory, "Explore+.png")
427
+ logo = Image.open(logo_path)
428
+ st.image(logo, width=60)
429
+
430
+ with c32:
431
+ st.title("Insta's EYE")
432
+ st.sidebar.title("Options")
433
+
434
+ if st.session_state.auth_state["signed_in"]:
435
+ st.sidebar.button("Sign Out", on_click=logout)
436
+ st.title("Welcome!")
437
+ main()
438
+ else:
439
+ option = st.sidebar.radio("Select Option", ["Login", "Sign-Up"])
440
+
441
+ email = st.text_input("Enter Email", help="Enter your email address")
442
+ password = st.text_input("Enter Password", type="password", help="Enter your password")
443
+
444
+ if option == "Login":
445
+ if st.button("Login"):
446
+ if not email or not password:
447
+ st.error("Please enter both email and password.")
448
+ else:
449
+ success, user = authenticate_user(email, password)
450
+ if success:
451
+ st.session_state.auth_state["user"] = user
452
+ st.session_state.auth_state["signed_in"] = True
453
+ st.success("Authentication successful! You can now manage your set of images and profiles.")
454
+ main()
455
+ else:
456
+ st.error("Authentication failed. Please check your email and password.")
457
+
458
+ elif option == "Sign-Up":
459
+ confirm_password = st.text_input("Confirm Password", type="password", help="Re-enter your password for confirmation")
460
+ if st.button("Sign-Up"):
461
+ if not email or not password or not confirm_password:
462
+ st.error("Please fill all the fields.")
463
+ elif password != confirm_password:
464
+ st.error("Passwords do not match.")
465
+ else:
466
+ success, uid = create_user(email, password)
467
+ if success:
468
+ st.success(f"User with UID: {uid} created successfully! You can now log in.")
469
+ else:
470
+ st.error("User creation failed. Please try again.")
471
+
472
+ # Log out user
473
+ def logout():
474
+ st.session_state.auth_state["user"] = None
475
+ st.session_state.auth_state["signed_in"] = False
476
+
477
+ # Define tour steps
478
+ steps = [
479
+ {
480
+ "title": "Welcome to Insta's EYE",
481
+ "content": "This is a tour guide to help you navigate through the application.",
482
+ },
483
+ {
484
+ "title": "Options Sidebar",
485
+ "content": "Here you can select different options such as adding a person, recognizing a face, deleting a person, or recognizing a face with optimal identification.",
486
+ },
487
+ {
488
+ "title": "Main Interface",
489
+ "content": "This is where the main functionality of the application is displayed.",
490
+ },
491
+ {
492
+ "title": "Upload Image",
493
+ "content": "You can upload an image here for face recognition or adding a person.",
494
+ },
495
+ {
496
+ "title": "Text Input",
497
+ "content": "Enter text here such as the person's name or Instagram handle.",
498
+ },
499
+ {
500
+ "title": "Buttons",
501
+ "content": "Click on these buttons to perform actions like adding a person or recognizing a face.",
502
+ },
503
+ ]
504
+
505
+ # Function to display tour steps
506
+ def display_tour_steps(steps):
507
+ st.markdown("# Tour Guide")
508
+ st.markdown("This tour will guide you through the application.")
509
+ st.markdown("---")
510
+
511
+ for step in steps:
512
+ st.markdown(f"## {step['title']}")
513
+ st.write(step['content'])
514
+ st.markdown("---")
515
+
516
+ # Update the main function to include the feedback option
517
+ def main():
518
+ st.sidebar.title("Options")
519
+ option = st.sidebar.radio("Select Option", ["Add Person", "Recognize Face", "Delete Person", "Recognize Face (Optimal)", "Tour Guide", "Feedback", "Messaging", "History", "Delete User"])
520
+
521
+ if option == "Add Person":
522
+ add_person_ui()
523
+ elif option == "Recognize Face":
524
+ recognize_face_ui()
525
+ elif option == "Delete Person":
526
+ delete_person_ui()
527
+ elif option == "Recognize Face (Optimal)":
528
+ recognize_face_optimal_ui()
529
+ elif option == "Tour Guide":
530
+ tour_guide_ui()
531
+ elif option == "Feedback":
532
+ feedback_ui()
533
+ elif option == "Messaging":
534
+ messaging_ui()
535
+ elif option == "History":
536
+ display_history(st.session_state.auth_state["user"].email)
537
+ elif option == "Delete User":
538
+ delete_user_ui()
539
+
540
+ # Run the tour guide
541
+ if __name__ == "__main__":
542
+ authenticate_user_ui()