RajaThor commited on
Commit
6166b18
·
verified ·
1 Parent(s): 57c28f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -87
app.py CHANGED
@@ -8,20 +8,21 @@ import numpy as np
8
  import cv2
9
  import dlib
10
 
11
- # Firebase Realtime Database initialization
12
  current_directory = os.path.dirname(os.path.abspath(__file__))
13
  skp_path = os.path.join(current_directory, "projectinsta-s-firebase-adminsdk-y6vlu-9a1345f468.json")
14
 
 
15
  if not firebase_admin._apps:
 
16
  cred = credentials.Certificate(skp_path)
17
  firebase_admin.initialize_app(cred, {
18
  'databaseURL': 'https://projectinsta-s-default-rtdb.firebaseio.com/',
19
  'projectId': 'projectinsta-s'
20
  })
21
 
 
22
  ref = db.reference('/')
23
- encodings_ref = ref.child('encodings') # Node to store encodings
24
- messages_ref = ref.child('messages') # Node to store chat messages
25
 
26
  # Streamlit session state
27
  if "auth_state" not in st.session_state:
@@ -36,20 +37,27 @@ detector = dlib.get_frontal_face_detector()
36
  shape_predictor = dlib.shape_predictor(shape_predictor_path)
37
 
38
  # Firebase Authentication
39
- def authenticate_user(email, password):
40
  try:
41
- user = auth.get_user_by_email(email)
 
 
 
 
 
 
42
  return True, user
43
  except auth.AuthError as e:
44
  print(f"Authentication error: {str(e)}")
45
  return False, None
46
 
47
  # Sign-up Functionality
48
- def create_user(email, password):
49
  try:
50
  user = auth.create_user(
51
  email=email,
52
- password=password
 
53
  )
54
  return True, user.uid
55
  except Exception as e:
@@ -78,6 +86,7 @@ def load_and_encode(image_path):
78
  def detect_and_align_faces(image_path):
79
  image = face_recognition.load_image_file(image_path)
80
 
 
81
  target_width = 800
82
  aspect_ratio = image.shape[1] / image.shape[0]
83
  target_height = int(target_width / aspect_ratio)
@@ -85,15 +94,18 @@ def detect_and_align_faces(image_path):
85
 
86
  gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
87
 
 
88
  faces = detector(gray)
89
 
90
  if not faces:
91
  return None
92
 
 
93
  face = faces[0]
94
 
 
95
  landmarks = shape_predictor(gray, face)
96
- aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256)
97
 
98
  return aligned_face
99
 
@@ -104,9 +116,11 @@ def add_person(name, image_path, instagram_handle):
104
  if not encoding:
105
  return "No face found in the provided image."
106
 
 
107
  encoding = encoding[0].tolist()
108
 
109
- encodings_ref.child(name).set({
 
110
  "encoding": encoding,
111
  "info": {
112
  "instagram_handle": instagram_handle,
@@ -129,7 +143,7 @@ def recognize_face(image_path):
129
  return "No face found in the provided image."
130
 
131
  matches = []
132
- for name, data in encodings_ref.get().items():
133
  known_encoding = np.array(data["encoding"])
134
  if face_recognition.compare_faces([known_encoding], unknown_encoding[0])[0]:
135
  matches.append((name, data["info"]))
@@ -158,7 +172,7 @@ def recognize_face_optimal(image_path):
158
  return "No face found in the provided image."
159
 
160
  matches = []
161
- for name, data in encodings_ref.get().items():
162
  known_encoding = np.array(data["encoding"])
163
  similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding[0])[0]
164
  matches.append((name, similarity_score))
@@ -166,7 +180,7 @@ def recognize_face_optimal(image_path):
166
  if matches:
167
  best_match = min(matches, key=lambda x: x[1])
168
  best_name, best_score = best_match
169
- info = encodings_ref.child(best_name).child("info").get()
170
  insta_handle = info["instagram_handle"]
171
  insta_link = info["instagram_link"]
172
  insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
@@ -179,46 +193,11 @@ def recognize_face_optimal(image_path):
179
  # Delete person from database
180
  def delete_person(name):
181
  try:
182
- encodings_ref.child(name).delete()
183
  return f"{name} deleted from the database!"
184
  except Exception as e:
185
  return f"Failed to delete person: {str(e)}"
186
 
187
- # Function to send message to Firebase Realtime Database
188
- def send_message(sender_email, receiver_email, message):
189
- try:
190
- sender_ref = messages_ref.child(sender_email).child(receiver_email)
191
- receiver_ref = messages_ref.child(receiver_email).child(sender_email)
192
-
193
- sender_ref.push().set({
194
- 'sender_email': sender_email,
195
- 'message': message
196
- })
197
-
198
- receiver_ref.push().set({
199
- 'sender_email': sender_email,
200
- 'message': message
201
- })
202
-
203
- return True
204
- except Exception as e:
205
- print(f"Failed to send message: {str(e)}")
206
- return False
207
-
208
- # Function to get messages from Firebase Realtime Database
209
- def get_messages(sender_email, receiver_email):
210
- try:
211
- sender_ref = messages_ref.child(sender_email).child(receiver_email)
212
- receiver_ref = messages_ref.child(receiver_email).child(sender_email)
213
-
214
- sender_messages = sender_ref.get()
215
- receiver_messages = receiver_ref.get()
216
-
217
- return sender_messages, receiver_messages
218
- except Exception as e:
219
- print(f"Failed to get messages: {str(e)}")
220
- return None, None
221
-
222
  # Streamlit interface for adding a person
223
  def add_person_ui():
224
  st.title("Add Person")
@@ -247,7 +226,7 @@ def recognize_face_optimal_ui():
247
  if st.button("Recognize Face (Optimal)"):
248
  result = recognize_face_optimal(image_path)
249
  st.write(result, unsafe_allow_html=True)
250
-
251
  # Streamlit interface for deleting a person
252
  def delete_person_ui():
253
  st.title("Delete Person")
@@ -259,6 +238,10 @@ def delete_person_ui():
259
  result = delete_person(name)
260
  st.success(result)
261
 
 
 
 
 
262
  # Streamlit interface for user authentication
263
  def authenticate_user_ui():
264
  st.title("Insta's EYE")
@@ -271,13 +254,13 @@ def authenticate_user_ui():
271
  else:
272
  option = st.sidebar.radio("Select Option", ["Login", "Sign-Up"])
273
 
274
- email = st.text_input("Enter Email", help="Enter your email address")
275
- password = st.text_input("Enter Password", type="password", help="Enter your password")
276
-
277
  if option == "Login":
 
 
 
278
  if st.button("Login"):
279
  if not email or not password:
280
- st.error("Please enter both email and password.")
281
  else:
282
  success, user = authenticate_user(email, password)
283
  if success:
@@ -286,12 +269,15 @@ def authenticate_user_ui():
286
  st.success("Authentication successful! You can now manage your set of images and profiles.")
287
  main()
288
  else:
289
- st.error("Authentication failed. Please check your email and password.")
290
-
291
  elif option == "Sign-Up":
 
 
 
 
292
  confirm_password = st.text_input("Confirm Password", type="password", help="Re-enter your password for confirmation")
293
  if st.button("Sign-Up"):
294
- if not email or not password or not confirm_password:
295
  st.error("Please fill all the fields.")
296
  elif password != confirm_password:
297
  st.error("Passwords do not match.")
@@ -307,35 +293,56 @@ def logout():
307
  st.session_state.auth_state["user"] = None
308
  st.session_state.auth_state["signed_in"] = False
309
 
310
- # Streamlit interface for chat
311
- def chat_ui():
312
- st.title("Chat")
313
- sender_email = st.session_state.auth_state["user"].email
314
- receiver_email = st.text_input("Receiver's Email", help="Enter the receiver's email address")
315
- message = st.text_input("Type your message", help="Type your message here")
316
- if st.button("Send"):
317
- if not message or not receiver_email:
318
- st.error("Please enter both a message and the receiver's email.")
319
- else:
320
- if send_message(sender_email, receiver_email, message):
321
- st.success("Message sent successfully!")
322
- else:
323
- st.error("Failed to send message.")
324
-
325
- st.title("Messages")
326
- sender_messages, receiver_messages = get_messages(sender_email, receiver_email)
327
- if sender_messages:
328
- for key, value in sender_messages.items():
329
- st.write(f"{value['sender_email']}: {value['message']}")
330
- if receiver_messages:
331
- for key, value in receiver_messages.items():
332
- st.write(f"{value['sender_email']}: {value['message']}")
333
-
334
- # Update the main function to include the chat UI option
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  def main():
336
  st.sidebar.title("Options")
337
- option = st.sidebar.radio("Select Option", ["Tour Guide", "Add Person", "Recognize Face", "Delete Person", "Recognize Face (Optimal)", "Chat"])
338
-
339
  if option == "Tour Guide":
340
  tour_guide_ui()
341
  elif option == "Add Person":
@@ -346,9 +353,7 @@ def main():
346
  delete_person_ui()
347
  elif option == "Recognize Face (Optimal)":
348
  recognize_face_optimal_ui()
349
- elif option == "Chat":
350
- chat_ui()
351
 
352
- # Run the application
353
  if __name__ == "__main__":
354
- authenticate_user_ui()
 
8
  import cv2
9
  import dlib
10
 
11
+ # Get the current working directory
12
  current_directory = os.path.dirname(os.path.abspath(__file__))
13
  skp_path = os.path.join(current_directory, "projectinsta-s-firebase-adminsdk-y6vlu-9a1345f468.json")
14
 
15
+ # Check if the app is already initialized
16
  if not firebase_admin._apps:
17
+ # Initialize Firebase Admin SDK
18
  cred = credentials.Certificate(skp_path)
19
  firebase_admin.initialize_app(cred, {
20
  'databaseURL': 'https://projectinsta-s-default-rtdb.firebaseio.com/',
21
  'projectId': 'projectinsta-s'
22
  })
23
 
24
+ # Reference to the root of your Firebase Realtime Database
25
  ref = db.reference('/')
 
 
26
 
27
  # Streamlit session state
28
  if "auth_state" not in st.session_state:
 
37
  shape_predictor = dlib.shape_predictor(shape_predictor_path)
38
 
39
  # Firebase Authentication
40
+ def authenticate_user(email_or_username, password):
41
  try:
42
+ # Check if the input is an email
43
+ if "@" in email_or_username:
44
+ user = auth.get_user_by_email(email_or_username)
45
+ else:
46
+ # If not an email, assume it's a username
47
+ user = auth.get_user_by_email(email_or_username + "@example.com") # Assuming the username is unique and we append a dummy domain
48
+ # The user is successfully fetched, meaning the email/username and password are valid.
49
  return True, user
50
  except auth.AuthError as e:
51
  print(f"Authentication error: {str(e)}")
52
  return False, None
53
 
54
  # Sign-up Functionality
55
+ def create_user(email, username, password):
56
  try:
57
  user = auth.create_user(
58
  email=email,
59
+ password=password,
60
+ display_name=username # Using display_name to store the username
61
  )
62
  return True, user.uid
63
  except Exception as e:
 
86
  def detect_and_align_faces(image_path):
87
  image = face_recognition.load_image_file(image_path)
88
 
89
+ # Resize the image to a fixed width (you can adjust the width as needed)
90
  target_width = 800
91
  aspect_ratio = image.shape[1] / image.shape[0]
92
  target_height = int(target_width / aspect_ratio)
 
94
 
95
  gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
96
 
97
+ # Detect faces using dlib
98
  faces = detector(gray)
99
 
100
  if not faces:
101
  return None
102
 
103
+ # Use the first face found (you can modify this to handle multiple faces)
104
  face = faces[0]
105
 
106
+ # Use dlib for face alignment
107
  landmarks = shape_predictor(gray, face)
108
+ aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256) # Adjust the size as needed
109
 
110
  return aligned_face
111
 
 
116
  if not encoding:
117
  return "No face found in the provided image."
118
 
119
+ # Convert NumPy arrays to lists for JSON serialization
120
  encoding = encoding[0].tolist()
121
 
122
+ # Save data to Firebase Realtime Database
123
+ ref.child(name).set({
124
  "encoding": encoding,
125
  "info": {
126
  "instagram_handle": instagram_handle,
 
143
  return "No face found in the provided image."
144
 
145
  matches = []
146
+ for name, data in ref.get().items():
147
  known_encoding = np.array(data["encoding"])
148
  if face_recognition.compare_faces([known_encoding], unknown_encoding[0])[0]:
149
  matches.append((name, data["info"]))
 
172
  return "No face found in the provided image."
173
 
174
  matches = []
175
+ for name, data in ref.get().items():
176
  known_encoding = np.array(data["encoding"])
177
  similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding[0])[0]
178
  matches.append((name, similarity_score))
 
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>'
 
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
  # Streamlit interface for adding a person
202
  def add_person_ui():
203
  st.title("Add Person")
 
226
  if st.button("Recognize Face (Optimal)"):
227
  result = recognize_face_optimal(image_path)
228
  st.write(result, unsafe_allow_html=True)
229
+
230
  # Streamlit interface for deleting a person
231
  def delete_person_ui():
232
  st.title("Delete Person")
 
238
  result = delete_person(name)
239
  st.success(result)
240
 
241
+ def tour_guide_ui():
242
+ st.title("Tour Guide")
243
+ display_tour_steps(steps)
244
+
245
  # Streamlit interface for user authentication
246
  def authenticate_user_ui():
247
  st.title("Insta's EYE")
 
254
  else:
255
  option = st.sidebar.radio("Select Option", ["Login", "Sign-Up"])
256
 
 
 
 
257
  if option == "Login":
258
+ st.write("## Login")
259
+ email = st.text_input("Email or Username", help="Enter your email address or username")
260
+ password = st.text_input("Password", type="password", help="Enter your password")
261
  if st.button("Login"):
262
  if not email or not password:
263
+ st.error("Please enter both email/username and password.")
264
  else:
265
  success, user = authenticate_user(email, password)
266
  if success:
 
269
  st.success("Authentication successful! You can now manage your set of images and profiles.")
270
  main()
271
  else:
272
+ st.error("Authentication failed. Please check your email/username and password.")
 
273
  elif option == "Sign-Up":
274
+ st.write("## Sign-Up")
275
+ email = st.text_input("Email", help="Enter your email address")
276
+ username = st.text_input("Username", help="Enter your desired username")
277
+ password = st.text_input("Password", type="password", help="Enter your password")
278
  confirm_password = st.text_input("Confirm Password", type="password", help="Re-enter your password for confirmation")
279
  if st.button("Sign-Up"):
280
+ if not email or not username or not password or not confirm_password:
281
  st.error("Please fill all the fields.")
282
  elif password != confirm_password:
283
  st.error("Passwords do not match.")
 
293
  st.session_state.auth_state["user"] = None
294
  st.session_state.auth_state["signed_in"] = False
295
 
296
+ # Define tour steps
297
+ steps = [
298
+ {
299
+ "element": "h1",
300
+ "title": "Welcome to Insta's EYE",
301
+ "content": "This is a tour guide to help you navigate through the application.",
302
+ },
303
+ {
304
+ "element": '[aria-label="Select Option"]',
305
+ "title": "Options Sidebar",
306
+ "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.",
307
+ },
308
+ {
309
+ "element": "div[class='element-container']",
310
+ "title": "Main Interface",
311
+ "content": "This is where the main functionality of the application is displayed.",
312
+ },
313
+ {
314
+ "element": "[data-testid='stFileUploader']",
315
+ "title": "Upload Image",
316
+ "content": "You can upload an image here for face recognition or adding a person.",
317
+ },
318
+ {
319
+ "element": "[data-testid='stTextInput']",
320
+ "title": "Text Input",
321
+ "content": "Enter text here such as the person's name or Instagram handle.",
322
+ },
323
+ {
324
+ "element": "[data-testid='stButton']",
325
+ "title": "Buttons",
326
+ "content": "Click on these buttons to perform actions like adding a person or recognizing a face.",
327
+ },
328
+ ]
329
+
330
+ # Function to display tour steps
331
+ def display_tour_steps(steps):
332
+ st.markdown("# Tour Guide")
333
+ st.markdown("This tour will guide you through the application.")
334
+ st.markdown("---")
335
+
336
+ for step in steps:
337
+ st.markdown(f"## {step['title']}")
338
+ st.write(step['content'])
339
+ st.markdown("---")
340
+
341
+ # Update the main function to include the new option
342
  def main():
343
  st.sidebar.title("Options")
344
+ option = st.sidebar.radio("Select Option", ["Tour Guide", "Add Person", "Recognize Face", "Delete Person", "Recognize Face (Optimal)"])
345
+
346
  if option == "Tour Guide":
347
  tour_guide_ui()
348
  elif option == "Add Person":
 
353
  delete_person_ui()
354
  elif option == "Recognize Face (Optimal)":
355
  recognize_face_optimal_ui()
 
 
356
 
357
+ # Run the tour guide
358
  if __name__ == "__main__":
359
+ authenticate_user_ui()