-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset-creation.py
43 lines (31 loc) · 1.18 KB
/
dataset-creation.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
import cv2
import os
# Load Haar Cascade for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Create a dataset folder
dataset_path = 'dataset'
os.makedirs(dataset_path, exist_ok=True)
def capture_face_images(user_name):
cap = cv2.VideoCapture(0)
img_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img_count += 1
face_img = frame[y:y + h, x:x + w]
# Save the face image in the dataset folder
cv2.imwrite(f"{dataset_path}/{user_name}_{img_count}.jpg", face_img)
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow("Capturing Face", frame)
if cv2.waitKey(1) == 27 or img_count >= 10: # Esc key or 10 images limit
break
cap.release()
cv2.destroyAllWindows()
print(f"Captured {img_count} images for {user_name}")
# Usage
user_name = input("Enter your name: ")
capture_face_images(user_name)