forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefficientpose_utils.py
346 lines (282 loc) · 12.6 KB
/
efficientpose_utils.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
import cv2
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.special import expit
from scipy.ndimage.filters import gaussian_filter
EFFICIENT_POSE_KEYPOINT_HEAD_TOP = (0)
EFFICIENT_POSE_KEYPOINT_UPPER_NECK = (1)
EFFICIENT_POSE_KEYPOINT_RIGHT_SHOULDER = (2)
EFFICIENT_POSE_KEYPOINT_RIGHT_ELBOW = (3)
EFFICIENT_POSE_KEYPOINT_RIGHT_WRIST = (4)
EFFICIENT_POSE_KEYPOINT_THORAX = (5)
EFFICIENT_POSE_KEYPOINT_LEFT_SHOULDER = (6)
EFFICIENT_POSE_KEYPOINT_LEFT_ELBOW = (7)
EFFICIENT_POSE_KEYPOINT_LEFT_RIGHT_WRIST = (8)
EFFICIENT_POSE_KEYPOINT_PELVIS = (9)
EFFICIENT_POSE_KEYPOINT_RIGHT_HIP = (10)
EFFICIENT_POSE_KEYPOINT_RIGHT_KNEE = (11)
EFFICIENT_POSE_KEYPOINT_RIGHT_ANKLE = (12)
EFFICIENT_POSE_KEYPOINT_LEFT_HIP = (13)
EFFICIENT_POSE_KEYPOINT_LEFT_KNEE = (14)
EFFICIENT_POSE_KEYPOINT_LEFT_ANKLE = (15)
EFFICIENT_POSE_KEYPOINT_CNT = 16
def resize(source_array, target_height, target_width):
"""
Resizes an image or image-like Numpy array to be no larger than (target_height, target_width) or (target_height, target_width, c).
Args:
source_array: ndarray
Numpy array of shape (h, w) or (h, w, 3)
target_height: int
Desired maximum height
target_width: int
Desired maximum width
Returns:
Resized Numpy array.
"""
# Get height and width of source array
source_height, source_width = source_array.shape[:2]
# Compute correct scale for resizing operation
target_ratio = target_height / target_width
source_ratio = source_height / source_width
if target_ratio > source_ratio:
scale = target_width / source_width
else:
scale = target_height / source_height
# Perform rescaling
width = int(source_width * scale)
height = int(source_height * scale)
dim = (width, height)
# resize image
resized_array = cv2.resize(source_array, dim, interpolation = cv2.INTER_AREA)/255.
# resized_array = rescale(source_array, scale, multichannel=True)
return resized_array
def pad(source_array, target_height, target_width):
"""
Pads an image or image-like Numpy array with zeros to fit the target-size.
Args:
source_array: ndarray
Numpy array of shape (h, w) or (h, w, 3)
target_height: int
Height of padded image
target_width: int
Width of padded image
Returns:
Zero-padded Numpy array of shape (target_height, target_width) or (target_height, target_width, c).
"""
# Get height and width of source array
source_height, source_width = source_array.shape[:2]
# Ensure array is resized properly
if (source_height > target_height) or (source_width > target_width):
source_array = resize(source_array, target_height, target_width)
source_height, source_width = source_array.shape[:2]
# Compute padding variables
pad_left = int((target_width - source_width) / 2)
pad_top = int((target_height - source_height) / 2)
pad_right = int(target_width - source_width - pad_left)
pad_bottom = int(target_height - source_height - pad_top)
paddings = [[pad_top, pad_bottom], [pad_left, pad_right]]
has_channels_dim = len(source_array.shape) == 3
if has_channels_dim:
paddings.append([0,0])
# Perform padding
target_array = np.pad(source_array, paddings, 'constant')
return target_array
def normalize(x):
"""Preprocesses a Numpy array encoding a batch of images.
Arguments:
x: Input array, 3D or 4D.
Returns:
Preprocessed Numpy array.
"""
x /= 255.
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
# Zero-center by mean pixel
x[..., 0] -= mean[0]
x[..., 1] -= mean[1]
x[..., 2] -= mean[2]
if std is not None:
x[..., 0] /= std[0]
x[..., 1] /= std[1]
x[..., 2] /= std[2]
return x
def preprocess(batch, resolution, lite=False):
"""
Preprocess Numpy array according to model preferences.
Args:
batch: ndarray
Numpy array of shape (n, h, w, 3)
resolution: int
Input height and width of model to utilize
lite: boolean
Defines if EfficientPose Lite model is used
Returns:
Preprocessed Numpy array of shape (n, resolution, resolution, 3).
"""
batch = [resize(frame, resolution, resolution) for frame in batch]
# Pad frames in batch to form quadratic input
batch = [pad(frame, resolution, resolution) for frame in batch]
# Convert from normalized pixels to RGB absolute values
batch = [np.uint8(255 * frame) for frame in batch]
# Construct Numpy array from batch
batch = np.asarray(batch).astype('float')
# Normalize images in batch
batch = normalize(batch)
return batch
def extract_coordinates(frame_output, frame_height, frame_width, real_time=False):
"""
Extract coordinates from supplied confidence maps.
Args:
frame_output: ndarray
Numpy array of shape (h, w, c)
frame_height: int
Height of relevant frame
frame_width: int
Width of relevant frame
real-time: boolean
Defines if processing is performed in real-time
Returns:
List of predicted coordinates for all c body parts in the frame the outputs are computed from.
"""
# Define body parts
body_parts = ['head_top', 'upper_neck', 'right_shoulder', 'right_elbow', 'right_wrist', 'thorax', 'left_shoulder', 'left_elbow', 'left_wrist', 'pelvis', 'right_hip', 'right_knee', 'right_ankle', 'left_hip', 'left_knee', 'left_ankle']
# Define confidence level
confidence = 0.3
# Fetch output resolution
output_height, output_width = frame_output.shape[0:2]
# Initialize coordinates
frame_coords = []
# Iterate over body parts
for i in range(frame_output.shape[-1]):
# Find peak point
conf = frame_output[...,i]
if not real_time:
conf = gaussian_filter(conf, sigma=1.)
max_index = np.argmax(conf)
peak_y = float(math.floor(max_index / output_width))
peak_x = max_index % output_width
conf_xy = conf[int(peak_y),int(peak_x)]
# Verify confidence
# if real_time and conf_xy < confidence:
# peak_x = -0.5
# peak_y = -0.5
# else:
# peak_x += 0.5
# peak_y += 0.5
peak_x += 0.5
peak_y += 0.5
# Normalize coordinates
peak_x /= output_width
peak_y /= output_height
# Convert to original aspect ratio
if frame_width > frame_height:
norm_padding = (frame_width - frame_height) / (2 * frame_width)
peak_y = (peak_y - norm_padding) / (1.0 - (2 * norm_padding))
peak_y = -0.5 / output_height if peak_y < 0.0 else peak_y
peak_y = 1.0 if peak_y > 1.0 else peak_y
elif frame_width < frame_height:
norm_padding = (frame_height - frame_width) / (2 * frame_height)
peak_x = (peak_x - norm_padding) / (1.0 - (2 * norm_padding))
peak_x = -0.5 / output_width if peak_x < 0.0 else peak_x
peak_x = 1.0 if peak_x > 1.0 else peak_x
frame_coords.append((body_parts[i], peak_x, peak_y, conf_xy))
return frame_coords
def display_body_parts(image, image_draw, coordinates, image_height=1024, image_width=1024, marker_radius=5):
"""
Draw markers on predicted body part locations.
Args:
image: PIL Image
The loaded image the coordinate predictions are inferred for
image_draw: PIL ImageDraw module
Module for performing drawing operations
coordinates: List
Predicted body part coordinates in image
image_height: int
Height of image
image_width: int
Width of image
marker_radius: int
Radius of marker
Returns:
Instance of PIL image with annotated body part predictions.
"""
# Define body part colors
body_part_colors = ['#fff142', '#fff142', '#576ab1', '#5883c4', '#56bdef', '#f19718', '#d33592', '#d962a6', '#e18abd', '#f19718', '#8ac691', '#a3d091', '#bedb8f', '#7b76b7', '#907ab8', '#a97fb9']
# Draw markers
for i, (body_part, body_part_x, body_part_y, conf) in enumerate(coordinates):
body_part_x *= image_width
body_part_y *= image_height
image_draw.ellipse([(body_part_x - marker_radius, body_part_y - marker_radius), (body_part_x + marker_radius, body_part_y + marker_radius)], fill=body_part_colors[i])
return image
def display_segments(image, image_draw, coordinates, image_height=1024, image_width=1024, segment_width=5):
"""
Draw segments between body parts according to predicted body part locations.
Args:
image: PIL Image
The loaded image the coordinate predictions are inferred for
image_draw: PIL ImageDraw module
Module for performing drawing operations
coordinates: List
Predicted body part coordinates in image
image_height: int
Height of image
image_width: int
Width of image
segment_width: int
Width of association line between markers
Returns:
Instance of PIL image with annotated body part segments.
"""
# Define segments and colors
segments = [(0, 1), (1, 5), (5, 2), (5, 6), (5, 9), (2, 3), (3, 4), (6, 7), (7, 8), (9, 10), (9, 13), (10, 11), (11, 12), (13, 14), (14, 15)]
segment_colors = ['#fff142', '#fff142', '#576ab1', '#5883c4', '#56bdef', '#f19718', '#d33592', '#d962a6', '#e18abd', '#f19718', '#8ac691', '#a3d091', '#bedb8f', '#7b76b7', '#907ab8', '#a97fb9']
# Draw segments
for (body_part_a_index, body_part_b_index) in segments:
_, body_part_a_x, body_part_a_y, conf_a = coordinates[body_part_a_index]
body_part_a_x *= image_width
body_part_a_y *= image_height
_, body_part_b_x, body_part_b_y, conf_b = coordinates[body_part_b_index]
body_part_b_x *= image_width
body_part_b_y *= image_height
image_draw.line([(body_part_a_x, body_part_a_y), (body_part_b_x, body_part_b_y)], fill=segment_colors[body_part_b_index], width=segment_width)
return image
def display_camera(cv2, frame, coordinates, frame_height, frame_width):
"""
Display camera frame with annotated body parts and segments according to predicted body part locations.
Args:
cv2: OpenCV
Imported OpenCV instance
frame: ndarray
Numpy array of shape (h, w, 3)
coordinates: List
Predicted body part coordinates in frame
frame_height: int
Height of frame
frame_width: int
Width of frame
"""
# Define body parts and segments
segments = [(0, 1), (1, 5), (5, 2), (5, 6), (5, 9), (2, 3), (3, 4), (6, 7), (7, 8), (9, 10), (9, 13), (10, 11), (11, 12), (13, 14), (14, 15)]
body_part_colors = [(66, 241, 255), (66, 241, 255), (177, 106, 87), (196, 131, 88), (239, 189, 86), (24, 151, 241), (146, 53, 211), (166, 98, 217), (189, 138, 225), (24, 151, 241), (145, 198, 138), (145, 208, 163), (143, 219, 190), (183, 118, 123), (184, 122, 144), (185, 127, 169)]
# Draw lines and markers
remaining = [i for i in range(len(body_part_colors))]
for (a, b) in segments:
a_coordinates = coordinates[a]
a_coordinate_x = int(a_coordinates[1] * frame_width)
a_coordinate_y = int(a_coordinates[2] * frame_height)
b_coordinates = coordinates[b]
b_coordinate_x = int(b_coordinates[1] * frame_width)
b_coordinate_y = int(b_coordinates[2] * frame_height)
if not (a_coordinate_x < 0 or a_coordinate_y < 0 or b_coordinate_x < 0 or b_coordinate_y < 0):
cv2.line(frame, (a_coordinate_x, a_coordinate_y), (b_coordinate_x, b_coordinate_y), color=body_part_colors[a], thickness=2)
if a in remaining:
cv2.circle(frame, (a_coordinate_x, a_coordinate_y), radius=3, color=body_part_colors[a], thickness=2)
remaining.remove(a)
if b in remaining:
cv2.circle(frame, (b_coordinate_x, b_coordinate_y), radius=3, color=body_part_colors[b], thickness=2)
remaining.remove(b)
# Display predictions
frame = cv2.resize(cv2.flip(frame, 1), (1000, 1000))
cv2.imshow('EfficientPose (Groos et al., 2020)', frame)