Prajwalds1 commited on
Commit
3fe5cc4
·
verified ·
1 Parent(s): 7ebf7ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ KNOWN_FOLDER = "known_faces"
9
+ ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
10
+ # FLASK_UPLOAD_URL = "https://flask-upload-pzch.onrender.com/upload"
11
+
12
+ # Set Streamlit layout
13
+ st.set_page_config(page_title="Second Eye - Face Recognition", layout="centered")
14
+
15
+ # Sidebar navigation
16
+ st.sidebar.title("Navigation")
17
+ page = st.sidebar.radio("Go to", ["Face Recognition"])
18
+
19
+ # Fetch latest image from ESP32 server
20
+ def get_latest_image():
21
+ try:
22
+ r = requests.get(f"{ESP32_SERVER_URL}/latest")
23
+ if r.status_code != 200:
24
+ return None
25
+ filename = r.json()["filename"]
26
+ return f"{ESP32_SERVER_URL}/uploads/{filename}"
27
+ except:
28
+ return None
29
+
30
+ # Face detection check
31
+ def is_face_detected(image_path):
32
+ try:
33
+ faces = DeepFace.extract_faces(img_path=image_path, enforce_detection=True)
34
+ return len(faces) > 0
35
+ except:
36
+ return False
37
+
38
+ # Face comparison using Facenet
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)
42
+ try:
43
+ result = DeepFace.verify(
44
+ img1_path=unknown_img_path,
45
+ img2_path=known_img_path,
46
+ model_name="Facenet",
47
+ enforce_detection=True
48
+ )
49
+ if result["verified"]:
50
+ return filename.split('.')[0]
51
+ except Exception as e:
52
+ print(f"Comparison failed with {filename}: {e}")
53
+ return None
54
+
55
+ # -------------- PAGE 1: Face Recognition --------------
56
+ if page == "Face Recognition":
57
+ st.title("ESP32-CAM Face Recognition")
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
+ if is_face_detected("latest.jpg"):
68
+ match = compare_with_known_faces("latest.jpg")
69
+ if match:
70
+ st.success(f"✅ Match found: {match}")
71
+ tts = gTTS(f"Match found: {match}")
72
+ else:
73
+ st.error("❌ No match found")
74
+ tts = gTTS("No match found")
75
+ else:
76
+ st.warning("😕 No face detected in the image.")
77
+ tts = gTTS("No face detected")
78
+
79
+ tts.save("result.mp3")
80
+ st.audio("result.mp3", autoplay=True)
81
+ else:
82
+ st.warning("No image found on ESP32 server.")