我是靠谱客的博主 可靠大象,最近开发中收集的这篇文章主要介绍可视化 ms_coco 人体关键点数据集(python),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

数据集下载地址:https://cocodataset.org/#download

在这里插入图片描述

文件路径信息

annotations文件保存person_keypoints_val2014.json(77M左右)
mscoco_imgs文件保存下载的图片和添加mask的图片

在这里插入图片描述
在这里插入图片描述

完整代码

import json
import numpy as np
import cv2
import urllib

json_dir="E:/DL/GCN/GCN_study/data/annotations/person_keypoints_val2014.json"
img_dir='E:/DL/GCN/GCN_study/data/mscoco_imgs/'

with open(json_dir,'r') as js:
    print(type(js))   
    jt=json.load(js)
    print("图像数量:n",len(jt["images"]))
    print("标签数量:n",len(jt["annotations"]))
    
    for key in jt:
        print(key)
        
    print(len(jt),jt["info"],'nnimages:',
          jt["images"][0],'nnlicenses:',
          jt["licenses"][0],'nnannotations:',
          jt["annotations"][0],'nncategories:',
          jt["categories"])
    
    m=0#2000图片的序号0~40504
    seg=jt["annotations"][m]['segmentation'][0]
    print("nseg:",len(seg),seg)
    
    anno_id=jt["annotations"][m]['image_id']
    kep=jt["annotations"][m]['keypoints']#关键点
    print("nkep:",len(kep),kep)
    
    kpt=jt["categories"][0]['keypoints']#关键点含义
    kpl=jt["categories"][0]['skeleton']#关键点连线
    print("nskeleton:",kpl)#节点的连接关系
    
    #由anno为主,找对应的image
    for i in range(len(jt["images"])):        
        if jt["images"][i]['id'] == anno_id:
          #根据网址下载图片
           urlStr =jt["images"][i]['coco_url']
           #下载图片
           print('n标签对应的图片地址:',urlStr)
           urllib.request.urlretrieve(urlStr , filename=img_dir+str(anno_id)+'.png')
    
    img=cv2.imread(img_dir+str(anno_id)+'.png')
    
   
    #画分割点
    print("nsegmentation信息:")
    segs=np.zeros((int(len(seg)/2),2),np.int)
    h, w, ch=img.shape
    src = np.zeros([h, w, ch],dtype=np.uint8)
    h=-1
    for j in range(0,len(seg),2):
        h=h+1
        print(seg[j:j+2])
        x1,y1=seg[j:j+2]
        segs[h,:2]=x1,y1
        cv2.circle(img,(int(x1),int(y1)),2,(255, 0, 0),-1)#画mask,shift圆心坐标点和半径值的小数点位数
    
    #画关键点    
    k=-1
    dots=[]
    print("nkeypoints信息:")
    for i in range(0,50,3):
        k=k+1
        print(kep[i:i+3],kpt[k])
        x,y,v=kep[i:i+3]
        dots.append([x,y])
        cv2.circle(img,(x,y),4,(255, 255, 0),-1)#画圆点
            
    print("ndots:",dots)
    #关键点连线
    print("n节点的连接关系:")
    for n in range(len(kpl)):
        [a,b]=kpl[n]
        print('n',dots[a-1],dots[b-1])
        if dots[a-1]!=[0,0] and dots[b-1]!=[0,0]:
            cv2.line(img,(dots[a-1][0],dots[a-1][1]),(dots[b-1][0],dots[b-1][1]), (0, 255, 0), 2)
    
    cv2.imwrite(img_dir+str(anno_id)+'-dot.png',img)#保存标点图
    #print(segs,segs.shape)
    cv2.polylines(img, [segs], isClosed=True, thickness=3, color=(144, 238, 144))
    mask=cv2.fillPoly(src,[segs],color=(0, 165, 255))
    mask_img=cv2.addWeighted(img,1,mask, 0.3,0)    
    cv2.imshow("img1",img)
    cv2.imshow("img2",mask_img)
    cv2.imwrite(img_dir+str(anno_id)+'-mask.png',mask_img)#保存mask图
    
    cv2.waitKey(5000)
    cv2.destroyAllWindows()    
        

代码编译结果

In [47]: runfile('E:/DL/GCN/GCN_study/keypoint_json.py', wdir='E:/DL/GCN/GCN_study')
<class '_io.TextIOWrapper'>
图像数量:
 40504
标签数量:
 88153
info
images
licenses
annotations
categories
5 {'description': 'COCO 2014 Dataset', 'url': 'http://cocodataset.org', 'version': '1.0', 'year': 2014, 'contributor': 'COCO Consortium', 'date_created': '2017/09/01'} 

images: {'license': 3, 'file_name': 'COCO_val2014_000000391895.jpg', 'coco_url': 'http://images.cocodataset.org/val2014/COCO_val2014_000000391895.jpg', 'height': 360, 'width': 640, 'date_captured': '2013-11-14 11:18:45', 'flickr_url': 'http://farm9.staticflickr.com/8186/8119368305_4e622c8349_z.jpg', 'id': 391895} 

licenses: {'url': 'http://creativecommons.org/licenses/by-nc-sa/2.0/', 'id': 1, 'name': 'Attribution-NonCommercial-ShareAlike License'} 

annotations: {'segmentation': [[267.03, 243.78, 314.59, 154.05, 357.84, 136.76, 374.05, 104.32, 410.81, 110.81, 429.19, 131.35, 420.54, 165.95, 451.89, 209.19, 464.86, 240.54, 480, 253.51, 484.32, 263.24, 496.22, 271.89, 484.32, 278.38, 438.92, 257.84, 401.08, 216.76, 370.81, 247.03, 414.05, 277.3, 433.51, 304.32, 443.24, 323.78, 400, 362.7, 376.22, 375.68, 400, 418.92, 394.59, 424.32, 337.3, 382.16, 337.3, 371.35, 388.11, 327.03, 341.62, 301.08, 311.35, 276.22, 304.86, 263.24, 294.05, 249.19]], 'num_keypoints': 8, 'area': 28292.08625, 'iscrowd': 0, 'keypoints': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 325, 160, 2, 398, 177, 2, 0, 0, 0, 437, 238, 2, 0, 0, 0, 477, 270, 2, 287, 255, 1, 339, 267, 2, 0, 0, 0, 423, 314, 2, 0, 0, 0, 355, 367, 2], 'image_id': 537548, 'bbox': [267.03, 104.32, 229.19, 320], 'category_id': 1, 'id': 183020} 

categories: [{'supercategory': 'person', 'name': 'person', 'skeleton': [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]], 'keypoints': ['nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle'], 'id': 1}]

seg: 60 [267.03, 243.78, 314.59, 154.05, 357.84, 136.76, 374.05, 104.32, 410.81, 110.81, 429.19, 131.35, 420.54, 165.95, 451.89, 209.19, 464.86, 240.54, 480, 253.51, 484.32, 263.24, 496.22, 271.89, 484.32, 278.38, 438.92, 257.84, 401.08, 216.76, 370.81, 247.03, 414.05, 277.3, 433.51, 304.32, 443.24, 323.78, 400, 362.7, 376.22, 375.68, 400, 418.92, 394.59, 424.32, 337.3, 382.16, 337.3, 371.35, 388.11, 327.03, 341.62, 301.08, 311.35, 276.22, 304.86, 263.24, 294.05, 249.19]

kep: 51 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 325, 160, 2, 398, 177, 2, 0, 0, 0, 437, 238, 2, 0, 0, 0, 477, 270, 2, 287, 255, 1, 339, 267, 2, 0, 0, 0, 423, 314, 2, 0, 0, 0, 355, 367, 2]

skeleton: [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]]

标签对应的图片地址: http://images.cocodataset.org/val2014/COCO_val2014_000000537548.jpg

segmentation信息:
[267.03, 243.78]
[314.59, 154.05]
[357.84, 136.76]
[374.05, 104.32]
[410.81, 110.81]
[429.19, 131.35]
[420.54, 165.95]
[451.89, 209.19]
[464.86, 240.54]
[480, 253.51]
[484.32, 263.24]
[496.22, 271.89]
[484.32, 278.38]
[438.92, 257.84]
[401.08, 216.76]
[370.81, 247.03]
[414.05, 277.3]
[433.51, 304.32]
[443.24, 323.78]
[400, 362.7]
[376.22, 375.68]
[400, 418.92]
[394.59, 424.32]
[337.3, 382.16]
[337.3, 371.35]
[388.11, 327.03]
[341.62, 301.08]
[311.35, 276.22]
[304.86, 263.24]
[294.05, 249.19]

keypoints信息:
[0, 0, 0] nose
[0, 0, 0] left_eye
[0, 0, 0] right_eye
[0, 0, 0] left_ear
[0, 0, 0] right_ear
[325, 160, 2] left_shoulder
[398, 177, 2] right_shoulder
[0, 0, 0] left_elbow
[437, 238, 2] right_elbow
[0, 0, 0] left_wrist
[477, 270, 2] right_wrist
[287, 255, 1] left_hip
[339, 267, 2] right_hip
[0, 0, 0] left_knee
[423, 314, 2] right_knee
[0, 0, 0] left_ankle
[355, 367, 2] right_ankle

dots: [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [325, 160], [398, 177], [0, 0], [437, 238], [0, 0], [477, 270], [287, 255], [339, 267], [0, 0], [423, 314], [0, 0], [355, 367]]

节点的连接关系:

 [0, 0] [0, 0]

 [0, 0] [287, 255]

 [355, 367] [423, 314]

 [423, 314] [339, 267]

 [287, 255] [339, 267]

 [325, 160] [287, 255]

 [398, 177] [339, 267]

 [325, 160] [398, 177]

 [325, 160] [0, 0]

 [398, 177] [437, 238]

 [0, 0] [0, 0]

 [437, 238] [477, 270]

 [0, 0] [0, 0]

 [0, 0] [0, 0]

 [0, 0] [0, 0]

 [0, 0] [0, 0]

 [0, 0] [0, 0]

 [0, 0] [325, 160]

 [0, 0] [398, 177]

可视化结果(保存在mscoco_imgs文件下)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

最后

以上就是可靠大象为你收集整理的可视化 ms_coco 人体关键点数据集(python)的全部内容,希望文章能够帮你解决可视化 ms_coco 人体关键点数据集(python)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部