Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,19 +4,21 @@ import requests
|
|
| 4 |
from PIL import Image
|
| 5 |
from gtts import gTTS
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
KNOWN_FOLDER = "known_faces"
|
| 9 |
ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
|
| 10 |
-
#
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
st.set_page_config(page_title="Second Eye -
|
| 14 |
-
|
| 15 |
-
# Sidebar navigation
|
| 16 |
st.sidebar.title("Navigation")
|
| 17 |
page = st.sidebar.radio("Go to", ["Face Recognition"])
|
| 18 |
|
| 19 |
-
# Fetch
|
| 20 |
def get_latest_image():
|
| 21 |
try:
|
| 22 |
r = requests.get(f"{ESP32_SERVER_URL}/latest")
|
|
@@ -27,15 +29,43 @@ def get_latest_image():
|
|
| 27 |
except:
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def is_face_detected(image_path):
|
| 32 |
try:
|
| 33 |
-
faces = DeepFace.extract_faces(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return len(faces) > 0
|
| 35 |
except:
|
| 36 |
return False
|
| 37 |
|
| 38 |
-
#
|
| 39 |
def compare_with_known_faces(unknown_img_path):
|
| 40 |
for filename in os.listdir(KNOWN_FOLDER):
|
| 41 |
known_img_path = os.path.join(KNOWN_FOLDER, filename)
|
|
@@ -43,8 +73,9 @@ def compare_with_known_faces(unknown_img_path):
|
|
| 43 |
result = DeepFace.verify(
|
| 44 |
img1_path=unknown_img_path,
|
| 45 |
img2_path=known_img_path,
|
| 46 |
-
model_name=
|
| 47 |
-
|
|
|
|
| 48 |
)
|
| 49 |
if result["verified"]:
|
| 50 |
return filename.split('.')[0]
|
|
@@ -52,20 +83,23 @@ def compare_with_known_faces(unknown_img_path):
|
|
| 52 |
print(f"Comparison failed with {filename}: {e}")
|
| 53 |
return None
|
| 54 |
|
| 55 |
-
#
|
| 56 |
if page == "Face Recognition":
|
| 57 |
-
st.title("
|
| 58 |
|
| 59 |
if st.button("Check for New Image"):
|
| 60 |
image_url = get_latest_image()
|
| 61 |
if image_url:
|
| 62 |
st.image(image_url, caption="Captured Image", use_container_width=True)
|
|
|
|
| 63 |
response = requests.get(image_url)
|
| 64 |
with open("latest.jpg", "wb") as f:
|
| 65 |
f.write(response.content)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
if match:
|
| 70 |
st.success(f"✅ Match found: {match}")
|
| 71 |
tts = gTTS(f"Match found: {match}")
|
|
@@ -79,4 +113,4 @@ if page == "Face Recognition":
|
|
| 79 |
tts.save("result.mp3")
|
| 80 |
st.audio("result.mp3", autoplay=True)
|
| 81 |
else:
|
| 82 |
-
st.warning("No image found on ESP32 server
|
|
|
|
| 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")
|
|
|
|
| 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)
|
|
|
|
| 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]
|
|
|
|
| 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}")
|
|
|
|
| 113 |
tts.save("result.mp3")
|
| 114 |
st.audio("result.mp3", autoplay=True)
|
| 115 |
else:
|
| 116 |
+
st.warning("No image found on ESP32 server")
|