@@ -40,7 +40,8 @@ def __init__(self, pose: Pose, thickness=None):
40
40
except ImportError :
41
41
raise ImportError ("Please install OpenCV with: pip install opencv-python" )
42
42
43
- def _draw_frame (self , frame : ma .MaskedArray , frame_confidence : np .ndarray , img ,
43
+ def _draw_frame (self , frame : ma .MaskedArray ,
44
+ frame_confidence : np .ndarray , img ,
44
45
transparency : bool = False ) -> np .ndarray :
45
46
"""
46
47
Draw frame of pose data of an image.
@@ -74,7 +75,6 @@ def _draw_frame(self, frame: ma.MaskedArray, frame_confidence: np.ndarray, img,
74
75
75
76
for person , person_confidence in zip (frame , frame_confidence ):
76
77
c = person_confidence .tolist ()
77
- points = [p for p in person .tolist ()]
78
78
idx = 0
79
79
for component in self .pose .header .components :
80
80
colors = [np .array (c [::- 1 ]) for c in component .colors ]
@@ -91,7 +91,7 @@ def _point_color(p_i: int):
91
91
# Collect Points
92
92
for i , point_name in enumerate (component .points ):
93
93
if c [i + idx ] > 0 :
94
- center = points [i + idx ]
94
+ center = person [i + idx ]
95
95
draw_operations .append ({
96
96
'type' : 'circle' ,
97
97
'center' : center ,
@@ -103,8 +103,8 @@ def _point_color(p_i: int):
103
103
})
104
104
105
105
if self .pose .header .is_bbox :
106
- point1 = points [0 + idx ]
107
- point2 = points [1 + idx ]
106
+ point1 = person [0 + idx ]
107
+ point2 = person [1 + idx ]
108
108
color = tuple (np .mean ([_point_color (0 ), _point_color (1 )], axis = 0 ))
109
109
110
110
draw_operations .append ({
@@ -119,8 +119,8 @@ def _point_color(p_i: int):
119
119
# Collect Limbs
120
120
for (p1 , p2 ) in component .limbs :
121
121
if c [p1 + idx ] > 0 and c [p2 + idx ] > 0 :
122
- point1 = points [p1 + idx ]
123
- point2 = points [p2 + idx ]
122
+ point1 = person [p1 + idx ]
123
+ point2 = person [p2 + idx ]
124
124
125
125
color = tuple (np .mean ([_point_color (p1 ), _point_color (p2 )], axis = 0 ))
126
126
@@ -136,27 +136,30 @@ def _point_color(p_i: int):
136
136
137
137
idx += len (component .points )
138
138
139
- draw_operations = sorted (draw_operations , key = lambda op : op ['z' ])
139
+ draw_operations = sorted (draw_operations , key = lambda op : op ['z' ], reverse = True )
140
+
141
+ def point_to_xy (point : ma .MaskedArray ):
142
+ return tuple ([round (p ) for p in point [:2 ]])
140
143
141
144
# Execute draw operations
142
145
for op in draw_operations :
143
146
if op ['type' ] == 'circle' :
144
147
self .cv2 .circle (img = img ,
145
- center = tuple (op ['center' ][: 2 ]),
148
+ center = point_to_xy (op ['center' ]),
146
149
radius = op ['radius' ],
147
150
color = op ['color' ],
148
151
thickness = op ['thickness' ],
149
152
lineType = op ['lineType' ])
150
153
elif op ['type' ] == 'rectangle' :
151
154
self .cv2 .rectangle (img = img ,
152
- pt1 = tuple (op ['pt1' ][: 2 ]),
153
- pt2 = tuple (op ['pt2' ][: 2 ]),
155
+ pt1 = point_to_xy (op ['pt1' ]),
156
+ pt2 = point_to_xy (op ['pt2' ]),
154
157
color = op ['color' ],
155
158
thickness = op ['thickness' ])
156
159
elif op ['type' ] == 'line' :
157
160
self .cv2 .line (img ,
158
- pt1 = tuple (op ['pt1' ][: 2 ]),
159
- pt2 = tuple (op ['pt2' ][: 2 ]),
161
+ pt1 = point_to_xy (op ['pt1' ]),
162
+ pt2 = point_to_xy (op ['pt2' ]),
160
163
color = op ['color' ],
161
164
thickness = op ['thickness' ],
162
165
lineType = op ['lineType' ])
@@ -184,12 +187,11 @@ def draw(self, background_color: Tuple[int, int, int] = (255, 255, 255), max_fra
184
187
# ...
185
188
if transparency :
186
189
background_color += (0 ,)
187
- int_frames = np .array (np .around (self .pose .body .data .data ), dtype = "int32" )
188
190
background = np .full (
189
191
(self .pose .header .dimensions .height , self .pose .header .dimensions .width , len (background_color )),
190
192
fill_value = background_color ,
191
193
dtype = "uint8" )
192
- for frame , confidence in itertools .islice (zip (int_frames , self .pose .body .confidence ), max_frames ):
194
+ for frame , confidence in itertools .islice (zip (self . pose . body . data , self .pose .body .confidence ), max_frames ):
193
195
yield self ._draw_frame (frame , confidence , img = background .copy (), transparency = transparency )
194
196
195
197
def draw_on_video (self , background_video , max_frames : int = None , blur = False ):
@@ -471,9 +473,8 @@ def draw(self, background_color: int = 0, foreground_color: int = 255):
471
473
np.ndarray
472
474
frames with drawn pose
473
475
"""
474
- int_frames = np .array (np .around (self .pose .body .data .data ), dtype = "int32" )
475
476
background = np .full ((self .pose .header .dimensions .height , self .pose .header .dimensions .width ),
476
477
fill_value = background_color ,
477
478
dtype = "uint8" )
478
- for frame in int_frames :
479
+ for frame in self . pose . body . data :
479
480
yield self ._draw_frame (frame , img = background .copy (), color = foreground_color )
0 commit comments