forked from Kazuhito00/NARUTO-HandSignDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ninjutsu_demo.py
395 lines (327 loc) · 13.4 KB
/
Ninjutsu_demo.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import csv
import time
import copy
from collections import deque
import cv2 as cv
import numpy as np
from utils import CvFpsCalc
from utils import CvDrawText
from model.yolox.yolox_onnx import YoloxONNX
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0)
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("--file", type=str, default=None)
parser.add_argument("--fps", type=int, default=30)
parser.add_argument("--skip_frame", type=int, default=0)
parser.add_argument(
"--model",
type=str,
default='model/yolox/yolox_nano.onnx',
)
parser.add_argument(
'--input_shape',
type=str,
default="416,416",
help="Specify an input shape for inference.",
)
parser.add_argument(
'--score_th',
type=float,
default=0.7,
help='Class confidence',
)
parser.add_argument(
'--nms_th',
type=float,
default=0.45,
help='NMS IoU threshold',
)
parser.add_argument(
'--nms_score_th',
type=float,
default=0.1,
help='NMS Score threshold',
)
parser.add_argument(
"--with_p6",
action="store_true",
help="Whether your model uses p6 in FPN/PAN.",
)
parser.add_argument("--sign_interval", type=float, default=2.0)
parser.add_argument("--jutsu_display_time", type=int, default=5)
parser.add_argument("--use_display_score", type=bool, default=False)
parser.add_argument("--erase_bbox", type=bool, default=False)
parser.add_argument("--use_jutsu_lang_en", type=bool, default=False)
parser.add_argument("--chattering_check", type=int, default=1)
parser.add_argument("--use_fullscreen", type=bool, default=False)
args = parser.parse_args()
return args
def main():
# 引数解析 #################################################################
args = get_args()
cap_width = args.width
cap_height = args.height
cap_device = args.device
if args.file is not None: # 動画ファイルを利用する場合
cap_device = args.file
fps = args.fps
skip_frame = args.skip_frame
model_path = args.model
input_shape = tuple(map(int, args.input_shape.split(',')))
score_th = args.score_th
nms_th = args.nms_th
nms_score_th = args.nms_score_th
with_p6 = args.with_p6
sign_interval = args.sign_interval
jutsu_display_time = args.jutsu_display_time
use_display_score = args.use_display_score
erase_bbox = args.erase_bbox
use_jutsu_lang_en = args.use_jutsu_lang_en
chattering_check = args.chattering_check
use_fullscreen = args.use_fullscreen
# カメラ準備 ###############################################################
cap = cv.VideoCapture(cap_device)
cap.set(cv.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, cap_height)
# モデル読み込み ############################################################
yolox = YoloxONNX(
model_path=model_path,
input_shape=input_shape,
class_score_th=score_th,
nms_th=nms_th,
nms_score_th=nms_score_th,
with_p6=with_p6,
# providers=['CPUExecutionProvider'],
)
# FPS計測モジュール #########################################################
cvFpsCalc = CvFpsCalc()
# フォント読み込み ##########################################################
# https://opentype.jp/kouzanmouhitufont.htm
font_path = './utils/font/衡山毛筆フォント.ttf'
# ラベル読み込み ###########################################################
with open('setting/labels.csv', encoding='utf8') as f: # 印
labels = csv.reader(f)
labels = [row for row in labels]
with open('setting/jutsu.csv', encoding='utf8') as f: # 術
jutsu = csv.reader(f)
jutsu = [row for row in jutsu]
# 印の表示履歴および、検出履歴 ##############################################
sign_max_display = 18
sign_max_history = 44
sign_display_queue = deque(maxlen=sign_max_display)
sign_history_queue = deque(maxlen=sign_max_history)
chattering_check_queue = deque(maxlen=chattering_check)
for index in range(-1, -1 - chattering_check, -1):
chattering_check_queue.append(index)
# 術名の言語設定 ###########################################################
lang_offset = 0
jutsu_font_size_ratio = sign_max_display
if use_jutsu_lang_en:
lang_offset = 1
jutsu_font_size_ratio = int((sign_max_display / 3) * 4)
# その他変数初期化 #########################################################
sign_interval_start = 0 # 印のインターバル開始時間初期化
jutsu_index = 0 # 術表示名のインデックス
jutsu_start_time = 0 # 術名表示の開始時間初期化
frame_count = 0 # フレームナンバーカウンタ
window_name = 'NARUTO HandSignDetection Ninjutsu Demo'
if use_fullscreen:
cv.namedWindow(window_name, cv.WINDOW_NORMAL)
while True:
start_time = time.time()
# カメラキャプチャ #####################################################
ret, frame = cap.read()
if not ret:
continue
frame_count += 1
debug_image = copy.deepcopy(frame)
if (frame_count % (skip_frame + 1)) != 0:
continue
# FPS計測 ##############################################################
fps_result = cvFpsCalc.get()
# 検出実施 #############################################################
bboxes, scores, class_ids = yolox.inference(frame)
# 検出内容の履歴追加 ####################################################
for _, score, class_id in zip(bboxes, scores, class_ids):
class_id = int(class_id) + 1
# 検出閾値未満の結果は捨てる
if score < score_th:
continue
# 指定回数以上、同じ印が続いた場合に、印検出とみなす ※瞬間的な誤検出対策
chattering_check_queue.append(class_id)
if len(set(chattering_check_queue)) != 1:
continue
# 前回と異なる印の場合のみキューに登録
if len(sign_display_queue
) == 0 or sign_display_queue[-1] != class_id:
sign_display_queue.append(class_id)
sign_history_queue.append(class_id)
sign_interval_start = time.time() # 印の最終検出時間
# 前回の印検出から指定時間が経過した場合、履歴を消去 ####################
if (time.time() - sign_interval_start) > sign_interval:
sign_display_queue.clear()
sign_history_queue.clear()
# 術成立判定 #########################################################
jutsu_index, jutsu_start_time = check_jutsu(
sign_history_queue,
labels,
jutsu,
jutsu_index,
jutsu_start_time,
)
# キー処理 ###########################################################
key = cv.waitKey(1)
if key == 99: # C:印の履歴を消去
sign_display_queue.clear()
sign_history_queue.clear()
if key == 27: # ESC:プログラム終了
break
# 画面反映 #############################################################
debug_image = draw_debug_image(
debug_image,
font_path,
fps_result,
labels,
bboxes,
scores,
class_ids,
score_th,
erase_bbox,
use_display_score,
jutsu,
sign_display_queue,
sign_max_display,
jutsu_display_time,
jutsu_font_size_ratio,
lang_offset,
jutsu_index,
jutsu_start_time,
)
if use_fullscreen:
cv.setWindowProperty(window_name, cv.WND_PROP_FULLSCREEN,
cv.WINDOW_FULLSCREEN)
cv.imshow(window_name, debug_image)
# cv.moveWindow(window_name, 100, 100)
# FPS調整 #############################################################
elapsed_time = time.time() - start_time
sleep_time = max(0, ((1.0 / fps) - elapsed_time))
time.sleep(sleep_time)
cap.release()
cv.destroyAllWindows()
def check_jutsu(
sign_history_queue,
labels,
jutsu,
jutsu_index,
jutsu_start_time,
):
# 印の履歴から術名をマッチング
sign_history = ''
if len(sign_history_queue) > 0:
for sign_id in sign_history_queue:
sign_history = sign_history + labels[sign_id][1]
for index, signs in enumerate(jutsu):
if sign_history == ''.join(signs[4:]):
jutsu_index = index
jutsu_start_time = time.time() # 術の最終検出時間
break
return jutsu_index, jutsu_start_time
def draw_debug_image(
debug_image,
font_path,
fps_result,
labels,
bboxes,
scores,
class_ids,
score_th,
erase_bbox,
use_display_score,
jutsu,
sign_display_queue,
sign_max_display,
jutsu_display_time,
jutsu_font_size_ratio,
lang_offset,
jutsu_index,
jutsu_start_time,
):
frame_width, frame_height = debug_image.shape[1], debug_image.shape[0]
# 印のバウンディングボックスの重畳表示(表示オプション有効時) ###################
if not erase_bbox:
for bbox, score, class_id in zip(bboxes, scores, class_ids):
class_id = int(class_id) + 1
# 検出閾値未満のバウンディングボックスは捨てる
if score < score_th:
continue
x1, y1 = int(bbox[0]), int(bbox[1])
x2, y2 = int(bbox[2]), int(bbox[3])
# バウンディングボックス(長い辺にあわせて正方形を表示)
x_len = x2 - x1
y_len = y2 - y1
square_len = x_len if x_len >= y_len else y_len
square_x1 = int(((x1 + x2) / 2) - (square_len / 2))
square_y1 = int(((y1 + y2) / 2) - (square_len / 2))
square_x2 = square_x1 + square_len
square_y2 = square_y1 + square_len
cv.rectangle(debug_image, (square_x1, square_y1),
(square_x2, square_y2), (255, 255, 255), 4)
cv.rectangle(debug_image, (square_x1, square_y1),
(square_x2, square_y2), (0, 0, 0), 2)
# 印の種類
font_size = int(square_len / 2)
debug_image = CvDrawText.puttext(
debug_image, labels[class_id][1],
(square_x2 - font_size, square_y2 - font_size), font_path,
font_size, (185, 0, 0))
# 検出スコア(表示オプション有効時)
if use_display_score:
font_size = int(square_len / 8)
debug_image = CvDrawText.puttext(
debug_image, '{:.3f}'.format(score),
(square_x1 + int(font_size / 4),
square_y1 + int(font_size / 4)), font_path, font_size,
(185, 0, 0))
# ヘッダー作成:FPS #########################################################
header_image = np.zeros((int(frame_height / 18), frame_width, 3), np.uint8)
header_image = CvDrawText.puttext(header_image, "FPS:" + str(fps_result),
(5, 0), font_path,
int(frame_height / 20), (255, 255, 255))
# フッター作成:印の履歴、および、術名表示 ####################################
footer_image = np.zeros((int(frame_height / 10), frame_width, 3), np.uint8)
# 印の履歴文字列生成
sign_display = ''
if len(sign_display_queue) > 0:
for sign_id in sign_display_queue:
sign_display = sign_display + labels[sign_id][1]
# 術名表示(指定時間描画)
if lang_offset == 0:
separate_string = '・'
else:
separate_string = ':'
if (time.time() - jutsu_start_time) < jutsu_display_time:
if jutsu[jutsu_index][0] == '': # 属性(火遁等)の定義が無い場合
jutsu_string = jutsu[jutsu_index][2 + lang_offset]
else: # 属性(火遁等)の定義が有る場合
jutsu_string = jutsu[jutsu_index][0 + lang_offset] + \
separate_string + jutsu[jutsu_index][2 + lang_offset]
footer_image = CvDrawText.puttext(
footer_image, jutsu_string, (5, 0), font_path,
int(frame_width / jutsu_font_size_ratio), (255, 255, 255))
# 印表示
else:
footer_image = CvDrawText.puttext(footer_image, sign_display, (5, 0),
font_path,
int(frame_width / sign_max_display),
(255, 255, 255))
# ヘッダーとフッターをデバッグ画像へ結合 ######################################
debug_image = cv.vconcat([header_image, debug_image])
debug_image = cv.vconcat([debug_image, footer_image])
return debug_image
if __name__ == '__main__':
main()