-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dataset.py
50 lines (46 loc) · 1.47 KB
/
create_dataset.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
import os
import cv2
def start_capture(name):
path = "./data/" + name
num_of_images = 0
detector = cv2.CascadeClassifier("./data/haarcascade_frontalface_default.xml")
try:
os.makedirs(path)
except:
print("Directory Already Created")
vid = cv2.VideoCapture(0)
while True:
ret, img = vid.read()
new_img = None
grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = detector.detectMultiScale(image=grayimg, scaleFactor=1.1, minNeighbors=5)
for x, y, w, h in face:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
cv2.putText(
img,
"Face Detected",
(x, y - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
(0, 0, 255),
)
cv2.putText(
img,
str(str(num_of_images) + " images captured"),
(x, y + h + 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
(0, 0, 255),
)
new_img = img[y : y + h, x : x + w]
cv2.imshow("FaceDetection", img)
key = cv2.waitKey(1) & 0xFF
try:
cv2.imwrite(str(path + "/" + str(num_of_images) + name + ".jpg"), new_img)
num_of_images += 1
except:
pass
if key == ord("q") or key == 27 or num_of_images > 310:
break
cv2.destroyAllWindows()
return num_of_images