-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_data.py
60 lines (34 loc) · 1.41 KB
/
face_data.py
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
import cv2
import os
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
output_dir = 'dataset'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
cap = cv2.VideoCapture(0)
print("Collecting images. Press 'q' to quit.")
count = 0
user_id = input("Enter user ID: ")
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
face = gray[y:y+h, x:x+w]
color_face = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(face)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(color_face, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
if len(eyes) >= 2:
face_filename = os.path.join(output_dir, f'user.{user_id}.{count}.jpg')
cv2.imwrite(face_filename, face)
print(f"Image {count} saved.")
count += 1
cv2.imshow('Collecting Images', frame)
if cv2.waitKey(1) & 0xFF == ord('q') or count >= 10:
break
cap.release()
cv2.destroyAllWindows()