File size: 13,468 Bytes
7e4f930 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
import os
import streamlit as st
import firebase_admin
from firebase_admin import credentials, db, auth
import face_recognition
from PIL import Image
import numpy as np
import cv2
import dlib
# Get the current working directory
current_directory = os.path.dirname(os.path.abspath(__file__))
skp_path = os.path.join(current_directory, "projectinsta-s-firebase-adminsdk-y6vlu-9a1345f468.json")
# Check if the app is already initialized
if not firebase_admin._apps:
# Initialize Firebase Admin SDK
cred = credentials.Certificate(skp_path)
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://projectinsta-s-default-rtdb.firebaseio.com/',
'projectId': 'projectinsta-s'
})
# Reference to the root of your Firebase Realtime Database
ref = db.reference('/')
# Streamlit session state
if "auth_state" not in st.session_state:
st.session_state.auth_state = {
"user": None,
"signed_in": False,
}
# Add the shape predictor model for face alignment
shape_predictor_path = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor(shape_predictor_path)
# Firebase Authentication
def authenticate_user(email, password):
try:
user = auth.get_user_by_email(email)
# The user is successfully fetched, meaning the email and password are valid.
return True, user
except auth.AuthError as e:
print(f"Authentication error: {str(e)}")
return False, None
# Sign-up Functionality
def create_user(email, password):
try:
user = auth.create_user(
email=email,
password=password
)
return True, user.uid
except Exception as e:
print(f"User creation error: {str(e)}")
return False, None
# Update load_and_encode function to use the aligned face without normalization
def load_and_encode(image_path):
try:
aligned_face = detect_and_align_faces(image_path)
if aligned_face is not None:
encoding = face_recognition.face_encodings(aligned_face)
if encoding:
return encoding
else:
return None
else:
return None
except Exception as e:
print(f"Error loading and encoding image: {str(e)}")
return None
# Function to detect and align faces in an image with preprocessing
def detect_and_align_faces(image_path):
image = face_recognition.load_image_file(image_path)
# Resize the image to a fixed width (you can adjust the width as needed)
target_width = 800
aspect_ratio = image.shape[1] / image.shape[0]
target_height = int(target_width / aspect_ratio)
resized_image = cv2.resize(image, (target_width, target_height))
gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
# Detect faces using dlib
faces = detector(gray)
if not faces:
return None
# Use the first face found (you can modify this to handle multiple faces)
face = faces[0]
# Use dlib for face alignment
landmarks = shape_predictor(gray, face)
aligned_face = dlib.get_face_chip(resized_image, landmarks, size=256) # Adjust the size as needed
return aligned_face
# Add person to database
def add_person(name, image_path, instagram_handle):
try:
encoding = load_and_encode(image_path)
if not encoding:
return "No face found in the provided image."
# Convert NumPy arrays to lists for JSON serialization
encoding = encoding[0].tolist()
# Save data to Firebase Realtime Database
ref.child(name).set({
"encoding": encoding,
"info": {
"instagram_handle": instagram_handle,
"instagram_link": f"https://www.instagram.com/{instagram_handle}/"
}
})
return f"Success: {name} added to the database!"
except Exception as e:
return f"Failed to add person: {str(e)}"
# Recognize face from image
def recognize_face(image_path):
if not image_path:
return "Please upload an image."
try:
unknown_encoding = load_and_encode(image_path)
if not unknown_encoding:
return "No face found in the provided image."
matches = []
for name, data in ref.get().items():
known_encoding = np.array(data["encoding"])
if face_recognition.compare_faces([known_encoding], unknown_encoding[0])[0]:
matches.append((name, data["info"]))
if matches:
results = []
for name, info in matches:
insta_handle = info["instagram_handle"]
insta_link = info["instagram_link"]
insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
results.append(f"- It's a picture of {name}! Insta handle: {insta_link_html}")
return "\n".join(results)
else:
return "Face not found in the database."
except Exception as e:
return f"Failed to recognize face: {str(e)}"
# Recognize face from image and return optimal or highest matching ID
def recognize_face_optimal(image_path):
if not image_path:
return "Please upload an image."
try:
unknown_encoding = load_and_encode(image_path)
if not unknown_encoding:
return "No face found in the provided image."
matches = []
for name, data in ref.get().items():
known_encoding = np.array(data["encoding"])
similarity_score = face_recognition.face_distance([known_encoding], unknown_encoding[0])[0]
matches.append((name, similarity_score))
if matches:
best_match = min(matches, key=lambda x: x[1])
best_name, best_score = best_match
info = ref.child(best_name).child("info").get()
insta_handle = info["instagram_handle"]
insta_link = info["instagram_link"]
insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
return f"Best match: {best_name} with a similarity score of {1 - best_score:.2%}. Insta handle: {insta_link_html}"
else:
return "Face not found in the database."
except Exception as e:
return f"Failed to recognize face: {str(e)}"
# Delete person from database
def delete_person(name):
try:
ref.child(name).delete()
return f"{name} deleted from the database!"
except Exception as e:
return f"Failed to delete person: {str(e)}"
# Streamlit interface for adding a person
def add_person_ui():
st.title("Add Person")
name = st.text_input("Enter Name", help="Enter the name of the person")
image_path = st.file_uploader("Upload Image", help="Upload an image containing the person's face")
instagram_handle = st.text_input("Enter Instagram Handle", help="Enter the person's Instagram handle")
if st.button("Add Person"):
if not name or not image_path or not instagram_handle:
st.error("Please fill all the fields.")
else:
result = add_person(name, image_path, instagram_handle)
st.success(result)
# Streamlit interface for recognizing face
def recognize_face_ui():
st.title("Recognize Face")
image_path = st.file_uploader("Upload Image", help="Upload an image for face recognition")
if st.button("Recognize Face"):
result = recognize_face(image_path)
st.write(result, unsafe_allow_html=True)
# Streamlit interface for recognizing face with optimal ID
def recognize_face_optimal_ui():
st.title("Recognize Face (Optimal)")
image_path = st.file_uploader("Upload Image", help="Upload an image for optimal face recognition")
if st.button("Recognize Face (Optimal)"):
result = recognize_face_optimal(image_path)
st.write(result, unsafe_allow_html=True)
# Streamlit interface for deleting a person
def delete_person_ui():
st.title("Delete Person")
name = st.text_input("Enter Name", help="Enter the name of the person to delete")
if st.button("Delete Person"):
if not name:
st.error("Please enter a name.")
else:
result = delete_person(name)
st.success(result)
def tour_guide_ui():
st.title("Tour Guide")
st.markdown("This tour will guide you through the application.")
with st.expander("Welcome"):
st.write("This is a tour guide to help you navigate through the application.")
with st.expander("Options Sidebar"):
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.")
with st.expander("Main Interface"):
st.write("This is where the main functionality of the application is displayed.")
with st.expander("Upload Image"):
st.write("You can upload an image here for face recognition or adding a person.")
with st.expander("Text Input"):
st.write("Enter text here such as the person's name or Instagram handle.")
with st.expander("Buttons"):
st.write("Click on these buttons to perform actions like adding a person or recognizing a face.")
# Streamlit interface for user authentication
def authenticate_user_ui():
st.title("Insta's EYE")
st.sidebar.title("Options")
if st.session_state.auth_state["signed_in"]:
st.sidebar.button("Sign Out", on_click=logout)
st.title("Welcome!")
main()
else:
option = st.sidebar.radio("Select Option", ["Login", "Sign-Up"])
email = st.text_input("Enter Email", help="Enter your email address")
password = st.text_input("Enter Password", type="password", help="Enter your password")
if option == "Login":
if st.button("Login"):
if not email or not password:
st.error("Please enter both email and password.")
else:
success, user = authenticate_user(email, password)
if success:
st.session_state.auth_state["user"] = user
st.session_state.auth_state["signed_in"] = True
st.success("Authentication successful! You can now manage your set of images and profiles.")
main()
else:
st.error("Authentication failed. Please check your email and password.")
elif option == "Sign-Up":
confirm_password = st.text_input("Confirm Password", type="password", help="Re-enter your password for confirmation")
if st.button("Sign-Up"):
if not email or not password or not confirm_password:
st.error("Please fill all the fields.")
elif password != confirm_password:
st.error("Passwords do not match.")
else:
success, uid = create_user(email, password)
if success:
st.success(f"User with UID: {uid} created successfully! You can now log in.")
else:
st.error("User creation failed. Please try again.")
# Log out user
def logout():
st.session_state.auth_state["user"] = None
st.session_state.auth_state["signed_in"] = False
# Define tour steps
steps = [
{
"title": "Welcome to Insta's EYE",
"content": "This is a tour guide to help you navigate through the application.",
},
{
"title": "Options Sidebar",
"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.",
},
{
"title": "Main Interface",
"content": "This is where the main functionality of the application is displayed.",
},
{
"title": "Upload Image",
"content": "You can upload an image here for face recognition or adding a person.",
},
{
"title": "Text Input",
"content": "Enter text here such as the person's name or Instagram handle.",
},
{
"title": "Buttons",
"content": "Click on these buttons to perform actions like adding a person or recognizing a face.",
},
]
# Function to display tour steps
def display_tour_steps(steps):
st.markdown("# Tour Guide")
st.markdown("This tour will guide you through the application.")
st.markdown("---")
for step in steps:
st.markdown(f"## {step['title']}")
st.write(step['content'])
st.markdown("---")
# Update the main function to include the new option
def main():
st.sidebar.title("Options")
option = st.sidebar.radio("Select Option", ["Add Person", "Recognize Face", "Delete Person", "Recognize Face (Optimal)","Tour Guide"])
if option == "Add Person":
add_person_ui()
elif option == "Recognize Face":
recognize_face_ui()
elif option == "Delete Person":
delete_person_ui()
elif option == "Recognize Face (Optimal)":
recognize_face_optimal_ui()
elif option == "Tour Guide":
tour_guide_ui()
# Run the tour guide
if __name__ == "__main__":
authenticate_user_ui() |