forked from nileshbhadana/ML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognition_dataset_creator.py
61 lines (47 loc) · 1.32 KB
/
face_recognition_dataset_creator.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 03:44:34 2018
@author: nilesh
"""
#importing libraries
import cv2,os
#casecading xml file
cascade=cv2.CascadeClassifier("face.xml")
#setting up counter
counter=0
#startiing the camera
cam=cv2.VideoCapture(0)
#defining directory name where images will be stored
dir_name="/home/nilesh/Desktop/ML/dataset_images"
#using try to avoid error when directory is already present
try:
os.mkdir(dir_name)
except:
print()
#startin the loop
while cam.isOpened():
#readin the frame
frame=cam.read()[1]
#converting frame to gray
gray_image=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#reading and detecting faces in the frame
faces=cascade.detectMultiScale(gray_image,1.5,5)
for (x,y,w,h) in faces:
#drawing around the face
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),1)
counter=counter+1
print(counter)
#saving the face images
cv2.imwrite(dir_name+"/image"+str(counter)+".jpg",gray_image)
#showing the frame
cv2.imshow('capturing images',frame)
#handler
if cv2.waitKey(50) & 0xFF==ord('q'):
break
elif cv2.waitKey(50) & counter==20:
break
#releasing the camera
cam.release()
#destroying all windows
cv2.destroyAllWindows()