Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,28 @@
|
|
| 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"
|
| 14 |
DETECTOR_BACKEND = "retinaface"
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
os.
|
| 18 |
-
|
| 19 |
-
# Fetch Hugging Face token securely from secrets
|
| 20 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 21 |
-
|
| 22 |
-
# Check if the token is available
|
| 23 |
-
if not HF_TOKEN:
|
| 24 |
-
raise ValueError("Hugging Face token is missing")
|
| 25 |
-
|
| 26 |
-
# Define your repository URL
|
| 27 |
-
HF_REPO = "https://huggingface.co/Prajwalds1/seceye" # Your repo URL
|
| 28 |
-
UPLOAD_URL = f"{HF_REPO}/upload"
|
| 29 |
|
| 30 |
# Streamlit setup
|
| 31 |
-
st.set_page_config(page_title="Second Eye
|
| 32 |
st.sidebar.title("Navigation")
|
| 33 |
page = st.sidebar.radio("Go to", ["Face Recognition", "Upload Known Face"])
|
| 34 |
|
| 35 |
-
# Fetch image from ESP32
|
| 36 |
-
@st.cache_data(show_spinner=False)
|
| 37 |
-
def get_latest_image():
|
| 38 |
-
try:
|
| 39 |
-
r = requests.get(f"{ESP32_SERVER_URL}/latest")
|
| 40 |
-
if r.status_code != 200:
|
| 41 |
-
return None
|
| 42 |
-
filename = r.json()["filename"]
|
| 43 |
-
return f"{ESP32_SERVER_URL}/uploads/{filename}"
|
| 44 |
-
except:
|
| 45 |
-
return None
|
| 46 |
-
|
| 47 |
# Enhance image quality
|
| 48 |
def preprocess_image(image_path):
|
| 49 |
img = cv2.imread(image_path)
|
|
@@ -60,7 +39,7 @@ def preprocess_image(image_path):
|
|
| 60 |
cv2.imwrite(output_path, final_img)
|
| 61 |
return output_path
|
| 62 |
|
| 63 |
-
#
|
| 64 |
def is_face_detected(image_path):
|
| 65 |
try:
|
| 66 |
faces = DeepFace.extract_faces(
|
|
@@ -72,14 +51,26 @@ def is_face_detected(image_path):
|
|
| 72 |
except:
|
| 73 |
return False
|
| 74 |
|
| 75 |
-
#
|
| 76 |
def compare_with_known_faces(unknown_img_path):
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
result = DeepFace.verify(
|
| 81 |
img1_path=unknown_img_path,
|
| 82 |
-
img2_path=
|
| 83 |
model_name=MODEL_NAME,
|
| 84 |
detector_backend=DETECTOR_BACKEND,
|
| 85 |
enforce_detection=False
|
|
@@ -90,60 +81,68 @@ def compare_with_known_faces(unknown_img_path):
|
|
| 90 |
continue
|
| 91 |
return None
|
| 92 |
|
| 93 |
-
# Upload
|
| 94 |
-
def
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.error(f"β Image upload failed with status code {response.status_code}: {response.text}")
|
| 106 |
|
| 107 |
# Upload Known Face Page
|
| 108 |
if page == "Upload Known Face":
|
| 109 |
st.title("Upload Known Face")
|
| 110 |
-
uploaded = st.file_uploader("Choose
|
| 111 |
-
name = st.text_input("Enter name
|
| 112 |
if uploaded and name:
|
| 113 |
-
image = Image.open(uploaded)
|
| 114 |
-
|
| 115 |
-
image.save(
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Face Recognition Page
|
| 122 |
elif page == "Face Recognition":
|
| 123 |
st.title("Second Eye - Face Recognition")
|
| 124 |
if st.button("Capture and Recognize Face"):
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
else:
|
| 140 |
-
st.
|
| 141 |
-
tts = gTTS("No
|
|
|
|
|
|
|
| 142 |
else:
|
| 143 |
-
st.warning("
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
-
tts.save("result.mp3")
|
| 147 |
-
st.audio("result.mp3", autoplay=True)
|
| 148 |
-
else:
|
| 149 |
-
st.warning("No image found on ESP32 server")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import requests
|
| 3 |
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
from gtts import gTTS
|
| 6 |
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from deepface import DeepFace
|
| 9 |
import cv2
|
| 10 |
import numpy as np
|
| 11 |
|
| 12 |
# Constants
|
| 13 |
+
REPO_ID = "Prajwalds1/seceye"
|
| 14 |
KNOWN_FOLDER = "known_faces"
|
|
|
|
| 15 |
MODEL_NAME = "ArcFace"
|
| 16 |
DETECTOR_BACKEND = "retinaface"
|
| 17 |
|
| 18 |
+
# Hugging Face Token from Secrets
|
| 19 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Streamlit setup
|
| 22 |
+
st.set_page_config(page_title="Second Eye", layout="centered")
|
| 23 |
st.sidebar.title("Navigation")
|
| 24 |
page = st.sidebar.radio("Go to", ["Face Recognition", "Upload Known Face"])
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Enhance image quality
|
| 27 |
def preprocess_image(image_path):
|
| 28 |
img = cv2.imread(image_path)
|
|
|
|
| 39 |
cv2.imwrite(output_path, final_img)
|
| 40 |
return output_path
|
| 41 |
|
| 42 |
+
# Face detection
|
| 43 |
def is_face_detected(image_path):
|
| 44 |
try:
|
| 45 |
faces = DeepFace.extract_faces(
|
|
|
|
| 51 |
except:
|
| 52 |
return False
|
| 53 |
|
| 54 |
+
# Compare with known faces
|
| 55 |
def compare_with_known_faces(unknown_img_path):
|
| 56 |
+
response = requests.get(
|
| 57 |
+
f"https://huggingface.co/api/datasets/{REPO_ID}/tree/main/{KNOWN_FOLDER}",
|
| 58 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"}
|
| 59 |
+
)
|
| 60 |
+
files = response.json()
|
| 61 |
+
|
| 62 |
+
for file in files:
|
| 63 |
+
filename = file["path"].split("/")[-1]
|
| 64 |
+
file_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{file['path']}"
|
| 65 |
+
|
| 66 |
+
response = requests.get(file_url)
|
| 67 |
+
with open("temp_face.jpg", "wb") as f:
|
| 68 |
+
f.write(response.content)
|
| 69 |
+
|
| 70 |
try:
|
| 71 |
result = DeepFace.verify(
|
| 72 |
img1_path=unknown_img_path,
|
| 73 |
+
img2_path="temp_face.jpg",
|
| 74 |
model_name=MODEL_NAME,
|
| 75 |
detector_backend=DETECTOR_BACKEND,
|
| 76 |
enforce_detection=False
|
|
|
|
| 81 |
continue
|
| 82 |
return None
|
| 83 |
|
| 84 |
+
# Upload image to Hugging Face repo
|
| 85 |
+
def upload_to_hf_repo(image_bytes, filename):
|
| 86 |
+
api_url = f"https://huggingface.co/api/repos/{REPO_ID}/upload/main/{KNOWN_FOLDER}/{filename}"
|
| 87 |
+
response = requests.put(
|
| 88 |
+
api_url,
|
| 89 |
+
headers={
|
| 90 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 91 |
+
"Content-Type": "application/octet-stream"
|
| 92 |
+
},
|
| 93 |
+
data=image_bytes
|
| 94 |
+
)
|
| 95 |
+
return response.status_code == 200
|
|
|
|
| 96 |
|
| 97 |
# Upload Known Face Page
|
| 98 |
if page == "Upload Known Face":
|
| 99 |
st.title("Upload Known Face")
|
| 100 |
+
uploaded = st.file_uploader("Choose image", type=["jpg", "jpeg", "png"])
|
| 101 |
+
name = st.text_input("Enter name")
|
| 102 |
if uploaded and name:
|
| 103 |
+
image = Image.open(uploaded).convert("RGB")
|
| 104 |
+
img_byte_arr = BytesIO()
|
| 105 |
+
image.save(img_byte_arr, format='JPEG')
|
| 106 |
+
img_bytes = img_byte_arr.getvalue()
|
| 107 |
+
filename = f"{name}.jpg"
|
| 108 |
+
success = upload_to_hf_repo(img_bytes, filename)
|
| 109 |
+
if success:
|
| 110 |
+
st.success(f"β
Image saved as {filename} to Hugging Face repo.")
|
| 111 |
+
else:
|
| 112 |
+
st.error("β Failed to upload image.")
|
| 113 |
|
| 114 |
# Face Recognition Page
|
| 115 |
elif page == "Face Recognition":
|
| 116 |
st.title("Second Eye - Face Recognition")
|
| 117 |
if st.button("Capture and Recognize Face"):
|
| 118 |
+
ESP32_URL = "https://esp32-upload-server.onrender.com/latest"
|
| 119 |
+
try:
|
| 120 |
+
res = requests.get(ESP32_URL)
|
| 121 |
+
filename = res.json()["filename"]
|
| 122 |
+
img_url = f"https://esp32-upload-server.onrender.com/uploads/{filename}"
|
| 123 |
+
image_response = requests.get(img_url)
|
| 124 |
+
if image_response.status_code == 200:
|
| 125 |
+
with open("latest.jpg", "wb") as f:
|
| 126 |
+
f.write(image_response.content)
|
| 127 |
+
st.image("latest.jpg", caption="Captured Image")
|
| 128 |
+
|
| 129 |
+
processed_img_path = preprocess_image("latest.jpg")
|
| 130 |
+
|
| 131 |
+
if is_face_detected(processed_img_path):
|
| 132 |
+
match = compare_with_known_faces(processed_img_path)
|
| 133 |
+
if match:
|
| 134 |
+
st.success(f"β
Match found: {match}")
|
| 135 |
+
tts = gTTS(f"Match found: {match}")
|
| 136 |
+
else:
|
| 137 |
+
st.error("β No match found")
|
| 138 |
+
tts = gTTS("No match found")
|
| 139 |
else:
|
| 140 |
+
st.warning("π No face detected")
|
| 141 |
+
tts = gTTS("No face detected")
|
| 142 |
+
tts.save("result.mp3")
|
| 143 |
+
st.audio("result.mp3", autoplay=True)
|
| 144 |
else:
|
| 145 |
+
st.warning("Failed to fetch image from ESP32")
|
| 146 |
+
except:
|
| 147 |
+
st.error("Error fetching from ESP32")
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|