-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
87 lines (68 loc) · 2.94 KB
/
sample.py
File metadata and controls
87 lines (68 loc) · 2.94 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import copy
import time
import cv2 as cv
def main():
# グラフ読み込み ###########################################################
pb_filepath = os.path.join(os.getcwd(), 'model',
'frozen_inference_graph.pb')
pbtxt_filepath = os.path.join(os.getcwd(), 'model', 'graph.pbtxt')
cvNet = cv.dnn.readNetFromTensorflow(pb_filepath, pbtxt_filepath)
# カメラ準備 ###############################################################
cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 480)
fps = 10
while True:
start_time = time.time()
# カメラキャプチャ #####################################################
ret, frame = cap.read()
if not ret:
continue
debug_image = copy.deepcopy(frame)
rows = debug_image.shape[0]
cols = debug_image.shape[1]
# 手検出実施 ###########################################################
blob_image = cv.dnn.blobFromImage(
debug_image, size=(512, 512), swapRB=True, crop=False)
cvNet.setInput(blob_image)
cvOut = cvNet.forward()
for detection in cvOut[0, 0, :, :]:
class_id = detection[1]
score = float(detection[2])
if score < 0.75:
continue
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
# バウンディングボックス描画 #######################################
cv.putText(debug_image,
str(class_id) + ":" + '{:.3f}'.format(score),
(int(left), int(top) - 15), cv.FONT_HERSHEY_SIMPLEX,
1.0, (0, 255, 0), 2, cv.LINE_AA)
cv.rectangle(
debug_image, (int(left), int(top)), (int(right), int(bottom)),
(0, 255, 0),
thickness=2)
# キー処理(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)
# 画面反映 #############################################################
cv.putText(
debug_image,
"Elapsed Time:" + '{:.1f}'.format(elapsed_time * 1000) + "ms",
(10, 50), cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2, cv.LINE_AA)
cv.imshow(' ', debug_image)
cv.moveWindow(' ', 100, 100)
cap.release()
cv.destroyAllWindows()
if __name__ == '__main__':
main()