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
 
 
 
 
 
 
1
+ #with all functioning and logo alignment
2
+ import os
3
+ import streamlit as st
4
+ import firebase_admin
5
+ from firebase_admin import credentials, db, auth, firestore
6
+ import face_recognition
7
+ from PIL import Image
8
+ import numpy as np
9
+ import cv2
10
+ import dlib
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 use the aligned face without normalization
66
+ def load_and_encode(image_path):
67
+ try:
68
+ aligned_face = detect_and_align_faces(image_path)
69
+
70
+ if aligned_face is not None:
71
+ encoding = face_recognition.face_encodings(aligned_face)
72
+
73
+ if encoding:
74
+ return encoding
75
+ else:
76
+ return None
77
+ else:
78
+ return None
79
+ except Exception as e:
80
+ print(f"Error loading and encoding image: {str(e)}")
81
+ return None
82
+
83
+ # Function to detect and align faces in an image with preprocessing
84
+ def detect_and_align_faces(image_path):
85
+ image = face_recognition.load_image_file(image_path)
86
+
87
+ # Resize the image to a fixed width (you can adjust the width as needed)
88
+ target_width = 800
89
+ aspect_ratio = image.shape[1] / image.shape[0]
90
+ target_height = int(target_width / aspect_ratio)
91
+ resized_image = cv2.resize(image, (target_width, target_height))
92
+
93
+ gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
94
+
95
+ # Detect faces using dlib
96
+ faces = detector(gray)
97
+
98
+ if not faces:
99
+ return None
100
+
101
+ # Use the first face found (you can modify this to handle multiple faces)
102
+ face = faces[0]
103
+
104
+ # Use dlib for face alignment
105
+ landmarks = shape_predictor(gray, face)
106
+ aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256) # Adjust the size as needed
107
+
108
+ return aligned_face
109
+
110
+ # Add person to database
111
+ def add_person(name, image_path, instagram_handle):
112
+ try:
113
+ encoding = load_and_encode(image_path)
114
+ if not encoding:
115
+ return "No face found in the provided image."
116
+
117
+ # Convert NumPy arrays to lists for JSON serialization
118
+ encoding = encoding[0].tolist()
119
+
120
+ # Save data to Firebase Realtime Database
121
+ ref.child(name).set({
122
+ "encoding": encoding,
123
+ "info": {
124
+ "instagram_handle": instagram_handle,
125
+ "instagram_link": f"https://www.instagram.com/{instagram_handle}/"
126
+ }
127
+ })
128
+
129
+ return f"Success: {name} added to the database!"
130
+ except Exception as e:
131
+ return f"Failed to add person: {str(e)}"
132
+
133
+ # Recognize face from image
134
+ def recognize_face(image_path):
135
+ if not image_path:
136
+ return "Please upload an image."
137
+
138
+ try:
139
+ unknown_encoding = load_and_encode(image_path)
140
+ if not unknown_encoding:
141
+ return "No face found in the provided image."
142
+
143
+ matches = []
144
+ for name, data in ref.get().items():
145
+ known_encoding = np.array(data["encoding"])
146
+ if face_recognition.compare_faces([known_encoding], unknown_encoding[0])[0]:
147
+ matches.append((name, data["info"]))
148
+
149
+ if matches:
150
+ results = []
151
+ for name, info in matches:
152
+ insta_handle = info["instagram_handle"]
153
+ insta_link = info["instagram_link"]
154
+ insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
155
+ results.append(f"- It's a picture of {name}! Insta handle: {insta_link_html}")
156
+ return "\n".join(results)
157
+ else:
158
+ return "Face not found in the database."
159
+ except Exception as e:
160
+ return f"Failed to recognize face: {str(e)}"
161
+
162
+ # Recognize face from image and return optimal or highest matching ID
163
+ def recognize_face_optimal(image_path):
164
+ if not image_path:
165
+ return "Please upload an image."
166
+
167
+ try:
168
+ unknown_encoding = load_and_encode(image_path)
169
+ if not unknown_encoding:
170
+ return "No face found in the provided image."
171
+
172
+ matches = []
173
+ for name, data in ref.get().items():
174
+ known_encoding = np.array(data["encoding"])
175
+ similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding[0])[0]
176
+ if similarity_score > 0.50: # Only consider matches above 50.00% similarity
177
+ continue
178
+ matches.append((name, similarity_score))
179
+
180
+ if matches:
181
+ best_match = min(matches, key=lambda x: x[1])
182
+ best_name, best_score = best_match
183
+ info = ref.child(best_name).child("info").get()
184
+ insta_handle = info["instagram_handle"]
185
+ insta_link = info["instagram_link"]
186
+ insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
187
+ return f"Best match: {best_name} with a similarity score of {1 - best_score:.2%}. Insta handle: {insta_link_html}"
188
+ else:
189
+ return "Face not found in the database."
190
+ except Exception as e:
191
+ return f"Failed to recognize face: {str(e)}"
192
+
193
+ # Delete person from database
194
+ def delete_person(name):
195
+ try:
196
+ ref.child(name).delete()
197
+ return f"{name} deleted from the database!"
198
+ except Exception as e:
199
+ return f"Failed to delete person: {str(e)}"
200
+
201
+ # Send feedback to Firebase
202
+ def send_feedback(feedback_data):
203
+ try:
204
+ db_firestore.collection('feedback').add(feedback_data)
205
+ except Exception as e:
206
+ st.error(f"Failed to submit feedback: {str(e)}")
207
+
208
+ # Streamlit interface for adding a person
209
+ def add_person_ui():
210
+ st.title("Add Person")
211
+ name = st.text_input("Enter Name", help="Enter the name of the person")
212
+ image_path = st.file_uploader("Upload Image", help="Upload an image containing the person's face")
213
+ instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
214
+ if st.button("Add Person"):
215
+ if not name or not image_path or not instagram_handle:
216
+ st.error("Please fill all the fields.")
217
+ else:
218
+ result = add_person(name, image_path, instagram_handle)
219
+ st.success(result)
220
+
221
+ # Streamlit interface for recognizing face
222
+ def recognize_face_ui():
223
+ st.title("Recognize Face")
224
+ image_path = st.file_uploader("Upload Image", help="Upload an image for face recognition")
225
+ if st.button("Recognize Face"):
226
+ result = recognize_face(image_path)
227
+ st.write(result, unsafe_allow_html=True)
228
+
229
+ # Streamlit interface for recognizing face with optimal ID
230
+ def recognize_face_optimal_ui():
231
+ st.title("Recognize Face (Optimal)")
232
+ image_path = st.file_uploader("Upload Image", help="Upload an image for optimal face recognition")
233
+ if st.button("Recognize Face (Optimal)"):
234
+ result = recognize_face_optimal(image_path)
235
+ if "not found" in result.lower(): # Check if "not found" is in the result message
236
+ st.error(result)
237
+ else:
238
+ st.write(result, unsafe_allow_html=True)
239
+
240
+ # Streamlit interface for deleting a person
241
+ def delete_person_ui():
242
+ st.title("Delete Person")
243
+ name = st.text_input("Enter Name", help="Enter the name of the person to delete")
244
+ if st.button("Delete Person"):
245
+ if not name:
246
+ st.error("Please enter a name.")
247
+ else:
248
+ result = delete_person(name)
249
+ st.success(result)
250
+
251
+ # Streamlit interface for feedback
252
+ def feedback_ui():
253
+ st.title("Feedback")
254
+ st.write("Your feedback is important to us! Please fill out the form below:")
255
+
256
+ name = st.text_input("Name (optional)")
257
+ email = st.text_input("Email (optional)")
258
+ category = st.selectbox("Category", ["Bug Report", "Feature Request", "General Feedback"])
259
+ message = st.text_area("Feedback Message")
260
+
261
+ if st.button("Submit Feedback"):
262
+ if not message:
263
+ st.error("Please enter your feedback message.")
264
+ else:
265
+ feedback_data = {
266
+ "name": name,
267
+ "email": email,
268
+ "category": category,
269
+ "message": message,
270
+ }
271
+ send_feedback(feedback_data)
272
+ st.success("Feedback submitted successfully! Thank you for your feedback.")
273
+
274
+ #section for messaging
275
+
276
+ # Send a message to the receiver_email from the sender_email
277
+ def send_message(sender_email, receiver_email, message_content):
278
+ try:
279
+ # Add a new document to the 'messages' collection
280
+ db_firestore.collection('messages').add({
281
+ 'sender_email': sender_email,
282
+ 'receiver_email': receiver_email,
283
+ 'message_content': message_content,
284
+ 'timestamp': firestore.SERVER_TIMESTAMP
285
+ })
286
+ return "Message sent successfully!"
287
+ except Exception as e:
288
+ return f"Failed to send message: {str(e)}"
289
+
290
+ # Function to retrieve messages for a user
291
+ def get_messages(user_email):
292
+ try:
293
+ messages = db_firestore.collection('messages').where('receiver_email', '==', user_email).order_by('timestamp', direction=firestore.Query.DESCENDING).stream()
294
+ return messages # Return the Firestore query snapshot
295
+ except Exception as e:
296
+ st.error(f"Failed to retrieve messages: {str(e)}")
297
+ return None
298
+
299
+ # Streamlit interface for sending messages
300
+ def send_message_ui():
301
+ st.title("Send Message")
302
+ sender_email = st.session_state.auth_state["user"].email
303
+ receiver_email = st.text_input("Receiver's Email", help="Enter the receiver's email address")
304
+ message_content = st.text_area("Message Content")
305
+ if st.button("Send Message"):
306
+ result = send_message(sender_email, receiver_email, message_content)
307
+ st.write(result)
308
+
309
+ # Streamlit interface for viewing messages
310
+ def view_messages_ui():
311
+ st.title("Messages")
312
+ user_email = st.session_state.auth_state["user"].email
313
+ messages = get_messages(user_email)
314
+ if messages is not None:
315
+ for message in messages:
316
+ message_dict = message.to_dict() # Convert Firestore document to dictionary
317
+ st.write(f"From: {message_dict['sender_email']}")
318
+ st.write(f"Message: {message_dict['message_content']}")
319
+ st.write("---")
320
+ #end of messaging section
321
+
322
+ def tour_guide_ui():
323
+ st.title("Tour Guide")
324
+ st.markdown("This tour will guide you through the application.")
325
+
326
+ with st.expander("Welcome"):
327
+ st.write("This is a tour guide to help you navigate through the application.")
328
+
329
+ with st.expander("Options Sidebar"):
330
+ 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.")
331
+
332
+ with st.expander("Main Interface"):
333
+ st.write("This is where the main functionality of the application is displayed.")
334
+
335
+ with st.expander("Upload Image"):
336
+ st.write("You can upload an image here for face recognition or adding a person.")
337
+
338
+ with st.expander("Text Input"):
339
+ st.write("Enter text here such as the person's name or Instagram handle.")
340
+
341
+ with st.expander("Buttons"):
342
+ st.write("Click on these buttons to perform actions like adding a person or recognizing a face.")
343
+
344
+ # Streamlit interface for user authentication
345
+ def authenticate_user_ui():
346
+
347
+ c30, c31, c32 = st.columns([0.2, 0.1, 3])
348
+ with c30:
349
+ st.caption("")
350
+ # Display the logo
351
+ logo_path = os.path.join(current_directory, "Explore+.png")
352
+ logo = Image.open(logo_path)
353
+ st.image(logo, width=60)
354
+
355
+ with c32:
356
+ st.title("Insta's EYE")
357
+ st.sidebar.title("Options")
358
+
359
+ if st.session_state.auth_state["signed_in"]:
360
+ st.sidebar.button("Sign Out", on_click=logout)
361
+ st.title("Welcome!")
362
+ main()
363
+ else:
364
+ option = st.sidebar.radio("Select Option", ["Login", "Sign-Up"])
365
+
366
+ email = st.text_input("Enter Email", help="Enter your email address")
367
+ password = st.text_input("Enter Password", type="password", help="Enter your password")
368
+
369
+ if option == "Login":
370
+ if st.button("Login"):
371
+ if not email or not password:
372
+ st.error("Please enter both email and password.")
373
+ else:
374
+ success, user = authenticate_user(email, password)
375
+ if success:
376
+ st.session_state.auth_state["user"] = user
377
+ st.session_state.auth_state["signed_in"] = True
378
+ st.success("Authentication successful! You can now manage your set of images and profiles.")
379
+ main()
380
+ else:
381
+ st.error("Authentication failed. Please check your email and password.")
382
+
383
+ elif option == "Sign-Up":
384
+ confirm_password = st.text_input("Confirm Password", type="password", help="Re-enter your password for confirmation")
385
+ if st.button("Sign-Up"):
386
+ if not email or not password or not confirm_password:
387
+ st.error("Please fill all the fields.")
388
+ elif password != confirm_password:
389
+ st.error("Passwords do not match.")
390
+ else:
391
+ success, uid = create_user(email, password)
392
+ if success:
393
+ st.success(f"User with UID: {uid} created successfully! You can now log in.")
394
+ else:
395
+ st.error("User creation failed. Please try again.")
396
+
397
+ # Log out user
398
+ def logout():
399
+ st.session_state.auth_state["user"] = None
400
+ st.session_state.auth_state["signed_in"] = False
401
+
402
+ # Define tour steps
403
+ steps = [
404
+ {
405
+ "title": "Welcome to Insta's EYE",
406
+ "content": "This is a tour guide to help you navigate through the application.",
407
+ },
408
+ {
409
+ "title": "Options Sidebar",
410
+ "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.",
411
+ },
412
+ {
413
+ "title": "Main Interface",
414
+ "content": "This is where the main functionality of the application is displayed.",
415
+ },
416
+ {
417
+ "title": "Upload Image",
418
+ "content": "You can upload an image here for face recognition or adding a person.",
419
+ },
420
+ {
421
+ "title": "Text Input",
422
+ "content": "Enter text here such as the person's name or Instagram handle.",
423
+ },
424
+ {
425
+ "title": "Buttons",
426
+ "content": "Click on these buttons to perform actions like adding a person or recognizing a face.",
427
+ },
428
+ ]
429
+
430
+ # Function to display tour steps
431
+ def display_tour_steps(steps):
432
+ st.markdown("# Tour Guide")
433
+ st.markdown("This tour will guide you through the application.")
434
+ st.markdown("---")
435
+
436
+ for step in steps:
437
+ st.markdown(f"## {step['title']}")
438
+ st.write(step['content'])
439
+ st.markdown("---")
440
+
441
+ # Update the main function to include the feedback option
442
+ def main():
443
+ st.sidebar.title("Options")
444
+ option = st.sidebar.radio("Select Option", ["Add Person", "Recognize Face", "Delete Person", "Recognize Face (Optimal)", "Tour Guide", "Feedback", "Send Message","Messages"])
445
+
446
+ if option == "Add Person":
447
+ add_person_ui()
448
+ elif option == "Recognize Face":
449
+ recognize_face_ui()
450
+ elif option == "Delete Person":
451
+ delete_person_ui()
452
+ elif option == "Recognize Face (Optimal)":
453
+ recognize_face_optimal_ui()
454
+ elif option == "Tour Guide":
455
+ tour_guide_ui()
456
+ elif option == "Feedback":
457
+ feedback_ui()
458
+ elif option == "Send Message":
459
+ send_message_ui()
460
+ elif option == "Messages":
461
+ view_messages_ui()
462
+
463
+ # Run the tour guide
464
+ if __name__ == "__main__":
465
+ authenticate_user_ui()