2
2
# Title: TASS Movidius Facenet WebCam Classifier
3
3
# Description: Connects to a local webcam or stream for realtime face recognition.
4
4
# Acknowledgements: Uses code from Intel movidius/ncsdk (https://github.com/movidius/ncsdk)
5
- # Last Modified: 2018-05-21
5
+ # Last Modified: 2018-05-16
6
6
############################################################################################
7
7
8
8
############################################################################################
51
51
capture = None
52
52
53
53
class Classifier ():
54
-
55
- def __init__ (self ):
56
-
57
- self ._configs = {}
58
- self .movidius = None
59
- self .jumpwayClient = None
60
- self .OpenCVCapture = None
61
-
62
- self .graphfile = None
63
- self .graph = None
64
-
65
- self .CheckDevices ()
66
- self .Helpers = Helpers ()
67
- self .OpenCVHelpers = OpenCVHelpers ()
68
- self .FacenetHelpers = FacenetHelpers ()
69
- self ._configs = self .Helpers .loadConfigs ()
70
- self .loadRequirements ()
71
- self .startMQTT ()
72
-
73
- self .detector = dlib .get_frontal_face_detector ()
74
- self .predictor = dlib .shape_predictor (self ._configs ["ClassifierSettings" ]["Dlib" ])
75
-
76
- print ("" )
77
- print ("-- Classifier Initiated" )
78
- print ("" )
79
-
80
- def CheckDevices (self ):
81
-
82
- #mvnc.SetGlobalOption(mvnc.GlobalOption.LOGLEVEL, 2)
83
- devices = mvnc .EnumerateDevices ()
84
- if len (devices ) == 0 :
85
- print ('!! WARNING! No Movidius Devices Found !!' )
86
- quit ()
87
-
88
- self .movidius = mvnc .Device (devices [0 ])
89
- self .movidius .OpenDevice ()
90
-
91
- print ("-- Movidius Connected" )
92
-
93
- def allocateGraph (self ,graphfile ):
94
-
95
- self .graph = self .movidius .AllocateGraph (graphfile )
96
-
97
- def loadRequirements (self ):
98
-
99
- with open (self ._configs ["ClassifierSettings" ]["NetworkPath" ] + self ._configs ["ClassifierSettings" ]["Graph" ], mode = 'rb' ) as f :
100
-
101
- self .graphfile = f .read ()
102
-
103
- self .allocateGraph (self .graphfile )
104
-
105
- print ("-- Allocated Graph OK" )
106
-
107
- def startMQTT (self ):
108
-
109
- try :
110
-
111
- self .jumpwayClient = JWMQTTdevice .DeviceConnection ({
54
+
55
+ def __init__ (self ):
56
+
57
+ self ._configs = {}
58
+ self .movidius = None
59
+ self .jumpwayClient = None
60
+ self .OpenCVCapture = None
61
+
62
+ self .graphfile = None
63
+ self .graph = None
64
+
65
+ self .CheckDevices ()
66
+ self .Helpers = Helpers ()
67
+ self .OpenCVHelpers = OpenCVHelpers ()
68
+ self .FacenetHelpers = FacenetHelpers ()
69
+ self ._configs = self .Helpers .loadConfigs ()
70
+ self .loadRequirements ()
71
+ self .startMQTT ()
72
+
73
+ self .detector = dlib .get_frontal_face_detector ()
74
+ self .predictor = dlib .shape_predictor (self ._configs ["ClassifierSettings" ]["Dlib" ])
75
+
76
+ print ("" )
77
+ print ("-- Classifier Initiated" )
78
+ print ("" )
79
+
80
+ def CheckDevices (self ):
81
+
82
+ #mvnc.SetGlobalOption(mvnc.GlobalOption.LOGLEVEL, 2)
83
+ devices = mvnc .EnumerateDevices ()
84
+ if len (devices ) == 0 :
85
+ print ('!! WARNING! No Movidius Devices Found !!' )
86
+ quit ()
87
+
88
+ self .movidius = mvnc .Device (devices [0 ])
89
+ self .movidius .OpenDevice ()
90
+
91
+ print ("-- Movidius Connected" )
92
+
93
+ def allocateGraph (self ,graphfile ):
94
+
95
+ self .graph = self .movidius .AllocateGraph (graphfile )
96
+
97
+ def loadRequirements (self ):
98
+
99
+ with open (self ._configs ["ClassifierSettings" ]["NetworkPath" ] + self ._configs ["ClassifierSettings" ]["Graph" ], mode = 'rb' ) as f :
100
+
101
+ self .graphfile = f .read ()
102
+
103
+ self .allocateGraph (self .graphfile )
104
+
105
+ print ("-- Allocated Graph OK" )
106
+
107
+ def startMQTT (self ):
108
+
109
+ try :
110
+
111
+ self .jumpwayClient = JWMQTTdevice .DeviceConnection ({
112
112
"locationID" : self ._configs ["IoTJumpWay" ]["Location" ],
113
113
"zoneID" : self ._configs ["IoTJumpWay" ]["Zone" ],
114
114
"deviceId" : self ._configs ["IoTJumpWay" ]["Device" ],
115
115
"deviceName" : self ._configs ["IoTJumpWay" ]["DeviceName" ],
116
116
"username" : self ._configs ["IoTJumpWayMQTT" ]["MQTTUsername" ],
117
117
"password" : self ._configs ["IoTJumpWayMQTT" ]["MQTTPassword" ]
118
118
})
119
-
120
- except Exception as e :
121
- print (str (e ))
122
- sys .exit ()
123
-
124
- self .jumpwayClient .connectToDevice ()
125
-
126
- print ("-- IoT JumpWay Initiated" )
119
+
120
+ except Exception as e :
121
+ print (str (e ))
122
+ sys .exit ()
123
+
124
+ self .jumpwayClient .connectToDevice ()
125
+
126
+ print ("-- IoT JumpWay Initiated" )
127
127
128
128
class CamHandler (BaseHTTPRequestHandler ):
129
129
def do_GET (self ):
@@ -141,7 +141,7 @@ def do_GET(self):
141
141
# to have a maximum width of 400 pixels
142
142
frame = capture .read ()
143
143
frame = imutils .resize (frame , width = 640 )
144
- rawFrame = frame
144
+ rawFrame = frame . copy ()
145
145
146
146
gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
147
147
rects = Classifier .detector (gray , 0 )
@@ -163,50 +163,53 @@ def do_GET(self):
163
163
for (x , y ) in shape :
164
164
cv2 .circle (frame , (x , y ), 1 , (0 , 255 , 0 ), - 1 )
165
165
166
- if frameWait >= 20 :
167
-
168
- frameWait = 0
169
- currentFace = rawFrame [y :y + h , x :x + w ]
170
-
171
- validDir = Classifier ._configs ["ClassifierSettings" ]["NetworkPath" ] + Classifier ._configs ["ClassifierSettings" ]["ValidPath" ]
172
-
173
- for valid in os .listdir (validDir ):
166
+ frameWait = 0
167
+ currentFace = rawFrame [
168
+ max (0 , rect .top ()- 100 ): min (rect .bottom ()+ 100 , 480 ),
169
+ max (0 , rect .left ()- 100 ): min (rect .right ()+ 100 , 640 )]
170
+ cv2 .imwrite ("test.jpg" ,currentFace )
171
+
172
+ validDir = Classifier ._configs ["ClassifierSettings" ]["NetworkPath" ] + Classifier ._configs ["ClassifierSettings" ]["ValidPath" ]
174
173
175
- if valid .endswith ('.jpg' ) or valid .endswith ('.jpeg' ) or valid .endswith ('.png' ) or valid .endswith ('.gif' ):
176
-
177
- if (FacenetHelpers .match (
178
- FacenetHelpers .infer (cv2 .imread (validDir + valid ), Classifier .graph ),
179
- FacenetHelpers .infer (currentFace , Classifier .graph ))):
174
+ for valid in os .listdir (validDir ):
180
175
181
- name = valid .rsplit ('.' , 1 )[0 ]
182
- print ("-- MATCH " + name )
183
- print ("" )
184
- Classifier .jumpwayClient .publishToDeviceChannel (
185
- "Warnings" ,
186
- {
187
- "WarningType" :"CCTV" ,
188
- "WarningOrigin" : Classifier ._configs ["Cameras" ][0 ]["ID" ],
189
- "WarningValue" : "RECOGNISED" ,
190
- "WarningMessage" :name + " Detected"
191
- }
192
- )
193
- break
194
- else :
195
- print ("-- NO MATCH" )
196
- print ("" )
176
+ if valid .endswith ('.jpg' ) or valid .endswith ('.jpeg' ) or valid .endswith ('.png' ) or valid .endswith ('.gif' ):
177
+
178
+ if (FacenetHelpers .match (
179
+ FacenetHelpers .infer (cv2 .imread (validDir + valid ), Classifier .graph ),
180
+ FacenetHelpers .infer (currentFace , Classifier .graph ))):
197
181
198
- Classifier .jumpwayClient .publishToDeviceChannel (
199
- "Warnings" ,
200
- {
201
- "WarningType" :"CCTV" ,
202
- "WarningOrigin" : Classifier ._configs ["Cameras" ][0 ]["ID" ],
203
- "WarningValue" : "INTRUDER" ,
204
- "WarningMessage" :"INTRUDER"
205
- }
206
- )
182
+ name = valid .rsplit ('.' , 1 )[0 ]
183
+ print ("-- MATCH " + name )
184
+ print ("" )
185
+ Classifier .jumpwayClient .publishToDeviceChannel (
186
+ "Warnings" ,
187
+ {
188
+ "WarningType" :"CCTV" ,
189
+ "WarningOrigin" : Classifier ._configs ["Cameras" ][0 ]["ID" ],
190
+ "WarningValue" : "RECOGNISED" ,
191
+ "WarningMessage" :name + " Detected"
192
+ }
193
+ )
194
+ break
207
195
else :
208
- print ("-- NO VALID ID " )
196
+ print ("-- NO MATCH " )
209
197
print ("" )
198
+
199
+ cv2 .rectangle (frame , (x , y ), (x + w , y + h ), (255 , 0 , 0 ), 2 )
200
+
201
+ Classifier .jumpwayClient .publishToDeviceChannel (
202
+ "Warnings" ,
203
+ {
204
+ "WarningType" :"CCTV" ,
205
+ "WarningOrigin" : Classifier ._configs ["Cameras" ][0 ]["ID" ],
206
+ "WarningValue" : "INTRUDER" ,
207
+ "WarningMessage" :"INTRUDER"
208
+ }
209
+ )
210
+ else :
211
+ print ("-- NO VALID ID" )
212
+ print ("" )
210
213
211
214
imgRGB = cv2 .cvtColor (frame ,cv2 .COLOR_BGR2RGB )
212
215
imgRGB = cv2 .flip (imgRGB , 1 )
@@ -226,11 +229,13 @@ def do_GET(self):
226
229
exit
227
230
return
228
231
if self .path .endswith ('.html' ):
232
+ src = '<img src="http://' + Classifier ._configs ["Cameras" ][0 ]["Stream" ]+ ':' + str (Classifier ._configs ["Cameras" ][0 ]["StreamPort" ])+ '/cam.mjpg" />'
233
+ print (src )
229
234
self .send_response (200 )
230
235
self .send_header ('Content-type' ,'text/html' )
231
236
self .end_headers ()
232
237
self .wfile .write ('<html><head></head><body>' .encode ())
233
- self .wfile .write ('<img src="http://192.168.1.44:8080/cam.mjpg"/>' .encode ())
238
+ self .wfile .write (src .encode ())
234
239
self .wfile .write ('</body></html>' .encode ())
235
240
return
236
241
@@ -256,7 +261,7 @@ def main():
256
261
sys .exit ()
257
262
258
263
try :
259
- server = ThreadedHTTPServer (('192.168.1.44' , 8080 ), CamHandler )
264
+ server = ThreadedHTTPServer ((Classifier . _configs [ "Cameras" ][ 0 ][ "Stream" ], Classifier . _configs [ "Cameras" ][ 0 ][ "StreamPort" ] ), CamHandler )
260
265
print ("server started" )
261
266
server .serve_forever ()
262
267
except KeyboardInterrupt :
0 commit comments