forked from NobuoTsukamoto/tflite-cv-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_detection_capture_opencv.py
134 lines (104 loc) · 3.84 KB
/
object_detection_capture_opencv.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Edge TPU object detection with OpenCV.
Copyright (c) 2020 Nobuo Tsukamoto
This software is released under the MIT License.
See the LICENSE file in the project root for more information.
"""
import argparse
import time
import numpy as np
from edgetpu.detection.engine import DetectionEngine
import cv2
import PIL
from utils import visualization as visual
WINDOW_NAME = "Edge TPU TF-lite object detection(OpenCV)"
def ReadLabelFile(file_path):
""" Function to read labels from text files.
Args:
file_path: File path to labels.
"""
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)
parser.add_argument("--videopath", help="File path of Videofile.", default="")
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)
# Video capture.
if args.videopath == "":
print('open camera.')
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, args.width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height)
else:
print(args.videopath)
cap = cv2.VideoCapture(args.videopath)
elapsed_list = []
while(cap.isOpened()):
_, frame = cap.read()
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
input_buf = PIL.Image.fromarray(im)
# 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(frame, box, colors[obj.label_id])
visual.draw_caption(frame, 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(frame, (10, 30), fps_text + avg_text)
# display
cv2.imshow(WINDOW_NAME, frame)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
# When everything done, release the window
cv2.destroyAllWindows()
if __name__ == "__main__":
main()