Labelme标注的json数据转化为coco格式的数据

  • 1. 转化方法一
  • 2. 转化方法二

1. 转化方法一

import os
import json
import numpy as np
import glob
import shutil
import cv2
from sklearn.model_selection import train_test_splitnp.random.seed(41)classname_to_id = {"mouth": 0,  # 改成自己的类别"teeth": 1,"tongue": 2,"uvula": 3,"oropharynx": 4
}class Lableme2CoCo:def __init__(self):self.images = []self.annotations = []self.categories = []self.img_id = 0self.ann_id = 0def save_coco_json(self, instance, save_path):json.dump(instance, open(save_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=1)  # indent=2 更加美观显示# 由json文件构建COCOdef to_coco(self, json_path_list):self._init_categories()for json_path in json_path_list:obj = self.read_jsonfile(json_path)self.images.append(self._image(obj, json_path))shapes = obj['shapes']for shape in shapes:annotation = self._annotation(shape)self.annotations.append(annotation)self.ann_id += 1self.img_id += 1instance = {}instance['info'] = 'spytensor created'instance['license'] = ['license']instance['images'] = self.imagesinstance['annotations'] = self.annotationsinstance['categories'] = self.categoriesreturn instance# 构建类别def _init_categories(self):for k, v in classname_to_id.items():category = {}category['id'] = vcategory['name'] = kself.categories.append(category)# 构建COCO的image字段def _image(self, obj, path):image = {}from labelme import utilsimg_x = utils.img_b64_to_arr(obj['imageData'])h, w = img_x.shape[:-1]image['height'] = himage['width'] = wimage['id'] = self.img_idimage['file_name'] = os.path.basename(path).replace(".json", ".jpg")return image# 构建COCO的annotation字段def _annotation(self, shape):# print('shape', shape)label = shape['label']points = shape['points']annotation = {}annotation['id'] = self.ann_idannotation['image_id'] = self.img_idannotation['category_id'] = int(classname_to_id[label])annotation['segmentation'] = [np.asarray(points).flatten().tolist()]annotation['bbox'] = self._get_box(points)annotation['iscrowd'] = 0annotation['area'] = 1.0return annotation# 读取json文件,返回一个json对象def read_jsonfile(self, path):with open(path, "r", encoding='utf-8') as f:return json.load(f)# COCO的格式: [x1,y1,w,h] 对应COCO的bbox格式def _get_box(self, points):min_x = min_y = np.infmax_x = max_y = 0for x, y in points:min_x = min(min_x, x)min_y = min(min_y, y)max_x = max(max_x, x)max_y = max(max_y, y)return [min_x, min_y, max_x - min_x, max_y - min_y]# 训练过程中,如果遇到Index put requires the source and destination dtypes match, got Long for the destination and Int for the source
# 参考:https://github.com/open-mmlab/mmdetection/issues/6706
if __name__ == '__main__':labelme_path = "./data/mouth/train"saved_coco_path = "./data/"print('reading...')# 创建文件if not os.path.exists("%scoco/annotations/" % saved_coco_path):os.makedirs("%scoco/annotations/" % saved_coco_path)if not os.path.exists("%scoco/images/train2017/" % saved_coco_path):os.makedirs("%scoco/images/train2017" % saved_coco_path)if not os.path.exists("%scoco/images/val2017/" % saved_coco_path):os.makedirs("%scoco/images/val2017" % saved_coco_path)# 获取images目录下所有的joson文件列表print(labelme_path + "/*.json")json_list_path = glob.glob(labelme_path + "/*.json")print('json_list_path: ', len(json_list_path))# 数据划分,这里没有区分val2017和tran2017目录,所有图片都放在images目录下train_path, val_path = train_test_split(json_list_path, test_size=0.0001, train_size=0.9999)print("train_n:", len(train_path), 'val_n:', len(val_path))# 把训练集转化为COCO的json格式l2c_train = Lableme2CoCo()train_instance = l2c_train.to_coco(train_path)l2c_train.save_coco_json(train_instance, '%scoco/annotations/instances_train2017.json' % saved_coco_path)for file in train_path:# shutil.copy(file.replace("json", "jpg"), "%scoco/images/train2017/" % saved_coco_path)img_name = file.replace('json', 'jpg')temp_img = cv2.imread(img_name)try:cv2.imwrite("{}coco/images/train2017/{}".format(saved_coco_path, img_name.split('\\')[-1].replace('png', 'jpg')),temp_img)except Exception as e:print(e)print('Wrong Image:', img_name)continueprint(img_name + '-->', img_name.replace('png', 'jpg'))for file in val_path:# shutil.copy(file.replace("json", "jpg"), "%scoco/images/val2017/" % saved_coco_path)img_name = file.replace('json', 'jpg')temp_img = cv2.imread(img_name)try:cv2.imwrite("{}coco/images/val2017/{}".format(saved_coco_path, img_name.split('\\')[-1].replace('png', 'jpg')),temp_img)except Exception as e:print(e)print('Wrong Image:', img_name)continueprint(img_name + '-->', img_name.replace('png', 'jpg'))# 把验证集转化为COCO的json格式l2c_val = Lableme2CoCo()val_instance = l2c_val.to_coco(val_path)l2c_val.save_coco_json(val_instance, '%scoco/annotations/instances_val2017.json' % saved_coco_path)
  • 修改下面类别为自己的类别
classname_to_id = {"mouth": 0,  # 改成自己的类别"teeth": 1,"tongue": 2,"uvula": 3,"oropharynx": 4
}
  • 修改原label位置和保存的新生成label位置。
    labelme_path = "./data/mouth/train"saved_coco_path = "./data/"

-划分训练集和测试集的比例

 train_path, val_path = train_test_split(json_list_path, test_size=0.0001, train_size=0.9999)

2. 转化方法二

import argparse
import json
import matplotlib.pyplot as plt
import skimage.io as io
# import cv2
from labelme import utils
import numpy as np
import glob
import PIL.Imageclass MyEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()else:return super(MyEncoder, self).default(obj)class labelme2coco(object):def __init__(self, labelme_json=[], save_json_path='./tran.json'):self.labelme_json = labelme_jsonself.save_json_path = save_json_pathself.images = []self.categories = []self.annotations = []# self.data_coco = {}self.label = []self.annID = 1self.height = 0self.width = 0self.save_json()def data_transfer(self):for num, json_file in enumerate(self.labelme_json):with open(json_file, 'r') as fp:data = json.load(fp)  # 加载json文件self.images.append(self.image(data, num))for shapes in data['shapes']:label = shapes['label']if label not in self.label:self.categories.append(self.categorie(label))self.label.append(label)points = shapes['points']  # 这里的point是用rectangle标注得到的,只有两个点,需要转成四个点points.append([points[0][0], points[1][1]])points.append([points[1][0], points[0][1]])self.annotations.append(self.annotation(points, label, num))self.annID += 1def image(self, data, num):image = {}img = utils.img_b64_to_arr(data['imageData'])  # 解析原图片数据# img=io.imread(data['imagePath']) # 通过图片路径打开图片# img = cv2.imread(data['imagePath'], 0)height, width = img.shape[:2]img = Noneimage['height'] = heightimage['width'] = widthimage['id'] = num + 1image['file_name'] = data['imagePath'].split('/')[-1]self.height = heightself.width = widthreturn imagedef categorie(self, label):categorie = {}categorie['supercategory'] = 'Cancer'categorie['id'] = len(self.label) + 1  # 0 默认为背景categorie['name'] = labelreturn categoriedef annotation(self, points, label, num):annotation = {}annotation['segmentation'] = [list(np.asarray(points).flatten())]annotation['iscrowd'] = 0annotation['image_id'] = num + 1# annotation['bbox'] = str(self.getbbox(points)) # 使用list保存json文件时报错(不知道为什么)# list(map(int,a[1:-1].split(','))) a=annotation['bbox'] 使用该方式转成listannotation['bbox'] = list(map(float, self.getbbox(points)))annotation['area'] = annotation['bbox'][2] * annotation['bbox'][3]# annotation['category_id'] = self.getcatid(label)annotation['category_id'] = self.getcatid(label)  # 注意,源代码默认为1annotation['id'] = self.annIDreturn annotationdef getcatid(self, label):for categorie in self.categories:if label == categorie['name']:return categorie['id']return 1def getbbox(self, points):# img = np.zeros([self.height,self.width],np.uint8)# cv2.polylines(img, [np.asarray(points)], True, 1, lineType=cv2.LINE_AA)  # 画边界线# cv2.fillPoly(img, [np.asarray(points)], 1)  # 画多边形 内部像素值为1polygons = pointsmask = self.polygons_to_mask([self.height, self.width], polygons)return self.mask2box(mask)def mask2box(self, mask):'''从mask反算出其边框mask:[h,w]  0、1组成的图片1对应对象,只需计算1对应的行列号(左上角行列号,右下角行列号,就可以算出其边框)'''# np.where(mask==1)index = np.argwhere(mask == 1)rows = index[:, 0]clos = index[:, 1]# 解析左上角行列号left_top_r = np.min(rows)  # yleft_top_c = np.min(clos)  # x# 解析右下角行列号right_bottom_r = np.max(rows)right_bottom_c = np.max(clos)# return [(left_top_r,left_top_c),(right_bottom_r,right_bottom_c)]# return [(left_top_c, left_top_r), (right_bottom_c, right_bottom_r)]# return [left_top_c, left_top_r, right_bottom_c, right_bottom_r]  # [x1,y1,x2,y2]return [left_top_c, left_top_r, right_bottom_c - left_top_c,right_bottom_r - left_top_r]  # [x1,y1,w,h] 对应COCO的bbox格式def polygons_to_mask(self, img_shape, polygons):mask = np.zeros(img_shape, dtype=np.uint8)mask = PIL.Image.fromarray(mask)xy = list(map(tuple, polygons))PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)mask = np.array(mask, dtype=bool)return maskdef data2coco(self):data_coco = {}data_coco['images'] = self.imagesdata_coco['categories'] = self.categoriesdata_coco['annotations'] = self.annotationsreturn data_cocodef save_json(self):self.data_transfer()self.data_coco = self.data2coco()# 保存json文件json.dump(self.data_coco, open(self.save_json_path, 'w'), indent=4, cls=MyEncoder)  # indent=4 更加美观显示labelme_json = glob.glob('./data/mouth/train/*.json')
# labelme_json=['./1.json']labelme2coco(labelme_json, './data/coco/annotations/instances_train2017.json')
  • 修改原数据集的label文件位置
labelme_json = glob.glob('./data/mouth/train/*.json')
  • 修改生成的新label文件的位置和名称
labelme2coco(labelme_json, './data/coco/annotations/instances_train2017.json')

Labelme标注的json数据转化为coco格式的数据相关推荐

  1. labelme标注的json文件转化成ICDAR2015格式

    在转换之前一定要备份备份备份备份备份!!!!!!!!!!!!!!!!!! # -*- coding: utf-8 -*-import os import jsondef jsonTotxt(jsonf ...

  2. LabelMe标记后的json文件怎么转为COCO格式

    LabelMe标记后的json文件怎么转为COCO格式 步骤如下: Step 1: 下载labelme ,链接为https://github.com/wkentaro/labelme Step 2: ...

  3. labelme标注的json标签转txt格式

    在机器学习训练中常常会有不同格式的标签之间的转换,本次是将json标签格式转化成txt格式,在此记录一下. 1.labelme标注: 2.json文件: {"version": & ...

  4. Python修改图片格式以及相对应labelme标注的Json文件

    Python修改图片格式以及相对应labelme标注的Json文件 前言 前提条件 相关介绍 实验环境 Python修改图片格式以及相对应labelme标注的Json文件 代码实现 输出结果 前言 本 ...

  5. 目标检测,将voc格式转化为coco格式详细过程

    在目标检测方法研究中,数据集的格式至关重要,为了减小模型的训练时长,需要现在小数据集进行模型研究,从而需要将VOC数据集转化为coco格式.下面一步一步来看: 1. 下载VOC数据集 Pascal V ...

  6. python typing typescript_将 python 数据转化为 TypeScript 格式

    前段时间写过这篇文章将 python 数据转化为 lua 格式, 这段时间因为新项目改用 Creator + TypeScript 的原因, 需要导出 ts 格式的数据. 当然我们可以选择使用 jso ...

  7. R语言把dataframe数据转化为tibble格式、查看每个数据列的缺失值个数、使用数据列的均值对数据列的缺失值进行填充

    R语言把dataframe数据转化为tibble格式.查看每个数据列的缺失值个数.使用数据列的均值对数据列的缺失值进行填充 目录

  8. LabelMe标注的.json文件批量转Dataset,通过查询到多种代码无法在我电脑上正常工作,发现他们有共同的报错,于是修改后能正常工作。

    1.写在前面 最近用到标注好的数据训练神经网络,用labelme标注好之后生成.json文件,我要实现的功能是目标分割,需要把.json文件转为VOC格式的数据.但是.json文件太多,于是在网上找一 ...

  9. 【深度学习之路记录02】python代码批量修改Labelme标注的json文件(删除标签、修改标签名)

    代码参考:https://blog.csdn.net/qq_44442727/article/details/112785978 创建自己的数据集时,经常需要一些调整,比如说修改某一批文件中已经标好的 ...

最新文章

  1. 【git】git入门之把自己的项目上传到github
  2. SurfaceView的一个小应用:开发示波器
  3. 一次完整的HTTP事务过程--超详细!
  4. jquery通过ajax提交form
  5. CentOS源码编译安装MySQL 5.5.15(转)
  6. 数据eda_银行数据EDA:逐步
  7. ios服务器需要开启ipv6的支持,针对iOS审核要求为应用兼容IPv6
  8. 备份自己常用的VS2010设置
  9. Oracle中用于发送邮件的存储过程
  10. 矩阵的逆及求逆矩阵的方法,可逆矩阵定理与判定方法,(非)奇异矩阵,方程Ax=b解法,Hilbert矩阵及其逆的求法,条件数(Condition Number)及其计算方法
  11. 什么是天线阵列及天线阵列类型
  12. 如何烧写Bubblegum-96 board
  13. Serverless开源框架对比
  14. STM32F10xxx20xxx21xxxL1xxxx Cortex-M3程序设计手册 阅读笔记三(4):Cortex-M3指令分类
  15. 【教程】花100块钱DIY一台民航客机雷达
  16. GPS简介和定位过程
  17. vulnhub之FirstBlood: 1
  18. 域名系统几类服务器,域名系统
  19. 24种常用颜色的中英文名字
  20. 拓事件|又双叒叕获客户赞誉,拓保获评中联重科“优秀合作伙伴”

热门文章

  1. windows系统coursera视频播放出错解决方法
  2. python短网址转换
  3. HTTPS 方式访问网站
  4. C语言math.h详解
  5. 目标网站怎么样才能收集尽可能详细信息
  6. 软件体系结构:应用软件的设计与开发
  7. 毕业设计-基于微信小程序的疫情防控系统
  8. Java Web应用开发——作业五
  9. 【模拟】寄居蟹与海葵
  10. 地理信息系统明年将服务全运会