-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeeplearning.py
481 lines (410 loc) · 17 KB
/
deeplearning.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
"""This module contains Deep Learning Detector Class."""
import sys
import time
import numpy as np
import cv2
from pyModbusTCP.server import ModbusServer
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
from utils import terminal, array, video
from processing import vizres
from constant import LIMIT_CMR_TIME, LIMIT_IR_TIME, LIMIT_DET_TIME
from hardware.sensor import prepare_gpio, clean_gpio
from hardware.sensor import Infrared, Led
from hardware.plc import prepare_server, server_set_di, server_set_ir
from hardware.plc import PLC
from detection.helper import Flags, Calculator
class DeepDetector:
"""Deep Learning Object Detector Detector Class."""
def __init__(
self,
is_multiple: bool,
model: str,
width: int,
height: int,
num_threads: int,
enable_edgetpu: bool,
):
"""Init an object instance from the class.
Args:
is_multiple: True/False whether there is multiple type of object.
model: Name of the TFLite object detection model.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
num_threads: The number of CPU threads to run the model.
enable_edgetpu: True/False whether the model is a EdgeTPU model.
"""
# Define detection attributes
self.is_multiple = is_multiple
self.model = model
self.width = width
self.height = height
self.num_threads = num_threads
self.enable_edgetpu = enable_edgetpu
# Define detection option
base_options = core.BaseOptions(
file_name=self.model,
use_coral=self.enable_edgetpu,
num_threads=self.num_threads,
)
detection_options = processor.DetectionOptions(
max_results=3, score_threshold=0.42
)
self.options = vision.ObjectDetectorOptions(
base_options=base_options, detection_options=detection_options
)
# Attribute to collect final detection
self.detected_list = []
self.final_score_list = []
self.final_pred_list = []
self.true_label_list = []
def detect(
self,
det_type: str,
true_label: str,
is_server: bool,
client_address: str,
server_address: str,
):
"""Run detection using deep learning method.
Args:
det_type: Type of detections (color, shape, or category).
true_label: true label for detected object.
is_server: flag to decide whether raspberry pi is server or not
client_address: IP address of client (PLC) to be connected.
server_address: IP address of Modbus server.
Return:
delay_list: recorded delay time
fps_list: recorded FPS values
detected_list: bool values of whether object is detected or not
final_score_list: probability scores for detection sessions
final_pred_list: predicted values for detection sessions
true_label_list: true label for detection sessions
"""
print("DETECTION STARTED!")
# Variable to collect detections
pred_list, index_list, score_list = [], [], []
# Convert the true label to tuple
atuple = tuple(true_label.strip("()").split(","))
label_length = len(atuple)
if label_length == 1:
true_label = atuple[0]
else:
true_label = (
int(round(float(atuple[0]))),
int(round(float(atuple[1]))),
int(round(float(atuple[2]))),
)
# Define LED and IR instance
prepare_gpio()
ir = Infrared(right_ir=22, left_ir=16) # We will use left IR
led = Led(yellow=18, red=11, blue=15)
# Initialize PLC instance and classid-to-bit dict
id_to_bit = {"0": "001", "1": "010", "2": "100"}
if is_server:
server_data, server_id = prepare_server()
server = ModbusServer(
server_address,
5020,
no_block=True,
data_bank=server_data,
device_id=server_id,
)
print("Starting server...")
server.start()
print("Server is online")
else:
plc = PLC(client_address)
# Define Flag and Calculator instance
flags = Flags()
calc = Calculator()
# Define camera attributes
cap = cv2.VideoCapture(0) # Default camera ID = 0
cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
# Initialize the object detection model
detector = vision.ObjectDetector.create_from_options(self.options)
# Continuously capture images from the camera and run inference
while cap.isOpened():
success, image = cap.read()
if not success:
sys.exit(
"""ERROR: Unable to read from webcam.
Please verify your webcam settings."""
)
calc.frame_up()
# Convert the image as required by the TFLite model.
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create a TensorImage object from the RGB image.
input_tensor = vision.TensorImage.create_from_array(rgb_image)
# Run object detection estimation using the model.
detection_result = detector.detect(input_tensor)
# If object detected by IR sensor
if (ir.read_sensor()[1] == 0) and (
(time.time() - calc.old_ir_time) >= LIMIT_IR_TIME
):
# Restart timer and reset flag
calc.restart_ir()
flags.reverse_ir()
print("")
# Update cpunter
calc.det_up()
# If object is detected, record the time and collect detections
if detection_result.detections:
if (time.time() - calc.old_cmr_time) >= LIMIT_CMR_TIME:
# Clear lists and restart camera timer
array.reset_list(pred_list, index_list, score_list)
calc.restart_cmr()
# Update flags
flags.reverse_cmr()
if time.time() <= (calc.old_cmr_time + LIMIT_DET_TIME):
if det_type == "color":
predictions = vizres.detect_color(detection_result)
elif det_type == "shape":
predictions = vizres.measure_dim(detection_result, self.height)
else:
predictions = vizres.categorize(detection_result)
array.update_list([score_list, index_list, pred_list], predictions)
# If object is detected on IR and Camera
if (flags.detected_ir) and (flags.detected_cmr):
# Calculate delay and print messages
calc.calculate_delay()
calc.print_data()
# Reset flags
flags.reverse_ir()
flags.reverse_cmr()
flags.reverse_msg()
# Else, if only detected on IR
elif (
flags.detected_ir
and (not flags.detected_cmr)
and ((time.time() - calc.old_ir_time) >= LIMIT_IR_TIME)
):
# Print message
terminal.print_undetected(calc.det_count)
# Turn off LED
led.turn_off()
# If there are multiple types of object,
# prompt user to input true label
if self.is_multiple:
true_label = terminal.prompt_label()
# Send data to plc
calc.start_coil()
server_set_di(server_data, "000") if is_server else plc.write_bits(
"000"
)
calc.calc_coil_latency()
if label_length == 1:
array.update_list([self.final_pred_list], ["-"])
else:
array.update_list([self.final_pred_list], [(0, 0, 0)])
calc.start_reg()
server_set_ir(
server_data, (0, 0, 0)
) if is_server else plc.write_words((0, 0, 0))
calc.calc_reg_latency()
# Update detected result list
calc.update_data(False)
array.update_list(
[self.final_score_list, self.detected_list, self.true_label_list],
[0, False, true_label],
)
# Reset flag
flags.reverse_ir()
# Only print result once every detection session
if (
(score_list)
and (time.time() > (calc.old_cmr_time + LIMIT_DET_TIME))
and (flags.print_message)
):
# Find object with highest probability score
object_id = np.argmax(score_list)
# print messages
if det_type == "color":
terminal.print_color(pred_list[object_id], score_list[object_id])
elif det_type == "shape":
terminal.print_dimension(
pred_list[object_id], score_list[object_id]
)
else:
terminal.print_detected(pred_list[object_id], score_list[object_id])
# Turn on LED
led.turn_on(index=index_list[object_id])
# If there are multiple types of object,
# prompt user to input true label
if self.is_multiple:
true_label = terminal.prompt_label()
# Send data to plc based on index
calc.start_coil()
if is_server:
server_set_di(server_data, id_to_bit[str(index_list[object_id])])
else:
plc.write_bits(id_to_bit[str(index_list[object_id])])
calc.calc_coil_latency()
if label_length != 1:
calc.start_reg()
if is_server:
server_set_ir(server_data, pred_list[object_id])
else:
plc.write_words(pred_list[object_id])
calc.calc_reg_latency()
# Update detected result list
calc.update_data(True)
array.update_list(
[
self.final_pred_list,
self.final_score_list,
self.detected_list,
self.true_label_list,
],
[pred_list[object_id], score_list[object_id], True, true_label],
)
# Reset flags
flags.reverse_msg()
# Draw keypoints and edges on input image based on object color
try:
box_color = vizres.detect_color(detection_result)[2]
except TypeError:
box_color = (0, 0, 0)
image = vizres.visualize(
image, detection_result, (self.width, self.height), box_color
)
# Show the FPS
calc.calculate_fps()
fps_text = f"FPS = {round(calc.fps, 1)}"
vizres.show_fps(
img=image, text=fps_text, resolution=(self.width, self.height)
)
# Stop the program if the ESC key is pressed.
if cv2.waitKey(1) == 27:
print("")
break
cv2.imshow("object_detector", image)
cap.release()
cv2.destroyAllWindows()
led.turn_off()
clean_gpio()
server_set_di(server_data, "000") if is_server else plc.write_bits("000")
server_set_ir(server_data, (0, 0, 0)) if is_server else plc.write_words(
(0, 0, 0)
)
print("Shutdown server...")
server.stop()
print("Server is offline")
print("DETECTION STOPPED!")
# Return list of recorded data
return (
calc.delay_list,
calc.fps_list,
calc.reg_latency_list,
calc.coil_latency_list,
self.detected_list,
self.final_score_list,
self.final_pred_list,
self.true_label_list,
)
def capture(self, true_label: str, all_images: bool, vid_filename: str):
"""Capture detected object frames.
Args:
true_label: true label for detected object.
all_images: True/False whether to collect all images.
vid_filename: filename of output video.
"""
# Define Flag and Calculator instance
flags = Flags()
calc = Calculator()
calc.det_up()
calc.img_up()
# Define camera instance and frame resolution
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
# Define the codec and create VideoWriter Object
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(vid_filename, fourcc, 2.5, (self.width, self.height))
# Initizalize object detection model
detector = vision.ObjectDetector.create_from_options(self.options)
# Continuously capture images from the camera and run inference
print("DETECTION STARTED!")
print("")
print(f"Detection Session: {calc.det_count}")
# If there is multiple type of object, prompt user to input true label
if self.is_multiple:
true_label = input("True label of the object: ")
while cap.isOpened():
success, image = cap.read()
if not success:
sys.exit(
"""ERROR: Unable to read from webcam.
Please verify your webcam settings."""
)
calc.frame_up()
# Convert the image as required by the TFLite model.
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create a TensorImage object from the RGB image.
input_tensor = vision.TensorImage.create_from_array(rgb_image)
# Run object detection estimation using the model.
detection_result = detector.detect(input_tensor)
# If something detected on camera, restart timer and flag
if (
detection_result.detections
and (time.time() - calc.old_cmr_time) >= LIMIT_CMR_TIME
):
calc.restart_cmr()
flags.reverse_msg()
# Only print result once every detection session
if (
time.time() > (calc.old_cmr_time + LIMIT_DET_TIME)
) and flags.print_message:
# print messages
print("Images of object has been collected.")
print("")
# Update counter and flags
calc.det_up()
calc.img_count = 1
flags.reverse_msg()
# If there is multiple type of object
print(f"Detection Session: {calc.det_count}")
if self.is_multiple:
true_label = input("True label of the object: ")
# Draw keypoints and edges on input image if detected
if detection_result.detections:
image = vizres.visualize(
image,
detection_result,
(self.width, self.height),
vizres.detect_color(detection_result)[2],
)
predicted_class = vizres.categorize(detection_result)[2]
# Either collect all images or image with correct labels
if all_images:
video.save_img(
image, predicted_class, calc.det_count, calc.img_count
)
calc.img_up()
elif predicted_class == true_label:
video.save_img(
image, predicted_class, calc.det_count, calc.img_count
)
calc.img_up()
else:
image = vizres.visualize(image, detection_result)
# Show the FPS
calc.calculate_fps()
fps_text = f"FPS = {round(calc.fps, 1)}"
vizres.show_fps(
img=image, text=fps_text, resolution=(self.width, self.height)
)
# Write Image to Videos
out.write(image)
# Stop the program if the ESC key is pressed.
if cv2.waitKey(1) == 27:
break
cv2.imshow("object_detector", image)
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
print("")
print("DETECTION STOPPED!")