在发人体姿态估计的相关文章时,往往需要得到关节点和肢体的可视化结果,因此写了一个代码结构进行可视化。
import os
import numpy as np
import json
import cv2
import random
from tqdm import tqdm
dataset_dir = "D:/send_paper/COCO val2017/val2017/"
dataset_save = "D:/send_paper/COCO val2017/result/"
def show_skeleton(img,kpts,color=(255,128,128),thr=0.5):
kpts = np.array(kpts).reshape(-1,3)
skelenton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10],
[9, 11], [1, 2], [1, 3], [2, 4], [3, 5]]
points_num = [num for num in range(1,18)]
for sk in skelenton:
pos1 = (int(kpts[sk[0] - 1, 0]), int(kpts[sk[0] - 1, 1]))
pos2 = (int(kpts[sk[1] - 1, 0]), int(kpts[sk[1] - 1, 1]))
if pos1[0] > 0 and pos1[1] > 0 and pos2[0] > 0 and pos2[1] > 0 and kpts[sk[0] - 1, 2] > thr and kpts[
sk[1] - 1, 2] > thr:
cv2.line(img, pos1, pos2, color, 2, 8)
for points in points_num:
pos = (int(kpts[points-1,0]),int(kpts[points-1,1]))
if pos[0] > 0 and pos[1] > 0 and kpts[points-1,2] > thr:
cv2.circle(img, pos,4,(0,0,255),-1) #为肢体点画红色实心圆
return img
with open("D:/send_paper/keypoints_val2017_results_0.json","r") as load_f:
load_dict = json.load(load_f)
imgIds_old = 0
image = cv2.imread(os.path.join(dataset_dir, str(397133).zfill(12) + '.jpg'))
skeleton_color = [(154,194,182),(123,151,138),(0,208,244),(8,131,229),(18,87,220)] # 选择自己喜欢的颜色
for dict_num in tqdm(load_dict):
imgIds = dict_num["image_id"]
if imgIds!=imgIds_old:
cv2.imwrite(os.path.join(dataset_save,str(imgIds_old).zfill(12)+'.jpg'),image)
image_path = os.path.join(dataset_dir, str(imgIds).zfill(12) + '.jpg')
image = cv2.imread(image_path)
color = random.choice(skeleton_color)
show_skeleton(image, dict_num["keypoints"],color=color)
imgIds_old = imgIds
import os
import numpy as np
import json
import cv2
import random
from tqdm import tqdm
dataset_dir = "D:/send_paper/MPII/mpii_valid/mpii_valid/"
dataset_save = "D:/send_paper/MPII/result/"
def show_skeleton(img,kpts,color=(255,128,128)):
skelenton = [[10, 11], [11, 12], [12, 8], [8, 13], [13, 14], [14, 15], [8, 9], [7, 8], [2, 6],
[3, 6], [1, 2], [1, 0], [3, 4], [4, 5],[6,7]]
points_num = [num for num in range(1,16)]
for sk in skelenton:
pos1 = (int(kpts[sk[0]][0]), int(kpts[sk[0]][1]))
pos2 = (int(kpts[sk[1]][0]), int(kpts[sk[1]][1]))
if pos1[0] > 0 and pos1[1] > 0 and pos2[0] > 0 and pos2[1] > 0:
cv2.line(img, pos1, pos2, color, 2, 8)
for points in points_num:
pos = (int(kpts[points-1][0]),int(kpts[points-1][1]))
if pos[0] > 0 and pos[1] > 0 :
cv2.circle(img, pos,4,(0,0,255),-1) #为肢体点画红色实心圆
return img
with open("D:/send_paper/MPII/valid.json","r") as load_f:
load_dict = json.load(load_f)
imgIds_old = "0.jpg"
image = cv2.imread(os.path.join(dataset_dir, '005808361.jpg'))
skeleton_color = [(154,194,182),(123,151,138),(0,208,244),(8,131,229),(18,87,220)] # 选择自己喜欢的颜色
for dict_num in tqdm(load_dict):
imgIds = dict_num["image"]
if imgIds!=imgIds_old:
cv2.imwrite(os.path.join(dataset_save,imgIds_old),image)
image_path = os.path.join(dataset_dir, imgIds)
image = cv2.imread(image_path)
color = random.choice(skeleton_color)
show_skeleton(image, dict_num["joints"],color=color)
imgIds_old = imgIds
之前两个代码傻傻的没用groupby,真是太菜了
在这里插入代码片import os
import numpy as np
import json
import cv2
import random
from tqdm import tqdm
from itertools import groupby
dataset_dir = "D:/send_paper/crowdpose_r/images/images/"
dataset_save = "D:/send_paper/crowdpose_r/result"
def show_skeleton(img,kpts,color=(255,128,128),thr=0.5):
kpts = np.array(kpts).reshape(-1,3)
skelenton = [[0, 2], [1, 3], [2, 4], [3, 5], [6, 8], [8, 10], [7, 9], [9, 11], [12, 13], [0, 13], [1, 13],
[6,13],[7, 13]]
points_num = [num for num in range(14)]
for sk in skelenton:
pos1 = (int(kpts[sk[0], 0]), int(kpts[sk[0], 1]))
pos2 = (int(kpts[sk[1], 0]), int(kpts[sk[1] , 1]))
if pos1[0] > 0 and pos1[1] > 0 and pos2[0] > 0 and pos2[1] > 0 and kpts[sk[0], 2] > thr and kpts[
sk[1], 2] > thr:
cv2.line(img, pos1, pos2, color, 2, 8)
for points in points_num:
pos = (int(kpts[points,0]),int(kpts[points,1]))
if pos[0] > 0 and pos[1] > 0 and kpts[points,2] > thr:
cv2.circle(img, pos,4,(0,0,255),-1) #为肢体点画红色实心圆
return img
with open("D:/send_paper/crowdpose_r/crowdpose_r.json","r") as load_f:
load_dict = json.load(load_f)
ret = list()
ret_num = list()
for group_num,group in groupby(load_dict,lambda x: x["image_id"]):
ret.append(list(group))
ret_num.append(group_num)
for item in ret:
if len(item)>5:
print(item[0]["image_id"])
print(item)
ret_result = zip(ret_num,ret)
joints=list()
skeleton_color = [(154,194,182),(123,151,138),(0,208,244),(8,131,229),(18,87,220)] # 选择自己喜欢的颜色
for num,person_mss in tqdm(ret_result):
if num == 101107:
image = cv2.imread(os.path.join(dataset_dir,str(num)+'.jpg'))
for person_num in range(len(person_mss)):
person_1 = np.array(person_mss[person_num]['keypoints']).reshape(-1, 3)
color=random.choice(skeleton_color)
show_skeleton(image,person_1,color=color)
cv2.imwrite(os.path.join(dataset_save,str(num)+'.jpg'),image)```
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200626094905178.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0RDR0o2NjY=,size_16,color_FFFFFF,t_70#pic_center)