-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataCollect.py
63 lines (42 loc) · 1.35 KB
/
DataCollect.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
import cv2
import os
import dlib
import face_recognition
import numpy as np
# cam = cv2.VideoCapture(0)
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# For each person, enter one numeric face id
face_name = input('\n Name : ')
dirName = 'Training/' + face_name
try:
# Create target Directory
os.mkdir(dirName)
print("Directory ", dirName, " Created ")
except FileExistsError:
print("Directory ", dirName, " already exists")
print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face count
count = 0
while (True):
ret, img = cam.read()
faces = face_detector.detectMultiScale(img, scaleFactor=1.3,minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
count += 1
print(count)
cv2.imwrite(
"Training/" + str(face_name) +"/"+ str(count) + ".jpg",
img[y:y + h, x:x + w])
cv2.imshow('image', img)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
elif count >= 200:
break
# Do a bit of cleanup
print("\n Exiting Program")
cam.release()
cv2.destroyAllWindows()