-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path05_finger_frame_detection_demo.py
More file actions
178 lines (138 loc) · 6.25 KB
/
05_finger_frame_detection_demo.py
File metadata and controls
178 lines (138 loc) · 6.25 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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 utils import CvDrawText
from utils import CvOverlayImage
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0)
parser.add_argument("--fps", type=int, default=10)
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/efficient_det_fingerframe/saved_model')
parser.add_argument("--score_th", type=float, default=0.5)
args = parser.parse_args()
return args
def run_inference_single_image(image, inference_func):
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 main():
# 引数解析 #################################################################
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
# カメラ準備 ###############################################################
cap = cv.VideoCapture(cap_device)
cap.set(cv.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, cap_height)
video = cv.VideoCapture('utils/map.mp4')
# FPS計測モジュール ########################################################
cvFpsCalc = CvFpsCalc()
# モデルロード #############################################################
DEFAULT_FUNCTION_KEY = 'serving_default'
loaded_model = tf.saved_model.load(model_path)
inference_func = loaded_model.signatures[DEFAULT_FUNCTION_KEY]
buffer_len = 5
deque_x1 = deque(maxlen=buffer_len)
deque_y1 = deque(maxlen=buffer_len)
deque_x2 = deque(maxlen=buffer_len)
deque_y2 = deque(maxlen=buffer_len)
# フォント
font_path = './utils/font/x12y20pxScanLine.ttf'
while True:
# FPS算出 #############################################################
display_fps = cvFpsCalc.get()
start_time = time.time()
# カメラキャプチャ #####################################################
ret, frame = cap.read()
if not ret:
continue
frame_width, frame_height = frame.shape[1], frame.shape[0]
debug_image = copy.deepcopy(frame)
# 検出実施 #############################################################
frame = frame[:, :, [2, 1, 0]] # BGR2RGB
image_np_expanded = np.expand_dims(frame, axis=0)
output = run_inference_single_image(image_np_expanded, inference_func)
num_detections = output['num_detections']
for i in range(num_detections):
score = output['detection_scores'][i]
bbox = output['detection_boxes'][i]
# class_id = output['detection_classes'][i].astype(np.int)
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 = 0.15
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)
x1 = int((x1 - 5) / 10) * 10
y1 = int((y1 - 5) / 10) * 10
x2 = int((x2 + 5) / 10) * 10
y2 = int((y2 + 5) / 10) * 10
deque_x1.append(x1)
deque_y1.append(y1)
deque_x2.append(x2)
deque_y2.append(y2)
x1 = int(sum(deque_x1) / len(deque_x1))
y1 = int(sum(deque_y1) / len(deque_y1))
x2 = int(sum(deque_x2) / len(deque_x2))
y2 = int(sum(deque_y2) / len(deque_y2))
ret, video_frame = video.read()
if ret is not False:
video.grab()
video.grab()
debug_add_image = np.zeros((frame_height, frame_width, 3),
np.uint8)
map_resize_image = cv.resize(video_frame,
((x2 - x1), (y2 - y1)))
debug_add_image = CvOverlayImage.overlay(
debug_add_image, map_resize_image, (x1, y1))
debug_add_image = cv.cvtColor(debug_add_image,
cv.COLOR_BGRA2BGR)
# cv.imshow('1', debug_add_image)
debug_image = cv.addWeighted(debug_image, 1.0, debug_add_image,
2.0, 0)
else:
video = cv.VideoCapture('map.mp4')
# キー処理(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)
# 画面反映 #############################################################
fps_string = u"FPS:" + str(display_fps)
debug_image = CvDrawText.puttext(debug_image, fps_string, (17, 17),
font_path, 32, (255, 255, 255))
fps_string = u"FPS:" + str(display_fps)
debug_image = CvDrawText.puttext(debug_image, fps_string, (15, 15),
font_path, 32, (0, 56, 86))
cv.imshow('Demo', debug_image)
cap.release()
cv.destroyAllWindows()
if __name__ == '__main__':
main()