-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathsample_objectron.py
160 lines (124 loc) · 5.62 KB
/
sample_objectron.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
import argparse
import cv2 as cv
import numpy as np
import mediapipe as mp
from utils import CvFpsCalc
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('--static_image_mode', action='store_true')
parser.add_argument("--max_num_objects",
help='max_num_objects',
type=int,
default=5)
parser.add_argument("--min_detection_confidence",
help='min_detection_confidence',
type=float,
default=0.5)
parser.add_argument("--min_tracking_confidence",
help='min_tracking_confidence',
type=int,
default=0.99)
parser.add_argument("--model_name",
help='model_name',
type=str,
default='Cup') # {'Shoe', 'Chair', 'Cup', 'Camera'}
args = parser.parse_args()
return args
def main():
# 引数解析 #################################################################
args = get_args()
cap_device = args.device
cap_width = args.width
cap_height = args.height
static_image_mode = args.static_image_mode
max_num_objects = args.max_num_objects
min_detection_confidence = args.min_detection_confidence
min_tracking_confidence = args.min_tracking_confidence
model_name = args.model_name
# カメラ準備 ###############################################################
cap = cv.VideoCapture(cap_device)
cap.set(cv.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, cap_height)
# モデルロード #############################################################
mp_objectron = mp.solutions.objectron
objectron = mp_objectron.Objectron(
static_image_mode=static_image_mode,
max_num_objects=max_num_objects,
min_detection_confidence=min_detection_confidence,
min_tracking_confidence=min_tracking_confidence,
model_name=model_name,
)
mp_drawing = mp.solutions.drawing_utils
# FPS計測モジュール ########################################################
cvFpsCalc = CvFpsCalc(buffer_len=10)
while True:
display_fps = cvFpsCalc.get()
# カメラキャプチャ #####################################################
ret, image = cap.read()
if not ret:
break
image = cv.flip(image, 1) # ミラー表示
debug_image = copy.deepcopy(image)
# 検出実施 #############################################################
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
results = objectron.process(image)
# 描画 ################################################################
if results.detected_objects is not None:
for detected_object in results.detected_objects:
mp_drawing.draw_landmarks(debug_image,
detected_object.landmarks_2d,
mp_objectron.BOX_CONNECTIONS)
mp_drawing.draw_axis(debug_image, detected_object.rotation,
detected_object.translation)
# キーポイント確認用
draw_landmarks(debug_image, detected_object.landmarks_2d)
cv.putText(debug_image, "FPS:" + str(display_fps), (10, 30),
cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2, cv.LINE_AA)
# キー処理(ESC:終了) #################################################
key = cv.waitKey(1)
if key == 27: # ESC
break
# 画面反映 #############################################################
cv.imshow('MediaPipe Objectron Demo', debug_image)
cap.release()
cv.destroyAllWindows()
def draw_landmarks(image, landmarks):
image_width, image_height = image.shape[1], image.shape[0]
landmark_point = []
for index, landmark in enumerate(landmarks.landmark):
landmark_x = min(int(landmark.x * image_width), image_width - 1)
landmark_y = min(int(landmark.y * image_height), image_height - 1)
landmark_point.append([(landmark_x, landmark_y)])
if index == 0: # 重心
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 1: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 2: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 3: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 4: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 5: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 6: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 7: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
if index == 8: #
cv.circle(image, (landmark_x, landmark_y), 5, (0, 255, 0), 2)
return image
def draw_bounding_rect(use_brect, image, brect):
if use_brect:
# 外接矩形
cv.rectangle(image, (brect[0], brect[1]), (brect[2], brect[3]),
(0, 255, 0), 2)
return image
if __name__ == '__main__':
main()