forked from NobuoTsukamoto/tflite-cv-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_detection_capture_picamera.py
141 lines (110 loc) · 4.4 KB
/
object_detection_capture_picamera.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Edge TPU object detection Raspberry Pi camera stream.
Copyright (c) 2019 Nobuo Tsukamoto
This software is released under the MIT License.
See the LICENSE file in the project root for more information.
"""
import argparse
import io
import time
import numpy as np
import picamera
from picamera.array import PiRGBArray
from edgetpu.detection.engine import DetectionEngine
import cv2
import PIL
from utils import visualization as visual
WINDOW_NAME = "Edge TPU TF-lite object detection(PiCamera)"
# Function to read labels from text files.
def ReadLabelFile(file_path):
with open(file_path, "r") as f:
lines = f.readlines()
ret = {}
for line in lines:
pair = line.strip().split(maxsplit=1)
ret[int(pair[0])] = pair[1].strip()
return ret
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--model", help="File path of Tflite model.", required=True)
parser.add_argument("--label", help="File path of label file.", required=True)
parser.add_argument("--top_k", help="keep top k candidates.", default=3)
parser.add_argument(
"--threshold", help="threshold to filter results.", default=0.5, type=float
)
parser.add_argument("--width", help="Resolution width.", default=640, type=int)
parser.add_argument("--height", help="Resolution height.", default=480, type=int)
args = parser.parse_args()
# Initialize window.
cv2.namedWindow(
WINDOW_NAME, cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_AUTOSIZE | cv2.WINDOW_KEEPRATIO
)
cv2.moveWindow(WINDOW_NAME, 100, 200)
# Initialize engine.
engine = DetectionEngine(args.model)
labels = ReadLabelFile(args.label) if args.label else None
# Generate random colors.
last_key = sorted(labels.keys())[len(labels.keys()) - 1]
colors = visual.random_colors(last_key)
elapsed_list = []
resolution_width = args.width
rezolution_height = args.height
with picamera.PiCamera() as camera:
camera.resolution = (resolution_width, rezolution_height)
camera.framerate = 30
_, width, height, channels = engine.get_input_tensor_shape()
rawCapture = PiRGBArray(camera)
# allow the camera to warmup
time.sleep(0.1)
try:
for frame in camera.capture_continuous(
rawCapture, format="rgb", use_video_port=True
):
rawCapture.truncate(0)
# input_buf = np.frombuffer(stream.getvalue(), dtype=np.uint8)
image = frame.array
im = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
input_buf = PIL.Image.fromarray(image)
# Run inference.
start_ms = time.time()
ans = engine.detect_with_image(
input_buf,
threshold=args.threshold,
keep_aspect_ratio=False,
relative_coord=False,
top_k=args.top_k,
)
elapsed_ms = engine.get_inference_time()
# Display result.
if ans:
for obj in ans:
label_name = "Unknown"
if labels:
label_name = labels[obj.label_id]
caption = "{0}({1:.2f})".format(label_name, obj.score)
# Draw a rectangle and caption.
box = obj.bounding_box.flatten().tolist()
visual.draw_rectangle(im, box, colors[obj.label_id])
visual.draw_caption(im, box, caption)
# Calc fps.
elapsed_list.append(elapsed_ms)
avg_text = ""
if len(elapsed_list) > 100:
elapsed_list.pop(0)
avg_elapsed_ms = np.mean(elapsed_list)
avg_text = " AGV: {0:.2f}ms".format(avg_elapsed_ms)
# Display fps
fps_text = "{0:.2f}ms".format(elapsed_ms)
visual.draw_caption(im, (10, 30), fps_text + avg_text)
# display
cv2.imshow(WINDOW_NAME, im)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
finally:
camera.stop_preview()
# When everything done, release the window
cv2.destroyAllWindows()
if __name__ == "__main__":
main()