-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
create_dataset.py
81 lines (71 loc) · 2.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import cv2
import os
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("Face Detection", 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 > 300: #take 300 frames
break
cv2.destroyAllWindows()
return num_of_images
#take frames by extract a video
def take_video(name, video):
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(video)
if not vid.isOpened():
print("Error: Could not open video file.")
exit()
num_of_images = 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)
if not ret:
break # Break the loop if no more frames are available
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("Face Detection", 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 > 300: #take 300 frames
break
vid.release()
cv2.destroyAllWindows()
return num_of_images
#take_video('tho1', 'data\WIN_20230920_07_56_11_Pro.mp4')