-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlibFaceTracking.py
169 lines (133 loc) · 5.48 KB
/
libFaceTracking.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
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
import sys
import urllib
from pyopencv import *
from apexisconst import *
class FaceTracking:
def __init__(self, ip, port, username, password):
self.ip = ip
self.port = port
self.username = username
self.password = password
capture = VideoCapture()
frame = Mat()
scaleOpt = "--scale="
cascadeOpt = "--cascade="
nestedCascadeOpt = "--nested-cascade"
nestedCascadeOptLen = len(nestedCascadeOpt)
inputName = ""
cascade = CascadeClassifier()
nestedCascade = CascadeClassifier()
scale = 1.0
cascade.load( cascadeName )
capture.open( "http://%s:%d/videostream.cgi?user=%s&pwd=%s" % (self.ip, self.port, self.username, self.password) )
if capture.isOpened():
while True:
capture >> frame
if frame.empty():
break
self.detectAndTracking( frame, cascade, nestedCascade, scale )
if waitKey( 10 ) >= 0:
break
setPosition( 31 )
def setPosition(self, cmd ):
f = urllib.urlopen( ( "http://%s:%d/decoder_control.cgi?user=%s&pwd=%s&command=%d" ) % (self.ip, self.port, self.username, self.password, cmd) )
s = f.read()
f.close()
def detectAndTracking(self, img, cascade, nestedCascade, scale):
i = 0
t = 0.0
colors = ( CV_RGB(0,0,255),
CV_RGB(0,128,255),
CV_RGB(0,255,255),
CV_RGB(0,255,0),
CV_RGB(255,128,0),
CV_RGB(255,255,0),
CV_RGB(255,0,0),
CV_RGB(255,0,255))
gray = Mat()
smallImg = Mat( Size(round(img.cols/scale), round(img.rows/scale)), CV_8UC1 )
cvtColor( img, gray, CV_BGR2GRAY )
resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR )
equalizeHist( smallImg, smallImg )
t = getTickCount()
faces = cascade.detectMultiScale( smallImg,
1.1, 2, 0
#|CascadeClassifier.FIND_BIGGEST_OBJECT
#|CascadeClassifier.DO_ROUGH_SEARCH
|CascadeClassifier.SCALE_IMAGE
,
Size(30, 30) )
t = getTickCount() - t
print( "detection time = %lf ms\n" % (t/(getTickFrequency()*1000.)) )
for i in range(len(faces)):
r = faces[i]
color = colors[i%8]
center = Point(round((r.x + r.width*0.5)*scale), round((r.y + r.height*0.5)*scale))
radius = round((r.width + r.height)*0.25*scale)
circle( img, center, radius, color, 3, 8, 0 )
#move left
if center.x < ( ( img.cols / 2 ) - radius ):
setPosition( PAN_LEFT )
print "Left"
CMD = PAN_LEFT
#move right
elif center.x > ( ( img.cols / 2) + radius ):
setPosition( PAN_RIGHT )
print "Right"
CMD = PAN_RIGHT
#is center position
elif center.x >= ( ( img.cols / 2 ) - radius ) and center.x <= ( ( img.cols / 2) + radius ):
print "Center"
setPosition( PAN_LEFT_STOP )
setPosition( PAN_RIGHT_STOP )
CMD = 0
#break
elif CMD == 0:
setPosition( PAN_LEFT_STOP )
setPosition( PAN_RIGHT_STOP )
if CMD == 0:
#move up
if center.y < ( ( img.rows / 2 ) - radius ):
setPosition( TILT_UP )
print "Up"
CMD = TILT_UP
#move down
elif center.y > ( ( img.rows / 2 ) + radius ) - 50:
setPosition( TILT_DOWN )
print "Down"
CMD = TILT_DOWN
#is middle position
elif center.y >= ( ( img.rows / 2 ) - radius ) and center.y <= ( ( ( img.rows / 2) + radius ) - 50 ):
print "Middle"
setPosition( TILT_UP_STOP )
setPosition( TILT_DOWN_STOP )
CMD = 0
#break
elif CMD == 0:
setPosition( TILT_UP_STOP )
setPosition( TILT_DOWN_STOP )
if nestedCascade.empty():
continue
"""
smallImgROI = smallImg(r)
nestedObjects = nestedCascade.detectMultiScale( smallImgROI,
1.1, 2, 0
#|CascadeClassifier.FIND_BIGGEST_OBJECT
#|CascadeClassifier.DO_ROUGH_SEARCH
#|CascadeClassifier.DO_CANNY_PRUNING
|CascadeClassifier.SCALE_IMAGE
,
Size(30, 30) )
for nr in nestedObjects:
center = Point(round((r.x + nr.x + nr.width*0.5)*scale), round((r.y + nr.y + nr.height*0.5)*scale))
radius = round((nr.width + nr.height)*0.25*scale)
circle( img, center, radius, color, 3, 8, 0 )
"""
if len(faces) <= 0:
setPosition( 31 )
#setPosition( PAN_LEFT_STOP )
#setPosition( PAN_RIGHT_STOP )
#setPosition( TILT_UP_STOP )
#setPosition( TILT_DOWN_STOP )
#imshow( "result", img )