-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
256 lines (210 loc) · 7.36 KB
/
Copy pathmain.py
File metadata and controls
256 lines (210 loc) · 7.36 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
[summary]
FingerFrame Lens
[description]
-
"""
import argparse
import time
import copy
from collections import deque
import cv2 as cv
import numpy as np
import tensorflow as tf
from utils import CvFpsCalc
from gui.app_gui import AppGui
def get_args():
"""
[summary]
引数解析
Parameters
----------
None
"""
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0)
parser.add_argument("--fps", type=float, default=10.1)
parser.add_argument("--width", help='cap width', type=int, default=960)
parser.add_argument("--height", help='cap height', type=int, default=540)
parser.add_argument("--model", default='model/EfficientDetD0/saved_model')
parser.add_argument("--score_th", type=float, default=0.7)
parser.add_argument("--smaller_ratio", type=float, default=0.22)
args = parser.parse_args()
return args
def run_od_inference(inference_func, image):
"""
[summary]
物体検出推論(1枚)
Parameters
----------
inference_func : func
推論用関数
image : image
推論対象の画像
None
"""
image = image[:, :, [2, 1, 0]] # BGR2RGB
image = np.expand_dims(image, axis=0)
tensor = tf.convert_to_tensor(image)
output = inference_func(tensor)
output['num_detections'] = int(output['num_detections'][0])
output['detection_classes'] = output['detection_classes'][0].numpy()
output['detection_boxes'] = output['detection_boxes'][0].numpy()
output['detection_scores'] = output['detection_scores'][0].numpy()
return output
def calc_od_bbox(detection_result, score_th, smaller_ratio, frame_width,
frame_height):
"""
[summary]
物体検出結果からバウンディングボックスを算出
Parameters
----------
detection_result : dict
物体検出結果
score_th : float
物体検出スコア閾値
smaller_ratio : float
縮小割合
frame_width : int
画像幅
frame_height : int
画像高さ
None
"""
x1, y1, x2, y2 = None, None, None, None
num_detections = detection_result['num_detections']
for i in range(num_detections):
score = detection_result['detection_scores'][i]
bbox = detection_result['detection_boxes'][i]
if score < score_th:
continue
# 検出結果可視化 ###################################################
x1, y1 = int(bbox[1] * frame_width), int(bbox[0] * frame_height)
x2, y2 = int(bbox[3] * frame_width), int(bbox[2] * frame_height)
risize_ratio = smaller_ratio
bbox_width = x2 - x1
bbox_height = y2 - y1
x1 = x1 + int(bbox_width * risize_ratio)
y1 = y1 + int(bbox_height * risize_ratio)
x2 = x2 - int(bbox_width * risize_ratio)
y2 = y2 - int(bbox_height * risize_ratio)
break # 有効なバウンディングボックスの1つ目を利用
return x1, y1, x2, y2
def run_classify(model, image):
"""
[summary]
画像クラス分類
Parameters
----------
model : model
クラス分類用モデル
image : image
推論対象の画像
None
"""
inp = cv.resize(image, (224, 224))
inp = inp[:, :, [2, 1, 0]] # BGR2RGB
inp = np.expand_dims(inp, axis=0)
tensor = tf.convert_to_tensor(inp)
tensor = tf.keras.applications.efficientnet.preprocess_input(tensor)
classifications = model.predict(tensor)
classifications = tf.keras.applications.efficientnet.decode_predictions(
classifications,
top=5,
)
classifications = np.squeeze(classifications)
return classifications
def main():
"""
[summary]
main()
Parameters
----------
None
"""
# 引数解析 #################################################################
args = get_args()
cap_device = args.device
cap_width = args.width
cap_height = args.height
fps = args.fps
model_path = args.model
score_th = args.score_th
smaller_ratio = args.smaller_ratio
# GUI準備 #################################################################
app_gui = AppGui(window_name='FingerFrameLens')
# 初期設定
app_gui.set_score_threshold(score_th)
# カメラ準備 ###############################################################
cap = cv.VideoCapture(cap_device)
cap.set(cv.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, cap_height)
# モデルロード #############################################################
# EfficientDet-D0
DEFAULT_FUNCTION_KEY = 'serving_default'
effdet_model = tf.saved_model.load(model_path)
inference_func = effdet_model.signatures[DEFAULT_FUNCTION_KEY]
# EfficientNet-B0
effnet_model = tf.keras.applications.EfficientNetB0(
include_top=True,
weights='imagenet',
input_shape=(224, 224, 3),
)
tensor = tf.convert_to_tensor(np.zeros((1, 224, 224, 3), np.uint8))
effnet_model.predict(tensor)
effnet_model.make_predict_function()
# FPS計測準備 ##############################################################
cvFpsCalc = CvFpsCalc(buffer_len=3)
cropping_image = None
classifications = None
while True:
start_time = time.time()
# GUI設定取得 #########################################################
score_th = app_gui.get_score_threshold()
# カメラキャプチャ #####################################################
ret, frame = cap.read()
if not ret:
continue
frame_width, frame_height = frame.shape[1], frame.shape[0]
debug_image = copy.deepcopy(frame)
# 物体検出実施 #########################################################
detections = run_od_inference(inference_func, frame)
x1, y1, x2, y2 = calc_od_bbox(
detections,
score_th,
smaller_ratio,
frame_width,
frame_height,
)
# cv.putText(debug_image, '{:.3f}'.format(score), (x1, y1 - 10),
# cv.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2,
# cv.LINE_AA)
# cv.rectangle(debug_image, (x1, y1), (x2, y2), (255, 255, 255), 2)
# クラス分類実施 #######################################################
if x1 is not None and y1 is not None and \
x2 is not None and y2 is not None:
cropping_image = copy.deepcopy(frame[y1:y2, x1:x2])
classifications = run_classify(effnet_model, cropping_image)
# GUI描画更新 ##########################################################
fps_result = cvFpsCalc.get()
app_gui.update(
fps_result,
debug_image,
cropping_image,
classifications,
)
app_gui.show()
# キー入力(ESC:プログラム終了) #########################################
key = cv.waitKey(1)
if key == 27: # ESC
break
# FPS調整 #############################################################
elapsed_time = time.time() - start_time
sleep_time = max(0, ((1.0 / fps) - elapsed_time))
time.sleep(sleep_time)
cap.release()
cv.destroyAllWindows()
if __name__ == '__main__':
main()