-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptureFace.py
93 lines (86 loc) · 3.58 KB
/
CaptureFace.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
82
83
84
85
86
87
88
89
90
91
92
93
import os
import sys
import select
import glob
import cv2
import hardware
import config
import face
import UserData
import RPi.GPIO as GPIO
class CapturePose(object):
def ReadCardID(self):
global ID
global camera
global door
global led
User = UserData.UserData()
ID = User.UserID()
camera = config.get_camera()
door =hardware.Door()
led = hardware.Led()
def cekDir(self):
# Create the directory for positive training images if it doesn't exist.
if not os.path.exists(config.TRAINING_DIR):
os.makedirs(config.TRAINING_DIR)
if not os.path.exists(config.POSITIVE_DIR):
os.makedirs(config.POSITIVE_DIR)
if not os.path.exists(config.POSITIVE_DIR + ID):
os.makedirs(config.POSITIVE_DIR + ID)
# Find the largest ID of existing positive images.
# Start new images after this ID value.
def CapturePic(self):
files = sorted(glob.glob(os.path.join(config.POSITIVE_DIR + ID, POSITIVE_FILE_PREFIX + '[0-9][0-9][0-9].pgm')))
count = 0
if len(files) > 0:
# Grab the count from the last filename.
count = int(files[-1][-7:-4])+1
print 'Capturing positive training images.'
print 'Press button or type c (and press enter) to capture an image.'
print 'Press Ctrl-C to quit.'
while True:
if door.is_button_up() or is_letter_input('c'):
led.LedOn()
print 'Capturing image...'
image = camera.read()
# Convert image to grayscale.
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Get coordinates of single face in captured image.
result = face.detect_single(image)
if result is None:
print 'Could not detect single face! Check the image in capture.pgm' \
' to see what was captured and try again with only one face visible.'
continue
x, y, w, h = result
# Crop image as close as possible to desired face aspect ratio.
# Might be smaller if face is near edge of image.
crop = face.crop(image, x, y, w, h)
# Save image to file.
filename = os.path.join(config.POSITIVE_DIR + ID, POSITIVE_FILE_PREFIX + '%03d.pgm' % count)
cv2.imwrite(filename, crop)
print 'Found face and wrote training image', filename
led.LedOff()
count += 1
def ListPose(self):
pose_list = config.LIST_DIR + ID + '.pkl'
folder_path = config.POSITIVE_DIR + ID
List = UserData.UserData()
List.WriteList(pose_list, folder_path)
# Prefix for positive training image filenames.
POSITIVE_FILE_PREFIX = 'positive_'
def is_letter_input(letter):
# Utility function to check if a specific character is available on stdin.
# Comparison is case insensitive.
if select.select([sys.stdin,],[],[],0.0)[0]:
input_char = sys.stdin.read(1)
return input_char.lower() == letter.lower()
return False
if __name__ == '__main__':
try:
CapturePose().ReadCardID()
CapturePose().cekDir()
CapturePose().CapturePic()
except KeyboardInterrupt:
GPIO.cleanup()
execfile('admin.py')
sys.exit()