Prajwalds1 commited on
Commit
e66c65d
·
verified ·
1 Parent(s): c8ca665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -75
app.py CHANGED
@@ -1,28 +1,44 @@
 
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,7 +55,7 @@ def preprocess_image(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,98 +67,97 @@ def is_face_detected(image_path):
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
77
  )
78
  if result["verified"]:
79
- return filename.split('.')[0]
80
  except:
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
 
 
 
 
 
 
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
+ from datetime import datetime
11
+ from io import BytesIO
12
+ from huggingface_hub import HfApi, HfFolder, Repository, upload_file
13
 
14
  # Constants
15
  REPO_ID = "Prajwalds1/seceye"
16
  KNOWN_FOLDER = "known_faces"
17
  MODEL_NAME = "ArcFace"
18
  DETECTOR_BACKEND = "retinaface"
19
+ ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
20
+ HF_TOKEN = os.environ.get("HF_TOKEN")
 
21
 
22
  # Streamlit setup
23
+ st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
24
  st.sidebar.title("Navigation")
25
  page = st.sidebar.radio("Go to", ["Face Recognition", "Upload Known Face"])
26
 
27
+ # Authenticate with Hugging Face
28
+ api = HfApi()
29
+
30
+ # Fetch latest image from ESP32
31
+ @st.cache_data(show_spinner=False)
32
+ def get_latest_image():
33
+ try:
34
+ r = requests.get(f"{ESP32_SERVER_URL}/latest")
35
+ if r.status_code != 200:
36
+ return None
37
+ filename = r.json()["filename"]
38
+ return f"{ESP32_SERVER_URL}/uploads/{filename}"
39
+ except:
40
+ return None
41
+
42
  # Enhance image quality
43
  def preprocess_image(image_path):
44
  img = cv2.imread(image_path)
 
55
  cv2.imwrite(output_path, final_img)
56
  return output_path
57
 
58
+ # Check if a face is present
59
  def is_face_detected(image_path):
60
  try:
61
  faces = DeepFace.extract_faces(
 
67
  except:
68
  return False
69
 
70
+ # Match against known faces
71
  def compare_with_known_faces(unknown_img_path):
72
+ # Get list of image URLs from repo
73
+ files = api.list_repo_files(REPO_ID, repo_type="dataset")
74
+ face_files = [f for f in files if f.startswith(KNOWN_FOLDER)]
75
+ for file in face_files:
 
 
 
 
 
 
 
 
 
 
76
  try:
77
+ url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{file}"
78
+ r = requests.get(url)
79
+ temp_path = f"temp_face.jpg"
80
+ with open(temp_path, "wb") as f:
81
+ f.write(r.content)
82
  result = DeepFace.verify(
83
  img1_path=unknown_img_path,
84
+ img2_path=temp_path,
85
  model_name=MODEL_NAME,
86
  detector_backend=DETECTOR_BACKEND,
87
  enforce_detection=False
88
  )
89
  if result["verified"]:
90
+ return os.path.splitext(os.path.basename(file))[0]
91
  except:
92
  continue
93
  return None
94
 
95
+ # Upload known face to Hugging Face repo
96
+ def upload_known_face_to_hf(image_data, filename):
97
+ try:
98
+ image_bytes = BytesIO()
99
+ image_data.save(image_bytes, format="JPEG")
100
+ image_bytes.seek(0)
101
+ upload_path = f"{KNOWN_FOLDER}/{filename}"
102
+ upload_file(
103
+ path_or_fileobj=image_bytes,
104
+ path_in_repo=upload_path,
105
+ repo_id=REPO_ID,
106
+ repo_type="dataset",
107
+ token=HF_TOKEN
108
+ )
109
+ return True
110
+ except Exception as e:
111
+ print("Upload failed:", e)
112
+ return False
113
 
114
+ # Upload Known Face page
115
  if page == "Upload Known Face":
116
  st.title("Upload Known Face")
117
+ uploaded = st.file_uploader("Choose an image of a known person", type=["jpg", "jpeg", "png"])
118
+ name = st.text_input("Enter name of the person")
119
  if uploaded and name:
120
+ image = Image.open(uploaded)
121
+ success = upload_known_face_to_hf(image, f"{name}.jpg")
 
 
 
 
122
  if success:
123
+ st.success(f"✅ Image successfully uploaded as {name}.jpg")
124
  else:
125
+ st.error("❌ Image upload failed")
126
+
127
+ # Display current known faces
128
+ with st.expander("View Uploaded Known Faces"):
129
+ files = api.list_repo_files(REPO_ID, repo_type="dataset")
130
+ face_files = [f for f in files if f.startswith(KNOWN_FOLDER)]
131
+ for file in face_files:
132
+ img_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{file}"
133
+ st.image(img_url, caption=file.split("/")[-1], width=150)
134
 
135
+ # Face Recognition page
136
  elif page == "Face Recognition":
137
  st.title("Second Eye - Face Recognition")
138
  if st.button("Capture and Recognize Face"):
139
+ image_url = get_latest_image()
140
+ if image_url:
141
+ st.image(image_url, caption="Captured Image", use_container_width=True)
142
+ response = requests.get(image_url)
143
+ with open("latest.jpg", "wb") as f:
144
+ f.write(response.content)
 
 
 
 
145
 
146
+ processed_img_path = preprocess_image("latest.jpg")
147
 
148
+ if is_face_detected(processed_img_path):
149
+ match = compare_with_known_faces(processed_img_path)
150
+ if match:
151
+ st.success(f"✅ Match found: {match}")
152
+ tts = gTTS(f"Match found: {match}")
 
 
 
153
  else:
154
+ st.error(" No match found")
155
+ tts = gTTS("No match found")
 
 
156
  else:
157
+ st.warning("😕 No face detected")
158
+ tts = gTTS("No face detected")
 
159
 
160
+ tts.save("result.mp3")
161
+ st.audio("result.mp3", autoplay=True)
162
+ else:
163
+ st.warning("No image found on ESP32 server")