/
ELEC-FaceCloud.py
executable file
·160 lines (119 loc) · 3.68 KB
/
ELEC-FaceCloud.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python
import sys
import os
import cv2.cv as cv
import cv2
from optparse import OptionParser
import numpy as np
import RPi.GPIO as GPIO
import time
import smtplib
import FolderActions as f
import IO
from common import clock
min_size = (20, 20)
image_scale = 2
haar_scale = 1.2
min_neighbors = 2
haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING
noface = 0
current_time = 0
dailyfolder = 0
capture = 0
cascade = 0
#size of video
width = 160
height = 120
frame_copy = 0
def detect_and_draw(img, cascade):
# allocate temporary images
#gray = cv.CreateImage((img.width,img.height), 8, 1)
global haar_scale
global min_neighbors
for image_scale in range(1,5,1):
gray = cv.CreateImage((img.width,img.height), 8, 1)
small_img = cv.CreateImage((cv.Round(img.width / image_scale),cv.Round (img.height / image_scale)), 8, 1)
global noface
global current_time
global dailyfolder
current_time=time.strftime("%H:%M:%S")
dailyfolder=time.strftime("%F")
FileName="/Detected-Faces/"
FileName=dailyfolder+FileName+current_time+"_Image_Scale_"+str(image_scale)+ "_Min_Neighbors_" + str(min_neighbors) +".jpeg"
# convert color input image to grayscale
cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
# scale input image for faster processing
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
cv.EqualizeHist(small_img, small_img)
if(cascade):
t = clock()
faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),haar_scale, min_neighbors, haar_flags, min_size)
t = clock() -t
if faces:
for ((x, y, w, h), n) in faces:
# the input to cv.HaarDetectObjects was resized, so scale the
# bounding box of each face and convert it to two CvPoints
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
#### DEBUG ###
#IO.LEDON()
#wait(10)
#IO.LEDOFF()
#save image update log
cv.SaveImage(FileName,img)
f.updatelog(t,image_scale,min_neighbors)
del(gray)
del(small_img)
else:
del(gray)
del(small_img)
def USBCameraSetup():
global capture
global cascade
global width
global height
global frame_copy
cascade = cv.Load('face.xml')
capture = cv.CreateCameraCapture(0)
if not capture :
print "Error loading camera"
if width is None:
width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
else:
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)
if height is None:
height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
else:
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height)
if capture:
frame_copy = None
if __name__=='__main__':
#status LED connects to gpio pin 24
#can use status LED for debugging
#or varification of a positive face ID
# how to turn LED on/off
# IO.LEDON()
# IO.LEDOFF()
USBCameraSetup()
IO.SetupGPIOPins()
f.makedailyfolder()
f.newlog()
global noface
global capture
global frame_copy
global cascade
while True:
frame = cv.QueryFrame(capture)
if not frame:
cv.WaitKey(0)
break
if not frame_copy:
frame_copy = cv.CreateImage((frame.width,frame.height),cv.IPL_DEPTH_8U, frame.nChannels)
if frame.origin == cv.IPL_ORIGIN_TL:
cv.Copy(frame, frame_copy)
else:
cv.Flip(frame, frame_copy, 0)
IO.LEDON()
detect_and_draw(frame_copy, cascade)
IO.LEDOFF()