-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py.save
106 lines (70 loc) · 3 KB
/
generate.py.save
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
print "Initializing ..."
import io
import time
import picamera
import picamera.array
import cv2
import numpy as np
import face
import config
import csv
CAMERA_WIDTH = 640
CAMERA_HEIGHT = 480
stream = io.BytesIO()
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
HAAR_FACES = 'haarcascade_frontalface_alt.xml'
HAAR_SCALE_FACTOR = 1.3
HAAR_MIN_NEIGHBORS = 4
HAAR_MIN_SIZE = (30, 30)
FACE_WIDTH = 92
FACE_HEIGHT = 112
def doCrop(image, x, y, w, h):
"""Crop box defined by x, y (upper left corner) and w, h (width and height)
to an image with the same aspect ratio as the face training data. Might
return a smaller crop if the box is near the edge of the image.
"""
crop_height = int((config.FACE_HEIGHT / float(config.FACE_WIDTH)) * w)
midy = y + h/2
y1 = max(0, midy-crop_height/2)
y2 = min(image.shape[0]-1, midy+crop_height/2)
return image[y1:y2, x:x+w]
def resize(image):
"""Resize a face image to the proper size for training and detection.
"""
return cv2.resize(image,
(FACE_WIDTH, FACE_HEIGHT),
interpolation=cv2.INTER_LANCZOS4)
if __name__ == '__main__':
count = 0
filePaths = list()
with picamera.PiCamera() as camera:
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
while (True):
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr', use_video_port=True)
image = stream.array
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(image,
scaleFactor=HAAR_SCALE_FACTOR,
minNeighbors=HAAR_MIN_NEIGHBORS,
minSize=HAAR_MIN_SIZE,
flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0),2)
crop = resize(doCrop(gray, x, y, w, h))
filePathForFile = ('imagestotrain' % str(count))
cv2.imwrite(filePathForFile, crop)
filePaths.append(filePathForFile)
print filePaths
count += 1
outfile = open('mg.csv', 'w')
cv2.imshow('Face Image', image)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
writer = csv.writer(outfile)
for files in filePaths:
writer.writerow([files,])
outfile.close()
break