-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
167 lines (146 loc) · 5.48 KB
/
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
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility functions to display the pose detection results."""
import math
from typing import List, Tuple
import cv2
from data import Person
import numpy as np
# map edges to a RGB color
KEYPOINT_EDGE_INDS_TO_COLOR = {
(0, 1): (147, 20, 255),
(0, 2): (255, 255, 0),
(1, 3): (147, 20, 255),
(2, 4): (255, 255, 0),
(0, 5): (147, 20, 255),
(0, 6): (255, 255, 0),
(5, 7): (147, 20, 255),
(7, 9): (147, 20, 255),
(6, 8): (255, 255, 0),
(8, 10): (255, 255, 0),
(5, 6): (0, 255, 255),
(5, 11): (147, 20, 255),
(6, 12): (255, 255, 0),
(11, 12): (0, 255, 255),
(11, 13): (147, 20, 255),
(13, 15): (147, 20, 255),
(12, 14): (255, 255, 0),
(14, 16): (255, 255, 0)
}
# A list of distictive colors
COLOR_LIST = [
(47, 79, 79),
(139, 69, 19),
(0, 128, 0),
(0, 0, 139),
(255, 0, 0),
(255, 215, 0),
(0, 255, 0),
(0, 255, 255),
(255, 0, 255),
(30, 144, 255),
(255, 228, 181),
(255, 105, 180),
]
def visualize(
image: np.ndarray,
list_persons: List[Person],
keypoint_color: Tuple[int, ...] = None,
keypoint_threshold: float = 0.05,
instance_threshold: float = 0.1,
) -> np.ndarray:
"""Draws landmarks and edges on the input image and return it.
Args:
image: The input RGB image.
list_persons: The list of all "Person" entities to be visualize.
keypoint_color: the colors in which the landmarks should be plotted.
keypoint_threshold: minimum confidence score for a keypoint to be drawn.
instance_threshold: minimum confidence score for a person to be drawn.
Returns:
Image with keypoints and edges.
"""
for person in list_persons:
if person.score < instance_threshold:
continue
keypoints = person.keypoints
bounding_box = person.bounding_box
# Assign a color to visualize keypoints.
if keypoint_color is None:
if person.id is None:
# If there's no person id, which means no tracker is enabled, use
# a default color.
person_color = (0, 255, 0)
else:
# If there's a person id, use different color for each person.
person_color = COLOR_LIST[person.id % len(COLOR_LIST)]
else:
person_color = keypoint_color
# Draw all the landmarks
for i in range(len(keypoints)):
if keypoints[i].score >= keypoint_threshold:
cv2.circle(image, keypoints[i].coordinate, 2, person_color, 4)
# Draw all the edges
for edge_pair, edge_color in KEYPOINT_EDGE_INDS_TO_COLOR.items():
if (keypoints[edge_pair[0]].score > keypoint_threshold and
keypoints[edge_pair[1]].score > keypoint_threshold):
cv2.line(image, keypoints[edge_pair[0]].coordinate,
keypoints[edge_pair[1]].coordinate, edge_color, 2)
# Draw bounding_box with multipose
if bounding_box is not None:
start_point = bounding_box.start_point
end_point = bounding_box.end_point
cv2.rectangle(image, start_point, end_point, person_color, 2)
# Draw id text when tracker is enabled for MoveNet MultiPose model.
# (id = None when using single pose model or when tracker is None)
if person.id:
id_text = 'id = ' + str(person.id)
cv2.putText(image, id_text, start_point, cv2.FONT_HERSHEY_PLAIN, 1,
(0, 0, 255), 1)
return image
def keep_aspect_ratio_resizer(
image: np.ndarray, target_size: int) -> Tuple[np.ndarray, Tuple[int, int]]:
"""Resizes the image.
The function resizes the image such that its longer side matches the required
target_size while keeping the image aspect ratio. Note that the resizes image
is padded such that both height and width are a multiple of 32, which is
required by the model. See
https://tfhub.dev/google/tfjs-model/movenet/multipose/lightning/1 for more
detail.
Args:
image: The input RGB image as a numpy array of shape [height, width, 3].
target_size: Desired size that the image should be resize to.
Returns:
image: The resized image.
(target_height, target_width): The actual image size after resize.
"""
height, width, _ = image.shape
if height > width:
scale = float(target_size / height)
target_height = target_size
scaled_width = math.ceil(width * scale)
image = cv2.resize(image, (scaled_width, target_height))
target_width = int(math.ceil(scaled_width / 32) * 32)
else:
scale = float(target_size / width)
target_width = target_size
scaled_height = math.ceil(height * scale)
image = cv2.resize(image, (target_width, scaled_height))
target_height = int(math.ceil(scaled_height / 32) * 32)
padding_top, padding_left = 0, 0
padding_bottom = target_height - image.shape[0]
padding_right = target_width - image.shape[1]
# add padding to image
image = cv2.copyMakeBorder(image, padding_top, padding_bottom, padding_left,
padding_right, cv2.BORDER_CONSTANT)
return image, (target_height, target_width)