File size: 3,630 Bytes
d7c1ed5
e66c65d
24ff78e
 
3fe5cc4
24ff78e
3f7610e
 
3fe5cc4
3f7610e
5d5d145
e66c65d
07eda8f
 
2eb283b
 
e66c65d
3fe5cc4
5d5d145
2eb283b
5d5d145
e66c65d
 
 
 
 
 
 
 
 
 
f09bf0d
3f7610e
 
 
07eda8f
5d5d145
3f7610e
f09bf0d
3f7610e
5d5d145
f09bf0d
3f7610e
f09bf0d
3f7610e
07eda8f
3f7610e
 
 
 
f09bf0d
 
3fe5cc4
3f7610e
 
f09bf0d
 
3f7610e
f09bf0d
 
 
 
 
 
 
07eda8f
 
f09bf0d
 
 
 
 
 
 
 
 
 
 
 
 
 
3fe5cc4
5d5d145
 
 
2eb283b
5d5d145
e66c65d
 
 
5d5d145
e66c65d
 
 
c8ca665
e66c65d
f09bf0d
 
 
 
 
 
3fe5cc4
e66c65d
 
3fe5cc4
5d5d145
e66c65d
3fe5cc4
e66c65d
 
 
07eda8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import streamlit as st
from deepface import DeepFace
import requests
from PIL import Image
from gtts import gTTS
import os
import cv2
import numpy as np

# Constants
KNOWN_FOLDER = "known_faces"
ESP32_SERVER_URL = "https://esp32-upload-server.onrender.com"
MODEL_NAME = "ArcFace"
DETECTOR_BACKEND = "retinaface"

# Streamlit setup
st.set_page_config(page_title="Second Eye - Enhanced Recognition", layout="centered")
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Face Recognition"])

# Fetch image from ESP32
def get_latest_image():
    try:
        r = requests.get(f"{ESP32_SERVER_URL}/latest")
        if r.status_code != 200:
            return None
        filename = r.json()["filename"]
        return f"{ESP32_SERVER_URL}/uploads/{filename}"
    except:
        return None

# Preprocess image for better recognition
def preprocess_image(image_path):
    img = cv2.imread(image_path)
    if img is None:
        return image_path

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    enhanced = clahe.apply(gray)

    gamma = 1.5
    lookUpTable = np.array([((i / 255.0) ** gamma) * 255 for i in range(256)]).astype("uint8")
    gamma_corrected = cv2.LUT(enhanced, lookUpTable)
    final_img = cv2.cvtColor(gamma_corrected, cv2.COLOR_GRAY2BGR)

    output_path = "preprocessed.jpg"
    cv2.imwrite(output_path, final_img)
    return output_path

# Detect faces
def is_face_detected(image_path):
    try:
        faces = DeepFace.extract_faces(
            img_path=image_path,
            enforce_detection=False,
            detector_backend=DETECTOR_BACKEND
        )
        return len(faces) > 0
    except:
        return False

# Compare with known faces
def compare_with_known_faces(unknown_img_path):
    for filename in os.listdir(KNOWN_FOLDER):
        if not filename.lower().endswith((".jpg", ".jpeg", ".png")):
            continue
        known_img_path = os.path.join(KNOWN_FOLDER, filename)
        try:
            result = DeepFace.verify(
                img1_path=unknown_img_path,
                img2_path=known_img_path,
                model_name=MODEL_NAME,
                detector_backend=DETECTOR_BACKEND,
                enforce_detection=False
            )
            if result["verified"]:
                return filename.split('.')[0]
        except Exception as e:
            print(f"Comparison failed with {filename}: {e}")
    return None

# Main UI
if page == "Face Recognition":
    st.title("Second Eye - Enhanced Face Recognition")

    if st.button("Check for New Image"):
        image_url = get_latest_image()
        if image_url:
            st.image(image_url, caption="Captured Image", use_container_width=True)

            response = requests.get(image_url)
            with open("latest.jpg", "wb") as f:
                f.write(response.content)

            processed_img_path = preprocess_image("latest.jpg")

            if is_face_detected(processed_img_path):
                match = compare_with_known_faces(processed_img_path)
                if match:
                    st.success(f"โœ… Match found: {match}")
                    tts = gTTS(f"Match found: {match}")
                else:
                    st.error("โŒ No match found")
                    tts = gTTS("No match found")
            else:
                st.warning("๐Ÿ˜• No face detected in the image.")
                tts = gTTS("No face detected")

            tts.save("result.mp3")
            st.audio("result.mp3", autoplay=True)
        else:
            st.warning("No image found on ESP32 server")