-
Notifications
You must be signed in to change notification settings - Fork 5
/
model_maker_object_detection_from_cam.py
120 lines (103 loc) · 5.09 KB
/
model_maker_object_detection_from_cam.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
import io
import time
import argparse
import numpy as np
from picamera import PiCamera
from PIL import Image
from PIL import ImageDraw
from pycoral.adapters import common
from pycoral.adapters import detect
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
colors = {0: (128, 255, 102), 1: (102, 255, 255)}
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-m', '--model', required=True,
help='File path of .tflite file.')
parser.add_argument('-l', '--labels',
help='File path of labels file.')
parser.add_argument('-t', '--threshold', type=float, default=0.0,
help='Classification score threshold')
parser.add_argument('--debug', dest='debug', action='store_true',
help='Saves the last image to last_image_obj_detec_from_cam.jpg')
args = parser.parse_args()
print(f"Initializing EdgeTPU... {args.model} and {args.labels}")
labels = read_label_file(args.labels) if args.labels else {}
interpreter = make_interpreter(*args.model.split('@'))
interpreter.allocate_tensors()
size = common.input_size(interpreter)
interpreter.invoke() # warmup
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(f"Model input details:\n{input_details}")
print(f"Model output details:\n{output_details}")
def process_img(image):
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
scores = interpreter.get_tensor(output_details[0]['index'])[0]
boxes = interpreter.get_tensor(output_details[1]['index'])[0]
num_detections = interpreter.get_tensor(output_details[2]['index'])[0]
classes = interpreter.get_tensor(output_details[3]['index'])[0]
return boxes, classes, scores, num_detections
print("Initializing picamera...")
# See https://picamera.readthedocs.io/en/release-1.13/api_camera.html
# for details about the parameters:
print("Starting...")
# Start the video process
stream = io.BytesIO()
time.sleep(2)
try:
with PiCamera() as camera:
camera.resolution = (320, 320)
camera.framerate = 60
# camera.contrast = contrast
camera.video_stabilization = True
camera.video_denoise = True
#camera.rotation = 180
start = time.perf_counter()
for _ in camera.capture_continuous(stream, format='rgb'):
print(f'Running at {(1/(time.perf_counter() - start)):.2f} fps')
start = time.perf_counter()
stream.truncate()
stream.seek(0)
image = Image.fromarray(np.frombuffer(stream.getvalue(), dtype=np.uint8).reshape((320,320,3))).convert('RGB').resize(size, Image.ANTIALIAS)
if image:
np_image = np.asarray(image)
input_tensor = np.expand_dims(np_image, axis=0)
boxes, classes, scores, num_detections = process_img(input_tensor)
for i in range(int(num_detections)):
if scores[i] >= args.threshold:
ymin = int(max(1, (boxes[i][0] * size[1])))
xmin = int(max(1, (boxes[i][1] * size[0])))
ymax = int(min(size[1], (boxes[i][2] * size[1])))
xmax = int(min(size[0], (boxes[i][3] * size[0])))
# for obji,obj in enumerate(objs):
print(f"Obj #{i} (Confidence {int(100*scores[i])}%): {labels.get(int(classes[i]))} - {(xmin,ymin),(xmax,ymax)}\n")
except KeyboardInterrupt:
pass
finally:
if args.debug and image:
dimage = ImageDraw.Draw(image)
if len(boxes):
for i in range(int(num_detections)):
if scores[i] >= args.threshold:
ymin = int(max(1, (boxes[i][0] * size[1])))
xmin = int(max(1, (boxes[i][1] * size[0])))
ymax = int(min(size[1], (boxes[i][2] * size[1])))
xmax = int(min(size[0], (boxes[i][3] * size[0])))
dimage.rectangle((xmin, ymin, xmax, ymax), width=7,
outline=colors[int(classes[i])])
dimage.rectangle((xmin, ymin, xmax, ymin-10),
fill=colors[int(classes[i])])
try:
label = labels[int(classes[i])]
except KeyError:
label = "None"
text = label + ' ' + str(scores[i]*100) + '%'
dimage.text((xmin+2, ymin-10), text, fill=(0, 0, 0), width=5)
image.save("last_image_obj_detec_from_cam.jpg")
print("last_image_obj_detec_from_cam.jpg saved!")
print("Done!")
if __name__ == '__main__':
main()