|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) Microsoft |
| 3 | +# Licensed under the MIT License. |
| 4 | +# Written by Ke Sun (sunk@mail.ustc.edu.cn) |
| 5 | +# Modified by Depu Meng (mdp@mail.ustc.edu.cn) |
| 6 | +# ------------------------------------------------------------------------------ |
| 7 | +import matplotlib as mpl |
| 8 | +mpl.use('Agg') |
| 9 | +import numpy as np |
| 10 | +from scipy.io import loadmat |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import cv2 |
| 13 | +import matplotlib.lines as mlines |
| 14 | +import matplotlib.patches as mpatches |
| 15 | +import math |
| 16 | +import os |
| 17 | +import argparse |
| 18 | + |
| 19 | + |
| 20 | +def parse_args(): |
| 21 | + parser = argparse.ArgumentParser(description='Visualize COCO predictions') |
| 22 | + # general |
| 23 | + parser.add_argument('--image-path', |
| 24 | + help='Path of COCO val images', |
| 25 | + type=str, |
| 26 | + default='data/mpii/images/' |
| 27 | + ) |
| 28 | + |
| 29 | + parser.add_argument('--save-path', |
| 30 | + help="Path to save the visualizations", |
| 31 | + type=str, |
| 32 | + default='visualize/mpii/') |
| 33 | + |
| 34 | + parser.add_argument('--prediction', |
| 35 | + help="Prediction file to visualize", |
| 36 | + type=str, |
| 37 | + required=True) |
| 38 | + |
| 39 | + parser.add_argument('--style', |
| 40 | + help="Style of the visualization: chunhua style or xiaochu style or openpose style", |
| 41 | + type=str, |
| 42 | + default='chunhua') |
| 43 | + |
| 44 | + args = parser.parse_args() |
| 45 | + |
| 46 | + return args |
| 47 | + |
| 48 | + |
| 49 | +""" |
| 50 | +# pose track |
| 51 | +link_pairs = [ |
| 52 | + [0, 1], [1, 2], [3, 4], [4, 5], [6, 7], [7, 8], [8, 9], [2, 3], |
| 53 | + [9, 10], [10, 11], [12, 13], [13, 14], [2, 8], [3, 9] |
| 54 | +] |
| 55 | +""" |
| 56 | + |
| 57 | +# joint[0]->joint[1](color: joint[2]) |
| 58 | +# MPII |
| 59 | + |
| 60 | +class ColorStyle: |
| 61 | + def __init__(self, color, link_pairs, point_color, ignore_id): |
| 62 | + self.color = color |
| 63 | + self.link_pairs = link_pairs |
| 64 | + self.point_color = point_color |
| 65 | + self.ignore_id = ignore_id |
| 66 | + for i in range(len(self.color)): |
| 67 | + self.link_pairs[i].append(tuple(np.array(self.color[i])/255.)) |
| 68 | + |
| 69 | + self.ring_color = [] |
| 70 | + for i in range(len(self.point_color)): |
| 71 | + self.ring_color.append(tuple(np.array(self.point_color[i])/255.)) |
| 72 | + |
| 73 | + |
| 74 | +# XiaoChu |
| 75 | +# (R,G,B) |
| 76 | +color1 = [(0,109,45),(49,163,84),(255,255,51),(228,26,28),(179,0,0), |
| 77 | + (255,255,51), (240,2,127), (0,0,255), (44,127,184), (255,255,51), |
| 78 | + (255,255,51), (254,153,41), (217,95,14)] |
| 79 | + |
| 80 | +link_pairs1 = [ |
| 81 | + [0, 1], [1, 2], [2, 12], [3, 4], [4, 5], [3, 13], [8, 9], |
| 82 | + [10, 11], [11,12], [12, 7], [7, 13], [13, 14], [14, 15], |
| 83 | +] |
| 84 | + |
| 85 | +point_color1 = [(0,109,45),(49,163,84),(255,255,51), |
| 86 | + (255,255,51),(228,26,28),(179,0,0), |
| 87 | + (255,255,51),(240,2,127), (240,2,127), |
| 88 | + (0,0,255), (44,127,184), (255,255,51), |
| 89 | + (255,255,51), (254,153,41), (217,95,14)] |
| 90 | + |
| 91 | +ignore_id1 = [6] |
| 92 | + |
| 93 | +xiaochu_style = ColorStyle(color1, link_pairs1, point_color1, ignore_id1) |
| 94 | + |
| 95 | +# Chunhua |
| 96 | +# (R,G,B) |
| 97 | +color2 = [(252,176,243),(252,176,243),(252,176,243), |
| 98 | + (0,176,240), (0,176,240), (0,176,240), |
| 99 | + (165, 104, 210), (255,0,0), |
| 100 | + (255,255,0), (255,255,0), (255,255,0), |
| 101 | + (169, 209, 142),(169, 209, 142),(169, 209, 142)] |
| 102 | + |
| 103 | +link_pairs2 = [ |
| 104 | + [0, 1], [1, 2], [2, 6], [6,3], [3, 4], [4, 5], [6, 7], [8, 9], |
| 105 | + [10, 11], [11,12], [12, 8], [8, 13], [13, 14], [14, 15], |
| 106 | +] |
| 107 | + |
| 108 | +point_color2 = [(252,176,243),(252,176,243),(252,176,243), |
| 109 | + (0,176,240),(0,176,240),(0,176,240), |
| 110 | + (165, 104, 210), (165, 104, 210), (255,0,0), (255,0,0), |
| 111 | + (255,255,0),(255,255,0), (255,255,0), |
| 112 | + (169, 209, 142), (169, 209, 142), (169, 209, 142)] |
| 113 | + |
| 114 | +ignore_id2 = [] |
| 115 | + |
| 116 | +chunhua_style = ColorStyle(color2, link_pairs2, point_color2, ignore_id2) |
| 117 | + |
| 118 | +# OpenPose |
| 119 | +# (R,G,B) |
| 120 | +color3 = [(121,67,226),(74,87,226),(47,118,177), |
| 121 | + (163,61,204), (216,53,204), (211,48,121), |
| 122 | + (63, 214, 217), (177,24,21), |
| 123 | + (43,192,128), (83,224,91), (111,210,58), |
| 124 | + (220, 132, 72),(194, 169, 37),(172, 214, 69)] |
| 125 | + |
| 126 | +link_pairs3 = [ |
| 127 | + [0, 1], [1, 2], [2, 6], [6,3], [3, 4], [4, 5], [6, 8], [8, 9], |
| 128 | + [10, 11], [11,12], [12, 8], [8, 13], [13, 14], [14, 15], |
| 129 | +] |
| 130 | + |
| 131 | +point_color3 = [(121,67,226),(74,87,226),(74,87,226), |
| 132 | + (163,61,204),(216,53,204),(211,48,121), |
| 133 | + (63, 214, 217),(63, 214, 217), (177,24,21), |
| 134 | + (43,192,128), (83,224,91), (111,210,58), |
| 135 | + (220, 132, 72), (194, 169, 37), (172, 214, 69)] |
| 136 | + |
| 137 | +ignore_id3 = [7] |
| 138 | + |
| 139 | +openpose_style = ColorStyle(color3, link_pairs3, point_color3, ignore_id3) |
| 140 | + |
| 141 | + |
| 142 | +""" |
| 143 | +def map_joint_array(joints, ignore_id): |
| 144 | + new_joints = np.zeros((16,3)) |
| 145 | + for i in range(joints.shape[1]): |
| 146 | + new_joints[i,2] = int(joints[0,i][2][0][0]) |
| 147 | + if new_joints[i,2] in ignore_id: |
| 148 | + continue |
| 149 | + else: |
| 150 | + new_joints[i,2] = int(joints[0,i][2][0][0]) |
| 151 | + new_joints[i,0] = int(joints[0,i][0][0,0]) |
| 152 | + new_joints[i,1] = int(joints[0,i][1][0,0]) |
| 153 | + return new_joints |
| 154 | +""" |
| 155 | + |
| 156 | +def map_joint_array(joints, ignore_id): |
| 157 | + new_joints = [] |
| 158 | + for i in range(joints.shape[1]): |
| 159 | + if int(joints[0,i][2][0][0]) in ignore_id: |
| 160 | + continue |
| 161 | + else: |
| 162 | + joint = [int(joints[0,i][j][0,0]) for j in range(3)] |
| 163 | + new_joints.append(joint) |
| 164 | + return np.array(new_joints) |
| 165 | + |
| 166 | + |
| 167 | +def map_joint_dict(joints): |
| 168 | + joints_dict = {} |
| 169 | + for i in range(joints.shape[1]): |
| 170 | + x = int(joints[0,i][0][0,0]) |
| 171 | + y = int(joints[0,i][1][0,0]) |
| 172 | + id = int(joints[0,i][2][0][0]) |
| 173 | + joints_dict[id] = (x, y) |
| 174 | + |
| 175 | + return joints_dict |
| 176 | + |
| 177 | +def plot_joints(image, joints): |
| 178 | + for id, pos in joints.items(): |
| 179 | + cv2.circle(image, pos, 3, (0,255,0), 2) |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == '__main__': |
| 184 | + args = parse_args() |
| 185 | + save_path = args.save_path |
| 186 | + if not os.path.exists(save_path): |
| 187 | + try: |
| 188 | + os.makedirs(save_path) |
| 189 | + except Exception: |
| 190 | + print('Fail to make {}'.format(save_path)) |
| 191 | + pred = loadmat(args.prediction)['pred'] |
| 192 | + |
| 193 | + # change color style here |
| 194 | + if args.style == 'chunhua': |
| 195 | + color_style = chunhua_style |
| 196 | + elif args.style == 'xiaochu': |
| 197 | + color_style = xiaochu_style |
| 198 | + elif args.style == 'openpose': |
| 199 | + color_style = openpose_style |
| 200 | + |
| 201 | + link_pairs = color_style.link_pairs |
| 202 | + ignore_id = color_style.ignore_id |
| 203 | + ring_color = color_style.ring_color |
| 204 | + |
| 205 | + for i in range(0, 1000): |
| 206 | + if len(pred[0,i][1]) < 1 or pred[0,i][1][0,0] is None or len(pred[0,i][1][0,0]) < 3 or len(pred[0,i][1][0,0][2][0]) < 1: |
| 207 | + continue |
| 208 | + img_name = pred[0,i][0][0,0][0][0][:-4] |
| 209 | + print('id: ', i) |
| 210 | + img_file = args.image_path + pred[0,i][0][0,0][0][0] |
| 211 | + scale = pred[0,i][1][0,0][0][0,0] |
| 212 | + center = (pred[0,i][1][0,0][1][0,0][0][0,0],pred[0,i][1][0,0][1][0,0][1][0,0]) |
| 213 | + joints = pred[0,i][1][0,0][2][0,0][0] |
| 214 | + joints_array = map_joint_array(joints, ignore_id) |
| 215 | + joints_dict = map_joint_dict(joints) |
| 216 | + |
| 217 | + data_numpy = cv2.imread(img_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) |
| 218 | + h = data_numpy.shape[0] |
| 219 | + w = data_numpy.shape[1] |
| 220 | + ref = np.min((h,w)) |
| 221 | + fig = plt.figure(figsize=(w/100, h/100), dpi=100) |
| 222 | + ax = plt.subplot(1,1,1) |
| 223 | + bk = plt.imshow(data_numpy[:,:,::-1]) |
| 224 | + bk.set_zorder(-1) |
| 225 | + |
| 226 | + # stick |
| 227 | + for link_pair in link_pairs: |
| 228 | + if link_pair[0] in joints_dict \ |
| 229 | + and link_pair[1] in joints_dict: |
| 230 | + line = mlines.Line2D( |
| 231 | + np.array([joints_dict[link_pair[0]][0],joints_dict[link_pair[1]][0]]), |
| 232 | + np.array([joints_dict[link_pair[0]][1],joints_dict[link_pair[1]][1]]), |
| 233 | + ls='-', lw=ref/90, alpha=1, color=link_pair[2],) |
| 234 | + line.set_zorder(0) |
| 235 | + ax.add_line(line) |
| 236 | + |
| 237 | + # black ring |
| 238 | + for j in range(joints_array.shape[0]): |
| 239 | + circle = mpatches.Circle(tuple(joints_array[j,:2]), radius=ref/90, |
| 240 | + ec='black', fc=ring_color[j], alpha=1, linewidth=ref/270) |
| 241 | + circle.set_zorder(1) |
| 242 | + ax.add_patch(circle) |
| 243 | + |
| 244 | + |
| 245 | + plt.gca().xaxis.set_major_locator(plt.NullLocator()) |
| 246 | + plt.gca().yaxis.set_major_locator(plt.NullLocator()) |
| 247 | + plt.axis('off') |
| 248 | + plt.subplots_adjust(top=1,bottom=0,left=0,right=1,hspace=0,wspace=0) |
| 249 | + plt.margins(0,0) |
| 250 | + plt.savefig(save_path + 'id_' +str(i)+ '.pdf', format='pdf', bbox_inckes='tight', dpi=100) |
| 251 | + plt.savefig(save_path + 'id_' +str(i)+ '.png', format='png', bbox_inckes='tight', dpi=100) |
| 252 | + plt.close() |
0 commit comments