-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.py
More file actions
69 lines (50 loc) · 2.31 KB
/
Stream.py
File metadata and controls
69 lines (50 loc) · 2.31 KB
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
import cv2
import face_recognition
import numpy as np
import pickle
import requests
from tensorflow.keras.models import load_model
# ESP32-CAM image URL
ESP32_URL = 'http://192.168.20.218/320x240.jpg' # change resolution if needed
# Load label encoder (optional, only if used)
with open("label_encoder.pickle", "rb") as f:
label_encoder = pickle.load(f)
# Load liveness detection model (Keras H5)
liveness_model = load_model("liveness.model.h5")
# Load known face encodings + names
with open("encodings.pickle", "rb") as f:
data = pickle.load(f) # should contain { "encodings": [...], "names": [...] }
known_encodings = data["encodings"]
known_names = data["names"]
cv2.namedWindow("Face + Liveness Detection", cv2.WINDOW_AUTOSIZE)
while True:
try:
resp = requests.get(ESP32_URL, timeout=5)
img_array = np.asarray(bytearray(resp.content), dtype=np.uint8)
frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
if frame is None:
continue
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb)
face_encodings = face_recognition.face_encodings(rgb, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# Only perform face recognition
matches = face_recognition.compare_faces(known_encodings, face_encoding)
face_distances = face_recognition.face_distance(known_encodings, face_encoding)
name = "Unknown"
if any(matches):
best_index = np.argmin(face_distances)
if face_distances[best_index] < 0.5: # threshold
name = known_names[best_index]
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left, top - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left, top - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
cv2.imshow("Face + Liveness Detection", frame)
except Exception as e:
print("Error fetching frame:", e)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cv2.destroyAllWindows()