-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollecting_images.py
45 lines (35 loc) · 1.11 KB
/
collecting_images.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
import cv2
import pickle
import os
import time
import pathlib
directory = pathlib.Path(__file__).parent.absolute()
face = cv2.CascadeClassifier(f'{directory}/haarcascade_frontalface_alt2.xml')
img_dir = "Images/"
name = input('enter you name: ')
label = name
font = cv2.FONT_HERSHEY_PLAIN
video = cv2.VideoCapture(0)
if os.path.isdir(f'{directory}/Images/{label}'):
print("already exists")
else:
os.mkdir(f'{directory}/Images/{label}')
a = 1
while(True):
if a <= 20:
ret,frame = video.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face.detectMultiScale(gray,scaleFactor = 1.5,minNeighbors = 5)
for (x,y,w,h) in (faces):
if (x,y,w,h) != (0,0,0,0) and a <= 20:
cv2.imwrite(f'{directory}/Images/{label}/{a}.jpg',frame)
cv2.imshow('face',frame)
a += 1
time.sleep(0.5)
else:
break
else:
time.sleep(1)
break
print("done")
# Display the resulting frame