Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import firebase_admin
|
| 4 |
-
from firebase_admin import credentials, db, auth
|
| 5 |
import face_recognition
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
|
@@ -20,6 +20,7 @@ if not firebase_admin._apps:
|
|
| 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('/')
|
|
@@ -57,6 +58,32 @@ def create_user(email, password):
|
|
| 57 |
except Exception as e:
|
| 58 |
print(f"User creation error: {str(e)}")
|
| 59 |
return False, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Update load_and_encode function to use the aligned face without normalization
|
| 62 |
def load_and_encode(image_path):
|
|
@@ -213,6 +240,18 @@ def recognize_face_ui():
|
|
| 213 |
result = recognize_face(image_path)
|
| 214 |
st.write(result, unsafe_allow_html=True)
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
# Streamlit interface for recognizing face with optimal ID
|
| 217 |
def recognize_face_optimal_ui():
|
| 218 |
st.title("Recognize Face (Optimal)")
|
|
@@ -280,31 +319,21 @@ def logout():
|
|
| 280 |
st.session_state.auth_state["user"] = None
|
| 281 |
st.session_state.auth_state["signed_in"] = False
|
| 282 |
|
| 283 |
-
# Update the main function to include the new option
|
| 284 |
# Update the main function to include the new option
|
| 285 |
def main():
|
| 286 |
st.sidebar.title("Options")
|
|
|
|
| 287 |
|
| 288 |
-
|
| 289 |
-
delete_icon_path = os.path.join(current_directory, "delete.png")
|
| 290 |
-
recognize_icon_path = os.path.join(current_directory, "Explore.png")
|
| 291 |
-
recognize_optimal_icon_path = os.path.join(current_directory, "Explore+.png")
|
| 292 |
-
|
| 293 |
-
add_icon = Image.open(add_icon_path)
|
| 294 |
-
delete_icon = Image.open(delete_icon_path)
|
| 295 |
-
recognize_icon = Image.open(recognize_icon_path)
|
| 296 |
-
recognize_optimal_icon = Image.open(recognize_optimal_icon_path)
|
| 297 |
-
|
| 298 |
-
option = st.sidebar.selectbox("Select Option", [add_icon, delete_icon, recognize_icon, recognize_optimal_icon], index=0, format_func=lambda x: "")
|
| 299 |
-
|
| 300 |
-
if option == add_icon:
|
| 301 |
add_person_ui()
|
| 302 |
-
elif option ==
|
| 303 |
-
delete_person_ui()
|
| 304 |
-
elif option == recognize_icon:
|
| 305 |
recognize_face_ui()
|
| 306 |
-
elif option ==
|
|
|
|
|
|
|
| 307 |
recognize_face_optimal_ui()
|
|
|
|
|
|
|
| 308 |
|
| 309 |
if __name__ == "__main__":
|
| 310 |
authenticate_user_ui()
|
|
|
|
| 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
|
|
|
|
| 20 |
'databaseURL': 'https://projectinsta-s-default-rtdb.firebaseio.com/',
|
| 21 |
'projectId': 'projectinsta-s'
|
| 22 |
})
|
| 23 |
+
db = firestore.client()
|
| 24 |
|
| 25 |
# Reference to the root of your Firebase Realtime Database
|
| 26 |
ref = db.reference('/')
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
print(f"User creation error: {str(e)}")
|
| 60 |
return False, None
|
| 61 |
+
|
| 62 |
+
# Function to send a message
|
| 63 |
+
def send_message(sender, recipient, message):
|
| 64 |
+
try:
|
| 65 |
+
# Add a new document with a generated ID
|
| 66 |
+
db.collection("messages").add({
|
| 67 |
+
"sender": sender,
|
| 68 |
+
"recipient": recipient,
|
| 69 |
+
"message": message,
|
| 70 |
+
"timestamp": firebase_admin.firestore.SERVER_TIMESTAMP
|
| 71 |
+
})
|
| 72 |
+
return "Message sent successfully!"
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"Failed to send message: {str(e)}"
|
| 75 |
+
|
| 76 |
+
# Function to retrieve messages
|
| 77 |
+
def retrieve_messages():
|
| 78 |
+
try:
|
| 79 |
+
# Retrieve all documents from the 'messages' collection
|
| 80 |
+
messages_ref = db.collection("messages").order_by("timestamp")
|
| 81 |
+
for message in messages_ref.stream():
|
| 82 |
+
message_data = message.to_dict()
|
| 83 |
+
# Display the message in the chat interface
|
| 84 |
+
st.write(f"{message_data['sender']}: {message_data['message']} ({message_data['timestamp']})")
|
| 85 |
+
except Exception as e:
|
| 86 |
+
st.error(f"Failed to retrieve messages: {str(e)}")
|
| 87 |
|
| 88 |
# Update load_and_encode function to use the aligned face without normalization
|
| 89 |
def load_and_encode(image_path):
|
|
|
|
| 240 |
result = recognize_face(image_path)
|
| 241 |
st.write(result, unsafe_allow_html=True)
|
| 242 |
|
| 243 |
+
# Streamlit interface for chat
|
| 244 |
+
def chat_ui():
|
| 245 |
+
st.title("Chat")
|
| 246 |
+
sender = st.session_state.auth_state["user"]["email"]
|
| 247 |
+
recipient = st.text_input("Recipient", help="Enter the recipient's email address")
|
| 248 |
+
message = st.text_area("Message", help="Type your message here")
|
| 249 |
+
if st.button("Send"):
|
| 250 |
+
result = send_message(sender, recipient, message)
|
| 251 |
+
st.write(result)
|
| 252 |
+
st.header("Messages")
|
| 253 |
+
retrieve_messages()
|
| 254 |
+
|
| 255 |
# Streamlit interface for recognizing face with optimal ID
|
| 256 |
def recognize_face_optimal_ui():
|
| 257 |
st.title("Recognize Face (Optimal)")
|
|
|
|
| 319 |
st.session_state.auth_state["user"] = None
|
| 320 |
st.session_state.auth_state["signed_in"] = False
|
| 321 |
|
|
|
|
| 322 |
# Update the main function to include the new option
|
| 323 |
def main():
|
| 324 |
st.sidebar.title("Options")
|
| 325 |
+
option = st.sidebar.radio("Select Option", ["Add Person", "Recognize Face", "Delete Person", "Recognize Face (Optimal)", "Chat"])
|
| 326 |
|
| 327 |
+
if option == "Add Person":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
add_person_ui()
|
| 329 |
+
elif option == "Recognize Face":
|
|
|
|
|
|
|
| 330 |
recognize_face_ui()
|
| 331 |
+
elif option == "Delete Person":
|
| 332 |
+
delete_person_ui()
|
| 333 |
+
elif option == "Recognize Face (Optimal)":
|
| 334 |
recognize_face_optimal_ui()
|
| 335 |
+
elif option == "Chat":
|
| 336 |
+
chat_ui()
|
| 337 |
|
| 338 |
if __name__ == "__main__":
|
| 339 |
authenticate_user_ui()
|