Skip to content

Commit a2fe3f4

Browse files
author
AdamMiltonBarker
committed
[Intel-Movidius/TASS] Enhanced Facenet streaming quality
1 parent 9677bfe commit a2fe3f4

File tree

2 files changed

+116
-111
lines changed

2 files changed

+116
-111
lines changed

Intel-Movidius/TASS/Facenet/WClassifier.py

Lines changed: 114 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Title: TASS Movidius Facenet WebCam Classifier
33
# Description: Connects to a local webcam or stream for realtime face recognition.
44
# Acknowledgements: Uses code from Intel movidius/ncsdk (https://github.com/movidius/ncsdk)
5-
# Last Modified: 2018-05-21
5+
# Last Modified: 2018-05-16
66
############################################################################################
77

88
############################################################################################
@@ -51,79 +51,79 @@
5151
capture=None
5252

5353
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({
112112
"locationID": self._configs["IoTJumpWay"]["Location"],
113113
"zoneID": self._configs["IoTJumpWay"]["Zone"],
114114
"deviceId": self._configs["IoTJumpWay"]["Device"],
115115
"deviceName": self._configs["IoTJumpWay"]["DeviceName"],
116116
"username": self._configs["IoTJumpWayMQTT"]["MQTTUsername"],
117117
"password": self._configs["IoTJumpWayMQTT"]["MQTTPassword"]
118118
})
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")
127127

128128
class CamHandler(BaseHTTPRequestHandler):
129129
def do_GET(self):
@@ -141,7 +141,7 @@ def do_GET(self):
141141
# to have a maximum width of 400 pixels
142142
frame = capture.read()
143143
frame = imutils.resize(frame, width=640)
144-
rawFrame = frame
144+
rawFrame = frame.copy()
145145

146146
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
147147
rects = Classifier.detector(gray, 0)
@@ -163,50 +163,53 @@ def do_GET(self):
163163
for (x, y) in shape:
164164
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
165165

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"]
174173

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):
180175

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))):
197181

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
207195
else:
208-
print("-- NO VALID ID")
196+
print("-- NO MATCH")
209197
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("")
210213

211214
imgRGB=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
212215
imgRGB = cv2.flip(imgRGB, 1)
@@ -226,11 +229,13 @@ def do_GET(self):
226229
exit
227230
return
228231
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)
229234
self.send_response(200)
230235
self.send_header('Content-type','text/html')
231236
self.end_headers()
232237
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())
234239
self.wfile.write('</body></html>'.encode())
235240
return
236241

@@ -256,7 +261,7 @@ def main():
256261
sys.exit()
257262

258263
try:
259-
server = ThreadedHTTPServer(('192.168.1.44', 8080), CamHandler)
264+
server = ThreadedHTTPServer((Classifier._configs["Cameras"][0]["Stream"], Classifier._configs["Cameras"][0]["StreamPort"]), CamHandler)
260265
print("server started")
261266
server.serve_forever()
262267
except KeyboardInterrupt:

Intel-Movidius/TASS/Facenet/tools/Facenet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
############################################################################################
22
# Title: Facenet Helpers
33
# Description: Helper functions for Facenet.
4-
# Last Modified: 2018-05-21
4+
# Last Modified: 2018/02/21
55
############################################################################################
66

77
import os, json, cv2
@@ -49,7 +49,7 @@ def match(self, face1_output, face2_output):
4949
total_diff += this_diff
5050
print('-- Total Difference is: ' + str(total_diff))
5151

52-
if (total_diff < 1.2):
52+
if (total_diff < 1.3):
5353
# the total difference between the two is under the threshold so
5454
# the faces match.
5555
return True

0 commit comments

Comments
 (0)