-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
232 lines (194 loc) · 8.1 KB
/
predict.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
from ultralytics import YOLO
import os
import numpy as np
import cv2
import argparse
import json
ROOT_DIR = os.path.abspath("./")
OUTPUT_DIR = os.path.join(ROOT_DIR, "output")
colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(255, 0, 255), # Magenta
(0, 255, 255), # Cyan
(255, 165, 0), # Orange
(128, 0, 128), # Purple
(218, 170, 34), # Gold
(0, 128, 128), # Dark Cyan
(176, 224, 230),# Light Blue
(189, 183, 117),# Chartreuse
(128, 128, 0), # Olive
(255, 192, 203),# Pink
(0, 255, 0), # Lime
(255, 140, 0), # Dark Orange
(0, 0, 128), # Navy Blue
(255, 69, 0), # Red-Orange
(128, 0, 0), # Maroon
(0, 128, 0), # Dark Green
(128, 0, 0), # Brown
(255, 255, 255),# White
(192, 192, 192),# Light Gray
(0, 0, 0), # Black
(70, 130, 180), # Steel Blue
(255, 99, 71), # Tomato
(0, 128, 255), # Royal Blue
(255, 20, 147), # Deep Pink
(255, 215, 0), # Gold
(0, 255, 128), # Spring Green
(139, 69, 19), # Saddle Brown
(205, 92, 92) # Indian Red
]
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
def solve_image(mode, image):
result = model(image)
boxes = result[0].boxes
keypoints = result[0].keypoints
xy = keypoints.xy.cpu().numpy()
xy = [[[idx, float(kp[0]), float(kp[1])] for idx, kp in enumerate(kps) if not (kp[0] == 0 and kp[1] == 0)] for kps in xy]
xyxy = boxes.xyxy.cpu().numpy().astype(float).tolist()
score = boxes.conf.cpu().numpy().astype(float).tolist()
cls_id = boxes.cls.cpu().numpy()
names = result[0].names
cls_name = [str(names[idx]) for idx in cls_id]
return {
'keypoints': xy,
'bbox': xyxy,
'score': score,
'category': cls_name
}
def solve_folder(source, render):
for file in os.listdir(source):
file_path = os.path.join(source, file)
if file_path.endswith(('mp4', 'avi', 'jpg', 'png')):
cap = cv2.VideoCapture(file_path) if file_path.endswith(('mp4', 'avi')) else None
image = cv2.imread(file_path) if not cap else None
rets = []
if cap is not None:
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
if args.render:
video_path = os.path.join(OUTPUT_DIR, f'{file}-keypoints.mp4')
out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
cnt = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
ret = solve_image(model, frame)
rets.append(ret)
if args.render:
for box in ret['bbox']:
cv2.rectangle(frame, tuple(map(int, box[:2])), tuple(map(int, box[2:])), (0, 0, 255), 2)
for kps in ret['keypoints']:
for kp in kps:
cv2.circle(frame, tuple(map(int, kp[1:])), 3, colors[int(kp[0]) % len(colors)], -1)
out.write(frame)
print(f'Frame {cnt} finished')
cnt += 1
json_path = os.path.join(OUTPUT_DIR, f'{file}-keypoints.json')
with open(json_path, 'w') as f:
json.dump(rets, f)
print(f'Save predictions to {json_path}')
if args.render:
out.release()
print(f'Save rendered video to {video_path}')
if image is not None:
ret = solve_image(model, image)
rets.append(ret)
json_path = os.path.join(OUTPUT_DIR, f'{file}-keypoints.json')
with open(json_path, 'w') as f:
json.dump(rets, f)
print(f'Save predictions to {json_path}')
if args.render:
for box in ret['bbox']:
cv2.rectangle(image, tuple(map(int, box[:2])), tuple(map(int, box[2:])), (0, 0, 255), 2)
for kps in ret['keypoints']:
for kp in kps:
cv2.circle(image, tuple(map(int, kp[1:])), 3, colors[int(kp[0]) % len(colors)], -1)
img_path = os.path.join(OUTPUT_DIR, f'{file}-keypoints.jpg')
cv2.imwrite(img_path, image)
print(f'Save rendered image to {img_path}')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('source', type=str, help='Path of the source (can be image or video or folder)')
parser.add_argument('--model', help='Path of model weights', required=True)
parser.add_argument("--render", help='Save the detections to another image or video', action='store_true')
args = parser.parse_args()
if not os.path.exists(args.model):
print('Model path not exist!')
exit(0)
# Load a model
model = YOLO(args.model) # pretrained model
# Load source
source = args.source
cap = None
image = None
if os.path.isdir(source):
solve_folder(source, args.render)
elif os.path.exists(source):
if source.endswith(('mp4', 'avi')):
cap = cv2.VideoCapture(source)
elif source.endswith(('jpg', 'png', 'bmp')):
image = cv2.imread(source)
else:
print('Bad file format.')
exit(0)
else:
print('Source path not exists.')
exit(0)
rets = []
if cap is not None:
ext = os.path.splitext(os.path.basename(source))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
if args.render:
video_path = os.path.join(OUTPUT_DIR,'{0}-keypoints.mp4'.format(ext[0]))
out = cv2.VideoWriter(video_path, fourcc, fps, (width,height))
cnt = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
ret = solve_image(model, frame)
rets.append(ret)
if args.render:
for box in ret['bbox']:
cv2.rectangle(frame, tuple(map(int, box[:2])), tuple(map(int, box[2:])), (0, 0, 255), 2)
for kps in ret['keypoints']:
for kp in kps:
cv2.circle(frame, tuple(map(int, kp[1:])), 3, colors[int(kp[0])][::-1], -1)
out.write(frame)
print('Frame {} finished'.format(cnt))
cnt += 1
json_path = os.path.join(OUTPUT_DIR, f'{file}-keypoints.json')
with open(json_path, 'w') as f:
json.dump(rets, f)
print(f'Save predictions to {json_path}')
if args.render:
out.release()
print('Save rendered video to {}'.format(video_path))
if image is not None:
ext = os.path.splitext(os.path.basename(source))
ret = solve_image(model, image)
rets.append(ret)
json_path = os.path.join(OUTPUT_DIR, f'{file}-keypoints.json')
with open(json_path, 'w') as f:
json.dump(rets, f)
print(f'Save predictions to {json_path}')
if args.render:
for box in ret['bbox']:
cv2.rectangle(image, tuple(map(int, box[:2])), tuple(map(int, box[2:])), (0, 0, 255), 2)
for kps in ret['keypoints']:
for kp in kps:
if int(kp[0]) in [24, 25, 26, 27, 28, 29, 30, 31]:
cv2.circle(image, tuple(map(int, kp[1:])), 3, colors[int(kp[0])][::-1], -1)
img_path = os.path.join(OUTPUT_DIR,'{0}-keypoints{1}'.format(ext[0], ext[1]))
cv2.imwrite(img_path, image)
print('Save rendered image to {}'.format(img_path))