Skip to content

Commit 82fbf18

Browse files
authored
Merge pull request #28 from AmitMY/draw-fast
feat(visualizer): add fast drawing function
2 parents bc27ed2 + 95f1760 commit 82fbf18

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

pose_format/pose_visualizer.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _draw_frame(self, frame: ma.MaskedArray, frame_confidence: np.ndarray, img)
3333

3434
for person, person_confidence in zip(frame, frame_confidence):
3535
c = person_confidence.tolist()
36+
points_2d = [tuple(p) for p in person[:, :2].tolist()]
3637
idx = 0
3738
for component in self.pose.header.components:
3839
colors = [np.array(c[::-1]) for c in component.colors]
@@ -50,42 +51,33 @@ def _point_color(p_i: int):
5051
color=_point_color(i), thickness=-1, lineType=16)
5152

5253
if self.pose.header.is_bbox:
53-
point1 = tuple(person[0 + idx].tolist())
54-
point2 = tuple(person[1 + idx].tolist())
54+
point1 = points_2d[0 + idx]
55+
point2 = points_2d[1 + idx]
5556
color = tuple(np.mean([_point_color(0), _point_color(1)], axis=0))
5657

5758
self.cv2.rectangle(img=img, pt1=point1, pt2=point2, color=color, thickness=thickness)
5859
else:
59-
int_person = person.astype(np.int32)
6060
# Draw Limbs
6161
for (p1, p2) in component.limbs:
6262
if c[p1 + idx] > 0 and c[p2 + idx] > 0:
63-
point1 = tuple(int_person[p1 + idx].tolist()[:2])
64-
point2 = tuple(int_person[p2 + idx].tolist()[:2])
63+
point1 = points_2d[p1 + idx]
64+
point2 = points_2d[p2 + idx]
6565

6666
# length = ((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2) ** 0.5
6767

6868
color = tuple(np.mean([_point_color(p1), _point_color(p2)], axis=0))
6969

7070
self.cv2.line(img, point1, point2, color, thickness, lineType=self.cv2.LINE_AA)
7171

72-
# deg = math.degrees(math.atan2(point1[1] - point2[1], point1[0] - point2[0]))
73-
# polygon = cv2.ellipse2Poly(
74-
# (int((point1[0] + point2[0]) / 2), int((point1[1] + point2[1]) / 2)),
75-
# (int(length / 2), thickness),
76-
# int(deg),
77-
# 0, 360, 1)
78-
# cv2.fillConvexPoly(img=img, points=polygon, color=color)
79-
8072
idx += len(component.points)
8173

8274
return img
8375

8476
def draw(self, background_color: Tuple[int, int, int] = (255, 255, 255), max_frames: int = None):
85-
int_data = np.array(np.around(self.pose.body.data.data), dtype="int32")
77+
int_frames = np.array(np.around(self.pose.body.data.data), dtype="int32")
8678
background = np.full((self.pose.header.dimensions.height, self.pose.header.dimensions.width, 3),
8779
fill_value=background_color, dtype="uint8")
88-
for frame, confidence in itertools.islice(zip(int_data, self.pose.body.confidence), max_frames):
80+
for frame, confidence in itertools.islice(zip(int_frames, self.pose.body.confidence), max_frames):
8981
yield self._draw_frame(frame, confidence, img=background.copy())
9082

9183
def draw_on_video(self, background_video, max_frames: int = None, blur=False):
@@ -162,3 +154,34 @@ def save_video(self, f_name: str, frames: Iterable[np.ndarray], custom_ffmpeg=No
162154
out.write(frame)
163155

164156
out.close()
157+
158+
159+
class FastAndUglyPoseVisualizer(PoseVisualizer):
160+
"""
161+
This class draws all frames as grayscale, without opacity based on confidence
162+
"""
163+
164+
def _draw_frame(self, frame: ma.MaskedArray, img, color: int):
165+
ignored_point = (0, 0)
166+
# Note: this can be made faster by drawing polylines instead of lines
167+
thickness = 1
168+
for person in frame:
169+
points_2d = [tuple(p) for p in person[:, :2].tolist()]
170+
idx = 0
171+
for component in self.pose.header.components:
172+
for (p1, p2) in component.limbs:
173+
point1 = points_2d[p1 + idx]
174+
point2 = points_2d[p2 + idx]
175+
if point1 != ignored_point and point2 != ignored_point:
176+
# Antialiasing is a bit slow, but necessary
177+
self.cv2.line(img, point1, point2, color, thickness, lineType=self.cv2.LINE_AA)
178+
179+
idx += len(component.points)
180+
return img
181+
182+
def draw(self, background_color: int = 0, foreground_color: int = 255):
183+
int_frames = np.array(np.around(self.pose.body.data.data), dtype="int32")
184+
background = np.full((self.pose.header.dimensions.height, self.pose.header.dimensions.width),
185+
fill_value=background_color, dtype="uint8")
186+
for frame in int_frames:
187+
yield self._draw_frame(frame, img=background.copy(), color=foreground_color)

0 commit comments

Comments
 (0)