Files changed (1) hide show
  1. app.py +128 -9
app.py CHANGED
@@ -1,3 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from deepface import DeepFace
3
  import requests
@@ -10,8 +132,8 @@ import numpy as np
10
  # Constants
11
  KNOWN_FOLDER = "known_faces"
12
  ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
13
- MODEL_NAME = "ArcFace" # Options: "VGG-Face", "Facenet", "ArcFace", etc.
14
- DETECTOR_BACKEND = "retinaface" # Options: "opencv", "mtcnn", "dlib", "retinaface", etc.
15
 
16
  # Streamlit setup
17
  st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
@@ -33,22 +155,17 @@ def get_latest_image():
33
  def preprocess_image(image_path):
34
  img = cv2.imread(image_path)
35
  if img is None:
36
- return image_path # Fallback
37
 
38
- # Convert to grayscale
39
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
40
-
41
- # CLAHE for contrast enhancement
42
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
43
  enhanced = clahe.apply(gray)
44
 
45
- # Gamma correction
46
  gamma = 1.5
47
  lookUpTable = np.array([((i / 255.0) ** gamma) * 255 for i in range(256)]).astype("uint8")
48
  gamma_corrected = cv2.LUT(enhanced, lookUpTable)
49
-
50
- # Convert back to BGR
51
  final_img = cv2.cvtColor(gamma_corrected, cv2.COLOR_GRAY2BGR)
 
52
  output_path = "preprocessed.jpg"
53
  cv2.imwrite(output_path, final_img)
54
  return output_path
@@ -68,6 +185,8 @@ def is_face_detected(image_path):
68
  # Compare with known faces
69
  def compare_with_known_faces(unknown_img_path):
70
  for filename in os.listdir(KNOWN_FOLDER):
 
 
71
  known_img_path = os.path.join(KNOWN_FOLDER, filename)
72
  try:
73
  result = DeepFace.verify(
 
1
+ # import streamlit as st
2
+ # from deepface import DeepFace
3
+ # import requests
4
+ # from PIL import Image
5
+ # from gtts import gTTS
6
+ # import os
7
+ # import cv2
8
+ # import numpy as np
9
+
10
+ # # Constants
11
+ # KNOWN_FOLDER = "known_faces"
12
+ # ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
13
+ # MODEL_NAME = "ArcFace" # Options: "VGG-Face", "Facenet", "ArcFace", etc.
14
+ # DETECTOR_BACKEND = "retinaface" # Options: "opencv", "mtcnn", "dlib", "retinaface", etc.
15
+
16
+ # # Streamlit setup
17
+ # st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
18
+ # st.sidebar.title("Navigation")
19
+ # page = st.sidebar.radio("Go to", ["Face Recognition"])
20
+
21
+ # # Fetch image from ESP32
22
+ # def get_latest_image():
23
+ # try:
24
+ # r = requests.get(f"{ESP32_SERVER_URL}/latest")
25
+ # if r.status_code != 200:
26
+ # return None
27
+ # filename = r.json()["filename"]
28
+ # return f"{ESP32_SERVER_URL}/uploads/{filename}"
29
+ # except:
30
+ # return None
31
+
32
+ # # Preprocess image for better recognition
33
+ # def preprocess_image(image_path):
34
+ # img = cv2.imread(image_path)
35
+ # if img is None:
36
+ # return image_path # Fallback
37
+
38
+ # # Convert to grayscale
39
+ # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
40
+
41
+ # # CLAHE for contrast enhancement
42
+ # clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
43
+ # enhanced = clahe.apply(gray)
44
+
45
+ # # Gamma correction
46
+ # gamma = 1.5
47
+ # lookUpTable = np.array([((i / 255.0) ** gamma) * 255 for i in range(256)]).astype("uint8")
48
+ # gamma_corrected = cv2.LUT(enhanced, lookUpTable)
49
+
50
+ # # Convert back to BGR
51
+ # final_img = cv2.cvtColor(gamma_corrected, cv2.COLOR_GRAY2BGR)
52
+ # output_path = "preprocessed.jpg"
53
+ # cv2.imwrite(output_path, final_img)
54
+ # return output_path
55
+
56
+ # # Detect faces
57
+ # def is_face_detected(image_path):
58
+ # try:
59
+ # faces = DeepFace.extract_faces(
60
+ # img_path=image_path,
61
+ # enforce_detection=False,
62
+ # detector_backend=DETECTOR_BACKEND
63
+ # )
64
+ # return len(faces) > 0
65
+ # except:
66
+ # return False
67
+
68
+ # # Compare with known faces
69
+ # def compare_with_known_faces(unknown_img_path):
70
+ # for filename in os.listdir(KNOWN_FOLDER):
71
+ # known_img_path = os.path.join(KNOWN_FOLDER, filename)
72
+ # try:
73
+ # result = DeepFace.verify(
74
+ # img1_path=unknown_img_path,
75
+ # img2_path=known_img_path,
76
+ # model_name=MODEL_NAME,
77
+ # detector_backend=DETECTOR_BACKEND,
78
+ # enforce_detection=False
79
+ # )
80
+ # if result["verified"]:
81
+ # return filename.split('.')[0]
82
+ # except Exception as e:
83
+ # print(f"Comparison failed with {filename}: {e}")
84
+ # return None
85
+
86
+ # # Main UI
87
+ # if page == "Face Recognition":
88
+ # st.title("Second Eye - Enhanced Face Recognition")
89
+
90
+ # if st.button("Check for New Image"):
91
+ # image_url = get_latest_image()
92
+ # if image_url:
93
+ # st.image(image_url, caption="Captured Image", use_container_width=True)
94
+
95
+ # response = requests.get(image_url)
96
+ # with open("latest.jpg", "wb") as f:
97
+ # f.write(response.content)
98
+
99
+ # processed_img_path = preprocess_image("latest.jpg")
100
+
101
+ # if is_face_detected(processed_img_path):
102
+ # match = compare_with_known_faces(processed_img_path)
103
+ # if match:
104
+ # st.success(f"βœ… Match found: {match}")
105
+ # tts = gTTS(f"Match found: {match}")
106
+ # else:
107
+ # st.error("❌ No match found")
108
+ # tts = gTTS("No match found")
109
+ # else:
110
+ # st.warning("πŸ˜• No face detected in the image.")
111
+ # tts = gTTS("No face detected")
112
+
113
+ # tts.save("result.mp3")
114
+ # st.audio("result.mp3", autoplay=True)
115
+ # else:
116
+ # st.warning("No image found on ESP32 server")
117
+
118
+
119
+
120
+
121
+
122
+
123
  import streamlit as st
124
  from deepface import DeepFace
125
  import requests
 
132
  # Constants
133
  KNOWN_FOLDER = "known_faces"
134
  ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
135
+ MODEL_NAME = "ArcFace"
136
+ DETECTOR_BACKEND = "retinaface"
137
 
138
  # Streamlit setup
139
  st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
 
155
  def preprocess_image(image_path):
156
  img = cv2.imread(image_path)
157
  if img is None:
158
+ return image_path
159
 
 
160
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
 
161
  clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
162
  enhanced = clahe.apply(gray)
163
 
 
164
  gamma = 1.5
165
  lookUpTable = np.array([((i / 255.0) ** gamma) * 255 for i in range(256)]).astype("uint8")
166
  gamma_corrected = cv2.LUT(enhanced, lookUpTable)
 
 
167
  final_img = cv2.cvtColor(gamma_corrected, cv2.COLOR_GRAY2BGR)
168
+
169
  output_path = "preprocessed.jpg"
170
  cv2.imwrite(output_path, final_img)
171
  return output_path
 
185
  # Compare with known faces
186
  def compare_with_known_faces(unknown_img_path):
187
  for filename in os.listdir(KNOWN_FOLDER):
188
+ if not filename.lower().endswith((".jpg", ".jpeg", ".png")):
189
+ continue
190
  known_img_path = os.path.join(KNOWN_FOLDER, filename)
191
  try:
192
  result = DeepFace.verify(