-
Notifications
You must be signed in to change notification settings - Fork 3
/
InceptionFlow.py
220 lines (148 loc) · 7.66 KB
/
InceptionFlow.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# *****************************************************************************
# InceptionFlow
# Copyright (c) 2018 Adam Milton-Barker - AdamMiltonBarker.com
# Based on Google's Tensorflow Imagenet Inception V3
# Uses TechBubble IoT JumpWay MQTT Client
# *****************************************************************************
import sys
import os
import time
import json
from datetime import datetime
import techbubbleiotjumpwaymqtt.device as IoTJumpWayDevice
import techbubbleiotjumpwaymqtt.application as IoTJumpWayApplication
import InceptionFlow
import cv2
class InceptionFlowCore():
def __init__(self):
"""
CLASSIFIER MODE:
Classifier configuration can be found in data/confs.json
- CustomCam: This mode sets the program to monitoring the camera for custom objects
- CustomTrain: This mode sets the program to to train using the provided custom training data
- CustomTest: This mode sets the program to test custom object detection using the provided custom testing data
- FacialCam: This mode sets the program to monitoring the camera for faces
- FacialTrain: This mode sets the program to to train using the provided faces training data
- FacialTest: This mode sets the program to test custom object detection using the provided custom testing data
- ObjectCam: This mode sets the program to monitoring the camera for objects
- ObjectTest: This mode sets the program to test object detection using images in the objects testing folder
"""
self.confs = {}
with open('data/confs.json') as confs:
self.confs = json.loads(confs.read())
self.startMQTT()
self.InceptionFlow = InceptionFlow.InceptionFlow()
self.InceptionFlow.checkModelDownload()
if self.confs["ClassifierSettings"]["MODE"] == "ObjectCam" or self.confs["ClassifierSettings"]["MODE"] == "FacialCam" or self.confs["ClassifierSettings"]["MODE"] == "CustomCam":
self.InceptionFlow.createGraph(self.confs["ClassifierSettings"]["MODE"])
try:
self.OpenCVCapture = cv2.VideoCapture(self.confs["Cameras"][0]["URL"])
except Exception as e:
print("FAILED TO CONNECT TO WEBCAM")
print(str(e))
sys.exit()
elif self.confs["ClassifierSettings"]["MODE"] == "ObjectTest" or self.confs["ClassifierSettings"]["MODE"] == "FacialTest" or self.confs["ClassifierSettings"]["MODE"] == "CustomTest":
self.InceptionFlow.createGraph(self.confs["ClassifierSettings"]["MODE"])
def startMQTT(self):
try:
self.JumpWayMQTTClient = IoTJumpWayApplication.JumpWayPythonMQTTApplicationConnection({
"locationID": self.confs["IoTJumpWaySettings"]["SystemLocation"],
"applicationID": self.confs["IoTJumpWaySettings"]["SystemApplicationID"],
"applicationName": self.confs["IoTJumpWaySettings"]["SystemApplicationName"],
"username": self.confs["IoTJumpWayMQTTSettings"]["MQTTUsername"],
"password": self.confs["IoTJumpWayMQTTSettings"]["MQTTPassword"]
})
except Exception as e:
print(str(e))
sys.exit()
self.JumpWayMQTTClient.connectToApplication()
InceptionFlowCore = InceptionFlowCore()
while True:
if InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "CustomTrain":
InceptionFlowCore.InceptionFlow.trainModel("Custom")
print("TRAINING COMPLETED")
print("")
sys.exit(0)
elif InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "FacialTrain":
InceptionFlowCore.InceptionFlow.trainModel("Facial")
print("TRAINING COMPLETED")
print("")
sys.exit(0)
elif InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "ObjectTest":
InceptionFlowCore.InceptionFlow.testModel("Object")
print("TESTING COMPLETED")
print("")
sys.exit(0)
elif InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "CustomTest":
InceptionFlowCore.InceptionFlow.testModel("Custom")
print("TESTING COMPLETED")
print("")
sys.exit(0)
elif InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "FacialTest":
InceptionFlowCore.InceptionFlow.testModel("Facial")
print("TESTING COMPLETED")
print("")
sys.exit(0)
else:
if InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "ObjectCam":
try:
ret, frame = InceptionFlowCore.OpenCVCapture.read()
if not ret: continue
savedFrame = InceptionFlowCore.InceptionFlow.saveImage(frame)
Object,Confidence = InceptionFlowCore.InceptionFlow.classifyObject(savedFrame)
if Confidence > InceptionFlowCore.confs["ClassifierSettings"]["OBJECT_THRESHOLD"]:
print("Object: "+str(Object))
print("Confidence: "+str(Confidence))
print("")
InceptionFlowCore.JumpWayMQTTClient.publishToDeviceChannel(
"Sensors",
InceptionFlowCore.confs["IoTJumpWaySettings"]["SystemZone"],
InceptionFlowCore.confs["IoTJumpWaySettings"]["SystemDeviceID"],
{
"Sensor":"CCTV",
"SensorID": InceptionFlowCore.confs["Cameras"][0]["ID"],
"SensorValue":"OBJECT: " + str(Object) + " (Confidence: " + str(Confidence) + ")"
}
)
else:
print("")
print("NOTHING IDENTIFIED")
print("")
cv2.imshow("InceptionFlow Viewer", frame)
if cv2.waitKey(1) == 27:
break
except cv2.error as e:
print(e)
elif InceptionFlowCore.confs["ClassifierSettings"]["MODE"] == "FacialCam":
try:
ret, frame = InceptionFlowCore.OpenCVCapture.read()
if not ret: continue
currentImage,inceptionImage, marked, detected = InceptionFlowCore.InceptionFlow.captureAndDetect(frame)
if detected is None:
continue
Object,Confidence = InceptionFlowCore.InceptionFlow.classifyFace(inceptionImage)
if Confidence > InceptionFlowCore.confs["ClassifierSettings"]["FACIAL_THRESHOLD"]:
print("Person: "+str(Object))
print("Confidence: "+str(Confidence))
print("")
InceptionFlowCore.JumpWayMQTTClient.publishToDeviceChannel(
"Sensors",
InceptionFlowCore.confs["IoTJumpWaySettings"]["SystemZone"],
InceptionFlowCore.confs["IoTJumpWaySettings"]["SystemDeviceID"],
{
"Sensor":"CCTV",
"SensorID": InceptionFlowCore.confs["Cameras"][0]["ID"],
"SensorValue":"PERSON: " + str(Object) + " (Confidence: " + str(Confidence) + ")"
}
)
else:
print("")
print("NOTHING IDENTIFIED")
print("")
cv2.imshow("InceptionFlow Viewer", marked)
if cv2.waitKey(1) == 27:
break
except cv2.error as e:
print(e)
InceptionFlowCore.OpenCVCapture.release()
InceptionFlowCore.JumpWayMQTTClient.disconnectFromApplication()