-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.py
61 lines (40 loc) · 1.39 KB
/
capture.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
import glob
import os
import sys
import select
import cv2
import face
import io
import picamera
import numpy
import config
POSITIVE_FILE_PREFIX = 'positive_'
##### CAPTURE IMAGE WITH PICAM
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (92, 112)
camera.capture(stream, format='jpeg')
#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
#Now creates an OpenCV image
image = cv2.imdecode(buff, 1)
#### CONVERT IMAGE TO GRAYSCALE
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#### DEFINING HAAR CASCADE TO FIND THE FACE
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')
#### CHEKING FOR THE FACE AND LOCATING IT
faces = face_cascade.detectMultiScale(image, 1.1, 5)
count = 0
if not os.path.exists(config.POSITIVE_DIR):
os.makedirs(config.POSITIVE_DIR)
# Find the largest ID of existing positive images.
# Start new images after this ID value.
files = sorted(glob.glob(os.path.join(config.POSITIVE_DIR,
POSITIVE_FILE_PREFIX + '[0-9][0-9][0-9].pgm')))
if len(files) > 0:
# Grab the count from the last filename.
count = int(files[-1][-7:-4])+1
filename = os.path.join(config.POSITIVE_DIR, POSITIVE_FILE_PREFIX + '%03d.pgm' % count)
cv2.imwrite(filename, image)
print 'Found face and wrote training image', filename
count += 1