Skip to content

Commit 41d77f9

Browse files
committed
sample update
1 parent 0d5794e commit 41d77f9

File tree

3 files changed

+63
-47
lines changed

3 files changed

+63
-47
lines changed

mobilenetv2ssd-async-usbcam.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,25 @@
4444
class ObjectDetectorLite():
4545
def __init__(self, model_path='detect.tflite', threads_num=4):
4646
try:
47-
self.interpreter = Interpreter(model_path=model_path, num_threads=num_threads)
47+
self.interpreter = Interpreter(model_path=model_path, num_threads=threads_num)
4848
except:
49-
self.interpreter = tf.lite.Interpreter(model_path=model_path, num_threads=num_threads)
49+
self.interpreter = tf.lite.Interpreter(model_path=model_path, num_threads=threads_num)
5050
try:
5151
self.interpreter.allocate_tensors()
5252
except:
5353
pass
5454
self.input_details = self.interpreter.get_input_details()
5555
self.output_details = self.interpreter.get_output_details()
5656

57-
def _boxes_coordinates(self,
58-
image,
59-
boxes,
60-
classes,
61-
scores,
62-
max_boxes_to_draw=20,
63-
min_score_thresh=.5):
57+
def _boxes_coordinates(
58+
self,
59+
image,
60+
boxes,
61+
classes,
62+
scores,
63+
max_boxes_to_draw=20,
64+
min_score_thresh=.5
65+
):
6466

6567
if not max_boxes_to_draw:
6668
max_boxes_to_draw = boxes.shape[0]
@@ -96,11 +98,13 @@ def detect(self, image, threshold=0.1):
9698
num = self.interpreter.get_tensor(self.output_details[3]['index'])
9799

98100
# Find detected boxes coordinates
99-
return self._boxes_coordinates(image,
100-
np.squeeze(boxes[0]),
101-
np.squeeze(classes[0]+1).astype(np.int32),
102-
np.squeeze(scores[0]),
103-
min_score_thresh=threshold)
101+
return self._boxes_coordinates(
102+
image,
103+
np.squeeze(boxes[0]),
104+
np.squeeze(classes[0]+1).astype(np.int32),
105+
np.squeeze(scores[0]),
106+
min_score_thresh=threshold,
107+
)
104108

105109

106110
def camThread(results, frameBuffer, camera_width, camera_height, vidfps, usbcamno):
@@ -197,7 +201,7 @@ def overlay_on_image(frames, object_infos, camera_width, camera_height):
197201
cv2.rectangle(img_cp, (box_left, box_top), (box_right, box_bottom), box_color, box_thickness)
198202

199203
percentage = int(obj[2] * 100)
200-
label_text = obj[3] + " (" + str(percentage) + "%)"
204+
label_text = obj[3] + " (" + str(percentage) + "%)"
201205

202206
label_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)[0]
203207
label_left = box_left
@@ -238,17 +242,21 @@ def overlay_on_image(frames, object_infos, camera_width, camera_height):
238242
results = mp.Queue()
239243

240244
# Start streaming
241-
p = mp.Process(target=camThread,
242-
args=(results, frameBuffer, camera_width, camera_height, vidfps, usbcamno),
243-
daemon=True)
245+
p = mp.Process(
246+
target=camThread,
247+
args=(results, frameBuffer, camera_width, camera_height, vidfps, usbcamno),
248+
daemon=True,
249+
)
244250
p.start()
245251
processes.append(p)
246252

247253
# Activation of inferencer
248254
for process_num in range(core_num):
249-
p = mp.Process(target=inferencer,
250-
args=(results, frameBuffer, model, camera_width, camera_height, process_num, threads_num),
251-
daemon=True)
255+
p = mp.Process(
256+
target=inferencer,
257+
args=(results, frameBuffer, model, camera_width, camera_height, process_num, threads_num),
258+
daemon=True,
259+
)
252260
p.start()
253261
processes.append(p)
254262

mobilenetv2ssd-sync-usbcam.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ def __init__(self, model_path='detect.tflite', num_threads=12):
5454
self.input_details = self.interpreter.get_input_details()
5555
self.output_details = self.interpreter.get_output_details()
5656

57-
def _boxes_coordinates(self,
58-
image,
59-
boxes,
60-
classes,
61-
scores,
62-
max_boxes_to_draw=20,
63-
min_score_thresh=.5):
57+
def _boxes_coordinates(
58+
self,
59+
image,
60+
boxes,
61+
classes,
62+
scores,
63+
max_boxes_to_draw=20,
64+
min_score_thresh=.5
65+
):
6466

6567
if not max_boxes_to_draw:
6668
max_boxes_to_draw = boxes.shape[0]
@@ -92,11 +94,13 @@ def detect(self, image, threshold=0.1):
9294
num = self.interpreter.get_tensor(self.output_details[3]['index'])
9395

9496
# Find detected boxes coordinates
95-
return self._boxes_coordinates(image,
96-
np.squeeze(boxes[0]),
97-
np.squeeze(classes[0]+1).astype(np.int32),
98-
np.squeeze(scores[0]),
99-
min_score_thresh=threshold)
97+
return self._boxes_coordinates(
98+
image,
99+
np.squeeze(boxes[0]),
100+
np.squeeze(classes[0]+1).astype(np.int32),
101+
np.squeeze(scores[0]),
102+
min_score_thresh=threshold,
103+
)
100104

101105

102106
def overlay_on_image(frames, object_infos, camera_width, camera_height):
@@ -115,7 +119,7 @@ def overlay_on_image(frames, object_infos, camera_width, camera_height):
115119
cv2.rectangle(img_cp, (box_left, box_top), (box_right, box_bottom), box_color, box_thickness)
116120

117121
percentage = int(obj[2] * 100)
118-
label_text = obj[3] + " (" + str(percentage) + "%)"
122+
label_text = obj[3] + " (" + str(percentage) + "%)"
119123

120124
label_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)[0]
121125
label_left = box_left
@@ -184,7 +188,7 @@ def overlay_on_image(frames, object_infos, camera_width, camera_height):
184188
else:
185189
ret, color_image = cam.read()
186190
if not ret:
187-
continue
191+
continue
188192

189193
prepimg = cv2.resize(color_image, (300, 300))
190194
frames = prepimg.copy()

mobilenetv2ssd.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ def __init__(self, model_path='detect.tflite'):
2222
self.input_details = self.interpreter.get_input_details()
2323
self.output_details = self.interpreter.get_output_details()
2424

25-
def _boxes_coordinates(self,
26-
image,
27-
boxes,
28-
classes,
29-
scores,
30-
max_boxes_to_draw=20,
31-
min_score_thresh=.5):
25+
def _boxes_coordinates(
26+
self,
27+
image,
28+
boxes,
29+
classes,
30+
scores,
31+
max_boxes_to_draw=20,
32+
min_score_thresh=.5
33+
):
3234

3335
if not max_boxes_to_draw:
3436
max_boxes_to_draw = boxes.shape[0]
@@ -64,11 +66,13 @@ def detect(self, image, threshold=0.1):
6466
num = self.interpreter.get_tensor(self.output_details[3]['index'])
6567

6668
# Find detected boxes coordinates
67-
return self._boxes_coordinates(image,
68-
np.squeeze(boxes[0]),
69-
np.squeeze(classes[0]+1).astype(np.int32),
70-
np.squeeze(scores[0]),
71-
min_score_thresh=threshold)
69+
return self._boxes_coordinates(
70+
image,
71+
np.squeeze(boxes[0]),
72+
np.squeeze(classes[0]+1).astype(np.int32),
73+
np.squeeze(scores[0]),
74+
min_score_thresh=threshold,
75+
)
7276

7377

7478
if __name__ == '__main__':

0 commit comments

Comments
 (0)