Prajwalds1 commited on
Commit
d7c1ed5
·
verified ·
1 Parent(s): a311bb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -1,28 +1,28 @@
1
- # app.py
2
- import streamlit as st
3
- from deepface import DeepFace
4
  import requests
5
  from PIL import Image
 
 
6
  from gtts import gTTS
7
- import os
8
  import cv2
9
  import numpy as np
10
 
11
  # Constants
12
- KNOWN_FOLDER = "known_faces"
 
13
  ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
14
- MODEL_NAME = "ArcFace"
15
- DETECTOR_BACKEND = "retinaface"
16
 
17
- # Ensure known folder exists
18
  os.makedirs(KNOWN_FOLDER, exist_ok=True)
19
 
20
- # Streamlit setup
21
  st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
22
  st.sidebar.title("Navigation")
23
  page = st.sidebar.radio("Go to", ["Face Recognition", "Upload Known Face"])
24
 
25
- # Get latest image from ESP32
26
  @st.cache_data(show_spinner=False)
27
  def get_latest_image():
28
  try:
@@ -34,7 +34,7 @@ def get_latest_image():
34
  except:
35
  return None
36
 
37
- # Preprocess the image to enhance quality
38
  def preprocess_image(image_path):
39
  img = cv2.imread(image_path)
40
  if img is None:
@@ -50,7 +50,7 @@ def preprocess_image(image_path):
50
  cv2.imwrite(output_path, final_img)
51
  return output_path
52
 
53
- # Check if a face is detected
54
  def is_face_detected(image_path):
55
  try:
56
  faces = DeepFace.extract_faces(
@@ -62,7 +62,7 @@ def is_face_detected(image_path):
62
  except:
63
  return False
64
 
65
- # Compare unknown image with known faces
66
  def compare_with_known_faces(unknown_img_path):
67
  for filename in os.listdir(KNOWN_FOLDER):
68
  known_img_path = os.path.join(KNOWN_FOLDER, filename)
@@ -86,25 +86,36 @@ if page == "Upload Known Face":
86
  uploaded = st.file_uploader("Choose an image of a known person", type=["jpg", "jpeg", "png"])
87
  name = st.text_input("Enter name of the person")
88
 
89
- if uploaded is not None and name.strip() != "":
90
  try:
91
- image_bytes = uploaded.read()
92
- filename = os.path.join(KNOWN_FOLDER, f"{name.strip().lower().replace(' ', '_')}.jpg")
93
- with open(filename, "wb") as f:
94
- f.write(image_bytes)
95
- st.success(f" Face saved as {name}")
96
- st.image(filename, caption="Saved Image", use_column_width=True)
 
 
 
 
 
 
 
 
97
  except Exception as e:
98
- st.error(f"❌ Error saving file: {e}")
99
 
100
- # Face recognition page
101
  elif page == "Face Recognition":
102
  st.title("Second Eye - Face Recognition")
 
103
  if st.button("Capture and Recognize Face"):
104
  image_url = get_latest_image()
 
105
  if image_url:
106
  st.image(image_url, caption="Captured Image", use_container_width=True)
107
  response = requests.get(image_url)
 
108
  with open("latest.jpg", "wb") as f:
109
  f.write(response.content)
110
 
 
1
+ import os
 
 
2
  import requests
3
  from PIL import Image
4
+ import streamlit as st
5
+ from deepface import DeepFace
6
  from gtts import gTTS
 
7
  import cv2
8
  import numpy as np
9
 
10
  # Constants
11
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
12
+ KNOWN_FOLDER = os.path.join(BASE_DIR, "known_faces")
13
  ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
14
+ MODEL_NAME = "ArcFace" # You can change the model to "VGG-Face" or "Facenet"
15
+ DETECTOR_BACKEND = "retinaface" # Detector can be "opencv", "mtcnn", "dlib", etc.
16
 
17
+ # Ensure the known_faces folder exists
18
  os.makedirs(KNOWN_FOLDER, exist_ok=True)
19
 
20
+ # Streamlit Setup
21
  st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
22
  st.sidebar.title("Navigation")
23
  page = st.sidebar.radio("Go to", ["Face Recognition", "Upload Known Face"])
24
 
25
+ # Image fetching from ESP32
26
  @st.cache_data(show_spinner=False)
27
  def get_latest_image():
28
  try:
 
34
  except:
35
  return None
36
 
37
+ # Enhance image quality
38
  def preprocess_image(image_path):
39
  img = cv2.imread(image_path)
40
  if img is None:
 
50
  cv2.imwrite(output_path, final_img)
51
  return output_path
52
 
53
+ # Check if a face is detected in the image
54
  def is_face_detected(image_path):
55
  try:
56
  faces = DeepFace.extract_faces(
 
62
  except:
63
  return False
64
 
65
+ # Compare with known faces
66
  def compare_with_known_faces(unknown_img_path):
67
  for filename in os.listdir(KNOWN_FOLDER):
68
  known_img_path = os.path.join(KNOWN_FOLDER, filename)
 
86
  uploaded = st.file_uploader("Choose an image of a known person", type=["jpg", "jpeg", "png"])
87
  name = st.text_input("Enter name of the person")
88
 
89
+ if uploaded and name:
90
  try:
91
+ # Ensure the image is RGB
92
+ image = Image.open(uploaded).convert("RGB")
93
+
94
+ # Sanitize filename
95
+ safe_name = name.strip().lower().replace(" ", "_")
96
+ file_path = os.path.join(KNOWN_FOLDER, f"{safe_name}.jpg")
97
+
98
+ # Save image
99
+ image.save(file_path, format="JPEG")
100
+
101
+ # Confirmation message
102
+ st.success(f"✅ Face saved as {safe_name}")
103
+ st.image(file_path, caption="Saved Image", use_column_width=True)
104
+
105
  except Exception as e:
106
+ st.error(f"❌ Error saving image: {e}")
107
 
108
+ # Face Recognition Page
109
  elif page == "Face Recognition":
110
  st.title("Second Eye - Face Recognition")
111
+
112
  if st.button("Capture and Recognize Face"):
113
  image_url = get_latest_image()
114
+
115
  if image_url:
116
  st.image(image_url, caption="Captured Image", use_container_width=True)
117
  response = requests.get(image_url)
118
+
119
  with open("latest.jpg", "wb") as f:
120
  f.write(response.content)
121