-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFace_recognition.py
More file actions
46 lines (35 loc) · 1.1 KB
/
Copy pathFace_recognition.py
File metadata and controls
46 lines (35 loc) · 1.1 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
from keras_facenet import FaceNet
import cv2
import numpy as np
from mtcnn import MTCNN
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# Load FaceNet model and MTCNN for face detection
embedder = FaceNet()
detector = MTCNN()
# Open webcam
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Detect faces
faces = detector.detect_faces(frame)
for face in faces:
x, y, width, height = face['box']
x2, y2 = x + width, y + height
# Extract face
face_crop = frame[y:y2, x:x2]
# Convert to FaceNet embedding
face_crop = cv2.resize(face_crop, (160, 160))
face_crop = np.expand_dims(face_crop, axis=0)
embedding = embedder.embeddings(face_crop)
# Draw box
cv2.rectangle(frame, (x, y), (x2, y2), (255, 0, 0), 2)
cv2.putText(frame, "Face Detected", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.imshow("Face Recognition", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()