引言

在做实例分割或语义分割的时候,我们通常要用labelme进行标注,labelme标注的json文件与coco数据集已经标注好的json文件的格式和内容有差异。如果要用coco数据集的信息,就要对json文件进行修改和转换。本博客提供两种格式的具体内容及含义以及两种格式相互转换的代码,并对两种格式的json标注信息进行可视化。

1.coco格式的json标注信息详解及可视化

从coco官网下载coco的数据集里面,关于实例的标注信息在“annotations_trainval2017.zip”压缩文件里面的“instances_train2017.json”和“instances_val2017.json”里面,分别是训练集和验证集的标注信息。

下载地址:

  1. 训练集图片:http://images.cocodataset.org/zips/train2017.zip
  2. 验证集图片:http://images.cocodataset.org/zips/val2017.zip
  3. 测试集图片:http://images.cocodataset.org/zips/test2017.zip
  4. 训练集、验证集标注信息:http://images.cocodataset.org/annotations/annotations_trainval2017.zip
  5. http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip
  6. http://images.cocodataset.org/annotations/panoptic_annotations_trainval2017.zip

由于“instances_train2017.json”里面把所有训练集图片的标注信息整合到一个文件了,文件非常大,不太好查看内部具体内容。我从这个文件中提取出一张图片的信息保存成一个新的json文件。

json文件内主要字段:

代码如下:

  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. """
  4. Created on Thu Aug 15 13:58:40 2019
  5.  
  6. @author: Taoting
  7. """
  8.  
  9. '''
  10. 从coco的标注文件里提取一张图片对应的json信息,并保存成新的json文件(以instance为例,其他的类似)。
  11. '''
  12. import matplotlib.pyplot as plt
  13. import os, sys, zipfile
  14. import urllib.request
  15. import shutil
  16. import numpy as np
  17. import skimage.io as io
  18. import pylab
  19. import json
  20. from pycocotools.coco import COCO
  21.  
  22. pylab.rcParams['figure.figsize'] = (8.0, 10.0)
  23.  
  24. json_file='../../../coco dataset/annotations_trainval2017/instances_val2017.json' # # Object Instance 类型的标注
  25. # json_file='./annotations/person_keypoints_val2017.json' # Object Keypoint 类型的标注格式
  26. # json_file='./annotations/captions_val2017.json' # Image Caption的标注格式
  27.  
  28. data=json.load(open(json_file,'r'))
  29.  
  30. data_2={}
  31. data_2['info']=data['info']
  32. data_2['licenses']=data['licenses']
  33. data_2['images']=[data['images'][0]] # 只提取第一张图片
  34. data_2['categories']=data['categories']
  35. annotation=[]
  36.  
  37. # 通过imgID 找到其所有instance
  38. imgID=data_2['images'][0]['id']
  39. #print(imgID)#397133
  40. # initialize COCO api for instance annotations
  41. coco=COCO(json_file)
  42. img = coco.loadImgs([imgID])
  43. #print(img)#[{'license': 4, 'file_name': '000000397133.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000397133.jpg',
  44. # 'height': 427, 'width': 640, 'date_captured': '2013-11-14 17:02:52', 'flickr_url': 'http://farm7.staticflickr.com/6116/6255196340_da26cf2c9e_z.jpg', 'id': 397133}]
  45. #print(img['file_name'])
  46. # load and display image
  47. I = io.imread('../../../coco dataset/val2017/%s' % img[0]['file_name'])
  48. # use url to load image
  49. #I = io.imread(img['coco_url'])
  50. #plt.axis('off')
  51. #plt.imshow(I)
  52. #plt.show()
  53.  
  54. for ann in data['annotations']:
  55. if ann['image_id']==imgID:
  56. annotation.append(ann)
  57.  
  58. data_2['annotations']=annotation
  59.  
  60. # 保存到新的json
  61. json.dump(data_2,open('./{}.json'.format(str(img[0]['file_name']).split('.')[0]),'w'),indent=4)

从coco标注json中提取单张图片的标注信息

得到一张图片的标注信息如下,包含5大部分的字段信息。

"info"的value是一个dict,存储数据集的一些基本信息,我们不需要关注;

"licenses"的value是一个list,存储license信息,我们不需要关注;

"categories"的value是一个list,存储数据集的类别信息,包括类别的超类、类别id、类别名称;

“images”的value是一个list,存储这张图片的基本信息,包括图片名、长、宽、id等重要信息;

"annotations"的value是一个list,存储这张图片的标注信息,非常重要,list中的每一个元素是一个dict,也即一个标注对象(instance)的信息。包括的字段有"segmentation":标注点的坐标,从第一个的x,y坐标一直到最后一个点的x,y坐标;"area"是标注的闭合多边形的面积; "iscrowd"表示对象之间是否有重叠;"image_id"是图片的id;“bbox”是instance的边界框的左上角的x,y,边界框的宽和高;"category_id"是这个instance对应的类别id;"id"表示此instance标注信息在所有instance标注信息中的id。

  1. {
  2. "info": {
  3. "description": "COCO 2017 Dataset",
  4. "url": "http://cocodataset.org",
  5. "version": "1.0",
  6. "year": 2017,
  7. "contributor": "COCO Consortium",
  8. "date_created": "2017/09/01"
  9. },
  10. "licenses": [
  11. {
  12. "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
  13. "id": 1,
  14. "name": "Attribution-NonCommercial-ShareAlike License"
  15. },
  16. {
  17. "url": "http://creativecommons.org/licenses/by-nc/2.0/",
  18. "id": 2,
  19. "name": "Attribution-NonCommercial License"
  20. },
  21. ...(太长,省略)
  22. ],
    "categories": [
            {
                "supercategory": "person",
                "id": 1,
                "name": "person"
            },
            ...(太长,省略)
    ],
  23. "images": [
  24. {
  25. "license": 2,
  26. "file_name": "000000000049.jpg",
  27. "coco_url": "http://images.cocodataset.org/train2017/000000000049.jpg",
  28. "height": 500,
  29. "width": 381,
  30. "date_captured": "2013-11-14 20:00:23",
  31. "flickr_url": "http://farm4.staticflickr.com/3250/2883102207_bcba5527a7_z.jpg",
  32. "id": 49
  33. }
  34. ],
  35. "annotations": [
  36. {
  37. "segmentation": [
  38. [
  39. 181.59,
  40. 363.43,
  41. ...(太长,省略)
  42. ]
  43. ],
  44. "area": 8451.22405,
  45. "iscrowd": 0,
  46. "image_id": 49,
  47. "bbox": [
  48. 162.57,
  49. 226.56,
  50. 130.41,
  51. 184.43
  52. ],
  53. "category_id": 19,
  54. "id": 56407
  55. },
  56. ...(太长,省略)
  57. ]
  58. }

我们对这个新coco格式的json文件进行可视化:

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Aug 15 14:48:12 2019
  4.  
  5. @author: Taoting
  6. """
  7. from __future__ import print_function
  8. import matplotlib.pyplot as plt
  9. # ~ from pycocotools.coco import COCO
  10. from coco import COCO
  11. import os, sys, zipfile
  12. import urllib.request
  13. import shutil
  14. import numpy as np
  15. import skimage.io as io
  16. import pylab
  17. pylab.rcParams['figure.figsize'] = (8.0, 10.0)
  18.  
  19. annFile='./modified_satisfied_json_train2017/000000000149.json'#json文件路径
  20. coco=COCO(annFile)
  21.  
  22. cats = coco.loadCats(coco.getCatIds())
  23. nms=[cat['name'] for cat in cats]
  24.  
  25. nms = set([cat['supercategory'] for cat in cats])
  26.  
  27. imgIds = coco.getImgIds()
  28. img = coco.loadImgs(imgIds[0])[0]
  29. dataType = './satisfied_images_train2017'
  30. I = io.imread('%s/%s'%(dataType,img['file_name']))
  31.  
  32. plt.axis('off')
  33. plt.imshow(I)
  34. plt.show()
  35.  
  36. # 加载和可视化instance标注信息
  37. catIds=[]
  38. for ann in coco.dataset['annotations']:
  39. if ann['image_id']==imgIds[0]:
  40. catIds.append(ann['category_id'])
  41.  
  42. plt.imshow(I); plt.axis('off')
  43. annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
  44. anns = coco.loadAnns(annIds)
  45. coco.showAnns(anns)
  46. plt.show()

coco格式的json文件可视化instance的mask

可视化结果如下:

2.labelme格式的json标注信息详解及可视化

labelme标注工具标注的json格式与coco的格式有差异:

重点关注的是:

"shapes":存储标注instance的闭合多边形的信息,重点关注:label:类别名称;points:闭合多边形的每个点的x,y坐标;

"line_color":闭合多边形的边界线颜色;

"fill_color":闭合多边形的填充颜色;

"imagePath":图片名称;

"imageData":图片路径(加密后);

"imageHeight":图片高;

"imageWidth":图片宽;

利用labelme提供的接口将标注好的json进行可视化代码:

  1. # -*- coding:utf-8 -*-
  2. """
  3. @author: Taoting
  4. 将用labeime标注格式的json进行可视化
  5. """
  6. import json
  7. import matplotlib.pyplot as plt
  8. import skimage.io as io
  9. from labelme import utils
  10.  
  11. def main():
  12. json_path = './PATH/TO/JSON'
  13. data = json.load(open(json_path))
  14. img = io.imread('%s/%s'%('./PATH/TO/IMAGE',data['imagePath']))
  15. lab, lab_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])
  16. captions = ['%d: %s' % (l, name) for l, name in enumerate(lab_names)]
  17. lab_ok = utils.draw_label(lab, img, captions)
  18.  
  19. plt.subplot(121)
  20. plt.imshow(img)
  21. plt.subplot(122)
  22. plt.imshow(lab_ok)
  23. plt.show()
  24.  
  25. if __name__ == '__main__':
  26. main()

可视化结果:

可以看到右图中的mask的可视化效果

结合1和2中的两种格式的json,我们只需要针对格式的差异对json文件做修改,就能将格式进行互相转换。

3.coco格式的json转labelme格式的json

直接上代码:

  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: Taoting
  4. 将用coco格式的json转化成labeime标注格式的json
  5. """
  6.  
  7. import json
  8. import cv2
  9. import numpy as np
  10. import os
  11.  
  12. #用一个labelme格式的json作为参考,因为很多信息都是相同的,不需要修改。
  13. def reference_labelme_json():
  14. ref_json_path = 'reference_labelme.json'
  15. data=json.load(open(ref_json_path))
  16. return data
  17.  
  18. def labelme_shapes(data,data_ref):
  19. shapes = []
  20. label_num = {'person':0,'bicycle':0,'car':0,'motorcycle':0,'bus':0,'train':0,'truck':0}#根据你的数据来修改
  21. for ann in data['annotations']:
  22. shape = {}
  23. class_name = [i['name'] for i in data['categories'] if i['id'] == ann['category_id']]
  24. #label要对应每一类从_1开始编号
  25. label_num[class_name[0]] += 1
  26. shape['label'] = class_name[0] + '_' + str(label_num[class_name[0]])
  27. shape['line_color'] = data_ref['shapes'][0]['line_color']
  28. shape['fill_color'] = data_ref['shapes'][0]['fill_color']
  29.  
  30. shape['points'] = []
  31. # ~ print(ann['segmentation'])
  32. if not type(ann['segmentation']) == list:
  33. continue
  34. else:
  35. x = ann['segmentation'][0][::2]#奇数个是x的坐标
  36. y = ann['segmentation'][0][1::2]#偶数个是y的坐标
  37. for j in range(len(x)):
  38. shape['points'].append([x[j], y[j]])
  39.  
  40. shape['shape_type'] = data_ref['shapes'][0]['shape_type']
  41. shape['flags'] = data_ref['shapes'][0]['flags']
  42. shapes.append(shape)
  43. return shapes
  44.  
  45. def Coco2labelme(json_path,data_ref):
  46. with open(json_path,'r') as fp:
  47. data = json.load(fp) # 加载json文件
  48. data_labelme={}
  49. data_labelme['version'] = data_ref['version']
  50. data_labelme['flags'] = data_ref['flags']
  51.  
  52. data_labelme['shapes'] = labelme_shapes(data,data_ref)
  53.  
  54. data_labelme['lineColor'] = data_ref['lineColor']
  55. data_labelme['fillColor'] = data_ref['fillColor']
  56. data_labelme['imagePath'] = data['images'][0]['file_name']
  57.  
  58. data_labelme['imageData'] = None
  59. # ~ data_labelme['imageData'] = data_ref['imageData']
  60.  
  61. data_labelme['imageHeight'] = data['images'][0]['height']
  62. data_labelme['imageWidth'] = data['images'][0]['width']
  63.  
  64. return data_labelme
  65.  
  66. if __name__ == '__main__':
  67. root_dir = './ROOT DIR'
  68. json_list = os.listdir(root_dir)
  69. #参考的json
  70. data_ref = reference_labelme_json()
  71.  
  72. for json_path in json_list:
  73. if json_path.split('.')[-1] == 'json':
  74. print('当前文件: ', json_path)
  75. data_labelme= Coco2labelme(os.path.join(root_dir,json_path), data_ref)
  76. file_name = data_labelme['imagePath']
  77. # 保存json文件
  78. json.dump(data_labelme,open('./PATH/%s.json' % file_name.split('.')[0],'w'),indent=4)

用2中的可视化代码检验是否正确转换。

4.labelme格式的json转coco格式的json

直接上代码:

  1. # -*- coding: utf-8 -*-
  2. """Created on Thu Aug 15 15:05:56 2019
  3. @author: Taoting
  4. 将用labeime标注的json转化成coco格式的json
  5. """
  6.  
  7. import json
  8. import cv2
  9. import numpy as np
  10. import os
  11.  
  12. #用闭包实现计数器
  13. def counter():
  14. cnt = 1000000
  15. def increce():
  16. nonlocal cnt
  17. x = cnt
  18. cnt += 1
  19. return x
  20. return increce
  21.  
  22. def p_images(data,data_coco):
  23. images=[]
  24. image={}
  25. file_name=data['imagePath'].split('\\')[-1]
  26. image['file_name']=file_name
  27. image['id']=int(file_name.split('.')[0])
  28. image['height']=data['imageHeight']
  29. image['width']=data['imageWidth']
  30. img=None
  31. images.append(image)
  32. data_coco['images']=images
  33. return file_name
  34.  
  35. #用一个coco格式的json做参考
  36. def modify_categories():
  37. ref_json_path = 'reference.json'
  38. data=json.load(open(ref_json_path))
  39. modified_categories = []
  40. catNms=['person','bicycle','car','motorcycle','truck','bus']#根据你的数据修改
  41. for i,cat in enumerate(data['categories']):
  42. if cat['name'] in catNms:
  43. modified_categories.append(cat)
  44. else:
  45. pass
  46. return modified_categories,data['info'],data['licenses']
  47.  
  48. def p_annotation(data,data_coco,cnt):
  49. # annotations
  50. annotations=[]
  51.  
  52. for i in range(len(data['shapes'])):
  53. annotation={}
  54. annotation['segmentation']=[list(np.asarray(data['shapes'][i]['points']).flatten())] # data['shapes'][0]['points']
  55. annotation['iscrowd']=0
  56. annotation['image_id']=data_coco['images'][0]['id']
  57. #找出标注点中的外接矩形的四个点
  58. x = annotation['segmentation'][0][::2]#奇数个是x的坐标
  59. y = annotation['segmentation'][0][1::2]#偶数个是y的坐标
  60. print(x,y)
  61. x_left = min(x)-1#往外扩展1个像素,也可以不扩展
  62. y_left = min(y)-1
  63. w = max(x) - min(x)+1
  64. h = max(y) - min(y)+1
  65. annotation['bbox']=[x_left,y_left,w,h] # [左上角x,y以及宽和高]
  66. cat_list_dict = [cat for cat in data_coco['categories'] if cat['name'] == data['shapes'][i]['label'].split('_')[1]]#注意这里是跟标注时填类别的方式有关
  67. annotation['category_id']=cat_list_dict[0]['id']
  68. annotation['id'] = cnt() # 第一个对象 这个ID也不能重复,如果下一张图,id不能取1,需从1 开始往下取
  69. #print('cnt', annotation['id'])
  70. #print('annotation',annotation)
  71. annotations.append(annotation)
  72. #print('annotations',annotations)
  73. data_coco['annotations']=annotations
  74. #print(data_coco['annotations'])
  75. #return data_coco
  76.  
  77. def Labelme2coco(json_path,cnt):
  78. with open(json_path,'r') as fp:
  79. data = json.load(fp) # 加载json文件
  80. data_coco={}
  81. # images
  82. file_name = p_images(data,data_coco)
  83. # categories
  84. modified_categories, info, p_license = modify_categories()
  85. data_coco['categories'] = modified_categories
  86. #print(data_coco['categories'])
  87. # info
  88. data_coco['info'] = info
  89. # license
  90. data_coco['license'] = p_license
  91. # annotations
  92. p_annotation(data,data_coco,cnt)
  93. #print(data_coco['annotations'])
  94. return data_coco,file_name
  95.  
  96. if __name__ == '__main__':
  97. root_dir = './ROOT DIR'
  98. json_list = os.listdir(root_dir)
  99. cnt = counter()
  100. for json_path in json_list:
  101. if json_path.split('.')[-1] == 'json':
  102. data_coco,file_name = Labelme2coco(os.path.join(root_dir,json_path),cnt)
  103. # 保存json文件
  104. json.dump(data_coco,open('./PATH/%s.json' % file_name.split('.')[0],'w'),indent=4)

用1中的可视化代码检验是否正确转换。

coco标注信息与labelme标注信息的详解、相互转换及可视化的更多相关文章

  1. 【Linux 运维】查看网络连接状态信息之netstat和ss命令详解

    一.netstat 常用命令详解 通过man netstat可以查看netstat的帮助信息: netstat 命令:用于显示各种网络相关信息,如网络连接,路由表,接口状态,无效连接,组播成员 等等. ...

  2. 一键获取linux内存、cpu、磁盘IO等信息脚本编写,及其原理详解

    更多linux知识,请关注公众号:一口Linux 一.脚本 今天主要分享一个shell脚本,用来获取linux系统CPU.内存.磁盘IO等信息. #!/bin/bash # 获取要监控的本地服务器IP ...

  3. 百度地图Api详解之地图标注

    标注概述 标注(Marker)是用来表示一个点位置的可见元素,每个标注自身都包含地理信息.比如你在西单商场位置添加了一个标注,不论地图移动.缩放,标注都会跟随一起移动,保证其始终指向正确的地理位置. ...

  4. HTTP协议的头信息详解

    转载地址:http://blog.csdn.net/guoguo1980/article/details/2649658 HTTP(HyperTextTransferProtocol)是超文本传输协议 ...

  5. 【图文详解】scrapy爬虫与动态页面——爬取拉勾网职位信息(2)

    上次挖了一个坑,今天终于填上了,还记得之前我们做的拉勾爬虫吗?那时我们实现了一页的爬取,今天让我们再接再厉,实现多页爬取,顺便实现职位和公司的关键词搜索功能. 之前的内容就不再介绍了,不熟悉的请一定要 ...

  6. linux dmesg命令参数及用法详解(linux显示开机信息命令)

    linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...

  7. yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并原数组信息---案例

    yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并元数组信息---案例 public function actionRandomLists(){ //查询到了所 ...

  8. 网络编辑基础:对HTTP协议的头信息详解

    HTTP(HyperTextTransferProtocol) 是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP 协议的详细内容请参 考RFC2616.HTTP协议采用了请求/响应模型 ...

  9. 百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度)

    百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度) <%@ Page Language="C#" AutoEventWireup="true&qu ...

随机推荐

  1. nl2br()处理字符串中的换行符

    nl2br() 函数 在字符串中包含换行符时,需要对其进行转换,php 中有str_replace()函数,可以直接对字符串进行替换处理.但php中还有nl2br()函数可以直接处理. 1.在字符串中 ...

  2. DedeCms常用内容调用标签实例大全

    一.调用顶级栏目标签 <a href="{dede:global.cfg_cmsurl/}/" class="ahov">首页</a> ...

  3. PHP--仿微信, 通过登陆者用户名显示好友列表,显示头像和昵称

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Redis项目实战---应用及理论(上)---redis基础知识介绍

    redis(Remote Dictionary Server)   一.原理及特性层面:     1.优势:        1)数据加载在内存中,执行速度快, 数据结构类似于HashMap,HashM ...

  5. Set接口的使用

    Set集合里多个对象之间没有明显的顺序.具体详细方法请参考API文档(可见身边随时带上API文档有多重要),基本与Collection方法相同.只是行为不同(Set不允许包含重复元素). Set集合不 ...

  6. poj 2503 Babelfish(字典树或map或哈希或排序二分)

    输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...

  7. Linux 常用命令及使用方法

    1.  type   :查询命令 是否属于shell解释器 2.  help  : 帮助命令3.  man : 为所有用户提供在线帮助4.  ls  : 列表显示目录内的文件及目录 -l    以长格 ...

  8. drf之序列化

    在django视图中使用serializer 只是使用serializer类编写API视图,没有用到REST框架 app01下的models.py from django.db import mode ...

  9. DataOps系列丨数据的“资产负债表”与“现状”

    作者:DataPipeline CEO 陈诚 <跨越鸿沟>的作者Geoffrey Moore曾说“没有数据,运营企业就像一个又聋又瞎的人在高速上开车一样”.数据的价值从未像现在这样被企业重 ...

  10. 浅谈Ceph纠删码

    目  录第1章 引言 1.1 文档说明 1.2 参考文档 第2章 纠删码概念和原理 2.1 概念 2.2 原理 第3章 CEPH纠删码介绍 3.1 CEPH纠删码用途 3.2 CEPH纠删码库 3.3 ...