@@ -218,54 +218,79 @@ def draw_segm(image,
218218 return Image .fromarray (img_array .astype ('uint8' ))
219219
220220
221- def map_coco_to_personlab (keypoints ):
222- permute = [0 , 6 , 8 , 10 , 5 , 7 , 9 , 12 , 14 , 16 , 11 , 13 , 15 , 2 , 1 , 4 , 3 ]
223- return keypoints [:, permute , :]
224-
225-
226- def draw_pose (image , results , visual_thread = 0.6 , save_name = 'pose.jpg' ):
221+ def draw_pose (image ,
222+ results ,
223+ visual_thread = 0.6 ,
224+ save_name = 'pose.jpg' ,
225+ save_dir = 'output' ,
226+ returnimg = False ,
227+ ids = None ):
227228 try :
228229 import matplotlib .pyplot as plt
229230 import matplotlib
230231 plt .switch_backend ('agg' )
231232 except Exception as e :
232- logger .error ('Matplotlib not found, plaese install matplotlib.'
233+ logger .error ('Matplotlib not found, please install matplotlib.'
233234 'for example: `pip install matplotlib`.' )
234235 raise e
235- EDGES = [(0 , 14 ), (0 , 13 ), (0 , 4 ), (0 , 1 ), (14 , 16 ), (13 , 15 ), (4 , 10 ),
236- (1 , 7 ), (10 , 11 ), (7 , 8 ), (11 , 12 ), (8 , 9 ), (4 , 5 ), (1 , 2 ), (5 , 6 ),
237- (2 , 3 )]
236+
237+ skeletons = np .array ([item ['keypoints' ] for item in results ])
238+ kpt_nums = 17
239+ if len (skeletons ) > 0 :
240+ kpt_nums = int (skeletons .shape [1 ] / 3 )
241+ skeletons = skeletons .reshape (- 1 , kpt_nums , 3 )
242+ if kpt_nums == 17 : #plot coco keypoint
243+ EDGES = [(0 , 1 ), (0 , 2 ), (1 , 3 ), (2 , 4 ), (3 , 5 ), (4 , 6 ), (5 , 7 ), (6 , 8 ),
244+ (7 , 9 ), (8 , 10 ), (5 , 11 ), (6 , 12 ), (11 , 13 ), (12 , 14 ),
245+ (13 , 15 ), (14 , 16 ), (11 , 12 )]
246+ else : #plot mpii keypoint
247+ EDGES = [(0 , 1 ), (1 , 2 ), (3 , 4 ), (4 , 5 ), (2 , 6 ), (3 , 6 ), (6 , 7 ), (7 , 8 ),
248+ (8 , 9 ), (10 , 11 ), (11 , 12 ), (13 , 14 ), (14 , 15 ), (8 , 12 ),
249+ (8 , 13 )]
238250 NUM_EDGES = len (EDGES )
239251
240252 colors = [[255 , 0 , 0 ], [255 , 85 , 0 ], [255 , 170 , 0 ], [255 , 255 , 0 ], [170 , 255 , 0 ], [85 , 255 , 0 ], [0 , 255 , 0 ], \
241253 [0 , 255 , 85 ], [0 , 255 , 170 ], [0 , 255 , 255 ], [0 , 170 , 255 ], [0 , 85 , 255 ], [0 , 0 , 255 ], [85 , 0 , 255 ], \
242254 [170 , 0 , 255 ], [255 , 0 , 255 ], [255 , 0 , 170 ], [255 , 0 , 85 ]]
243255 cmap = matplotlib .cm .get_cmap ('hsv' )
244256 plt .figure ()
245- skeletons = np .array ([item ['keypoints' ] for item in results ]).reshape (- 1 ,
246- 17 , 3 )
257+
247258 img = np .array (image ).astype ('float32' )
248- canvas = img .copy ()
249259
250- for i in range (17 ):
251- rgba = np .array (cmap (1 - i / 17. - 1. / 34 ))
252- rgba [0 :3 ] *= 255
260+ color_set = results ['colors' ] if 'colors' in results else None
261+
262+ if 'bbox' in results and ids is None :
263+ bboxs = results ['bbox' ]
264+ for j , rect in enumerate (bboxs ):
265+ xmin , ymin , xmax , ymax = rect
266+ color = colors [0 ] if color_set is None else colors [color_set [j ] %
267+ len (colors )]
268+ cv2 .rectangle (img , (xmin , ymin ), (xmax , ymax ), color , 1 )
269+
270+ canvas = img .copy ()
271+ for i in range (kpt_nums ):
253272 for j in range (len (skeletons )):
254273 if skeletons [j ][i , 2 ] < visual_thread :
255274 continue
275+ if ids is None :
276+ color = colors [i ] if color_set is None else colors [color_set [j ]
277+ %
278+ len (colors )]
279+ else :
280+ color = get_color (ids [j ])
281+
256282 cv2 .circle (
257283 canvas ,
258284 tuple (skeletons [j ][i , 0 :2 ].astype ('int32' )),
259285 2 ,
260- colors [ i ] ,
286+ color ,
261287 thickness = - 1 )
262288
263289 to_plot = cv2 .addWeighted (img , 0.3 , canvas , 0.7 , 0 )
264290 fig = matplotlib .pyplot .gcf ()
265291
266292 stickwidth = 2
267293
268- skeletons = map_coco_to_personlab (skeletons )
269294 for i in range (NUM_EDGES ):
270295 for j in range (len (skeletons )):
271296 edge = EDGES [i ]
@@ -283,7 +308,13 @@ def draw_pose(image, results, visual_thread=0.6, save_name='pose.jpg'):
283308 polygon = cv2 .ellipse2Poly ((int (mY ), int (mX )),
284309 (int (length / 2 ), stickwidth ),
285310 int (angle ), 0 , 360 , 1 )
286- cv2 .fillConvexPoly (cur_canvas , polygon , colors [i ])
311+ if ids is None :
312+ color = colors [i ] if color_set is None else colors [color_set [j ]
313+ %
314+ len (colors )]
315+ else :
316+ color = get_color (ids [j ])
317+ cv2 .fillConvexPoly (cur_canvas , polygon , color )
287318 canvas = cv2 .addWeighted (canvas , 0.4 , cur_canvas , 0.6 , 0 )
288319 image = Image .fromarray (canvas .astype ('uint8' ))
289320 plt .close ()
0 commit comments