Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
133 lines (108 loc) · 4.51 KB
/
Copy pathscript.py
File metadata and controls
133 lines (108 loc) · 4.51 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import cv2
from deepface import DeepFace
import time
import numpy as np
import os
# Set the path to the database of known persons
database_path = r'--your directory for faces--'
# Create a dictionary to store face encodings
face_db = {}
print("Starting to process the face database...")
start_time = time.time()
# Process each person's images in the database
for person_name in os.listdir(database_path):
person_path = os.path.join(database_path, person_name)
if os.path.isdir(person_path):
print(f"Processing {person_name}...")
encodings = []
for img_name in os.listdir(person_path):
img_path = os.path.join(person_path, img_name)
print(f"Processing image {img_name}...")
try:
# Detect and encode the face
face_objs = DeepFace.extract_faces(img_path, detector_backend='yolov8', enforce_detection=False)
for face in face_objs:
encoding = DeepFace.represent(face['face'], model_name='VGG-Face', enforce_detection=False)[0][
"embedding"]
encodings.append(encoding)
except Exception as e:
print(f"Error processing image {img_name}: {e}")
# Store encodings in the dictionary
face_db[person_name] = encodings
print(f"Finished processing {person_name}")
print(f"Finished processing all faces. Total persons processed: {len(face_db)}")
def recognize_face(face_img, face_db, threshold=0.35):
"""
Recognize the face from the face database.
Args:
face_img (np.array): Image of the face to be recognized.
face_db (dict): Dictionary of known face encodings.
threshold (float): Distance threshold for face recognition.
Returns:
str: Name of the recognized person or "Unknown".
"""
try:
encoding = DeepFace.represent(face_img, model_name='VGG-Face', enforce_detection=False)[0]["embedding"]
distances = {}
for person_name, encodings in face_db.items():
dist = np.min([np.linalg.norm(np.array(encoding) - np.array(enc)) for enc in encodings])
distances[person_name] = dist
if distances:
min_dist = min(distances.values())
print(min(distances, key=distances.get), min_dist)
if min_dist > threshold:
return min(distances, key=distances.get)
else:
return "Unknown"
else:
return "Unknown"
except Exception as e:
print(f"Error in face recognition: {e}")
return "Unknown"
# Initialize webcam capture
cap = cv2.VideoCapture(0)
cv2.startWindowThread()
# Check if the webcam is opened correctly
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
last_recognition_time = 0
recognition_interval = 0 # seconds
recognized_faces = {}
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
current_time = time.time()
try:
# Use DeepFace to detect faces with YOLOv8
faces = DeepFace.extract_faces(frame, detector_backend='yolov8', enforce_detection=False)
# Draw rectangles around the detected faces
for face in faces:
x, y, w, h = face['facial_area']['x'], face['facial_area']['y'], face['facial_area']['w'], \
face['facial_area']['h']
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 0), 2)
# Extract face for recognition
face_img = frame[y:y + h, x:x + w]
# Perform face recognition
if current_time - last_recognition_time >= recognition_interval:
name = recognize_face(face_img, face_db)
recognized_faces[(x, y, w, h)] = name
last_recognition_time = current_time
# Display the name
if (x, y, w, h) in recognized_faces:
name = recognized_faces[(x, y, w, h)]
cv2.putText(frame, f'Name: {name}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
except Exception as e:
print(f"Error in face detection: {e}")
# Display the resulting frame
cv2.imshow('Webcam Feed', frame)
# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
# Release the webcam
cap.release()
cv2.destroyAllWindows()