-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (45 loc) · 1.62 KB
/
main.py
File metadata and controls
59 lines (45 loc) · 1.62 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
import cv2
import os
# Load the Haar Cascade face detector
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# Directory to save detected face images
save_dir = "saved_faces"
os.makedirs(save_dir, exist_ok=True)
# Start the webcam
cap = cv2.VideoCapture(0)
# List to store coordinates of already saved faces
saved_faces_coords = []
# Function to check if a face is already saved (based on coordinates)
def is_new_face(x, y, w, h):
for (sx, sy, sw, sh) in saved_faces_coords:
if abs(x - sx) < 20 and abs(y - sy) < 20:
return False
return True
face_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
# Save only new faces
if is_new_face(x, y, w, h):
face_count += 1
face_img = frame[y:y+h, x:x+w]
face_path = os.path.join(save_dir, f"face_{face_count}.png")
cv2.imwrite(face_path, face_img)
saved_faces_coords.append((x, y, w, h))
print(f"[+] Saved new face: {face_path}")
# Draw rectangle around face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Display the webcam feed
cv2.imshow("Face Detection", frame)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()