labelme标注后多个json文件转标准coco格式

labelme标注后多个json文件转标准coco格式

labelme软件标注后是每张图片都有一个.json文件,这与常用标准coco格式不一致,因此需要进行转换,避免忘记特此记录。

一、下载labelme2coco-master,并cd进去后运行 pip install labelme2coco 进行安装

可直接在此处下载安装对应版本labelme2coco软件,也可找到github版本,cd到相应目录后运行下列命令进行安装

pip install labelme2coco

二、找出所有数据中属于图片的(也可找到所有属于.json的,避免重复)

import os
path = r'E:\working\images'
li = os.listdir(path)
a = []
for i in li:
  if i.split('.')[-1]=='jpg':
    a.append(i)

三、划分训练集和测试集(此处是按照8:2进行划分,也可自行定义划分方式)

import random
ratio = 0.8
offset = int(len(a)*ratio)
random.shuffle(a) #将a中文件名打乱
train = a[:offset]
test = a[offset:]

四、将训练集和测试集数据分开

import shutil
path = r'E:\working\images'
path_train = r'E:\working\coco\train'
path_test = r'E:\working\coco\test'
for i in train:
  shutil.copy(os.path.join(path,i), os.path.join(path_train, i)) #图片
  shutil.copy(os.path.join(path,i.split('.')[0]+'.json'), os.path.join(path_train, i.split('.')[0]+'.json')) #标注
for i in test:
  shutil.copy(os.path.join(path,i), os.path.join(path_test, i)) #图片
  shutil.copy(os.path.join(path,i.split('.')[0]+'.json'), os.path.join(path_test, i.split('.')[0]+'.json')) #标注

五、json文件合并

import labelme2coco
# 训练集
labelme_folder = r'E:\working\coco\train'
save_json_path = r'E:\working\coco\train\train_coco.json'
labelme2coco.convert(labelme_folder, save_json_path)
# 测试集
labelme_folder = r'E:\working\coco\test'
save_json_path = r'E:\working\coco\test\test_coco.json'
labelme2coco.convert(labelme_folder, save_json_path)

自此便完成了全部数据转换,包括train和test两个文件夹分别存放训练和测试用的图片,还生成了train_coco.json、test_coco.json两个文件,包括所有的训练和测试标签及类别信息。

版权声明:本文为CSDN博主「developer&learner」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44034578/article/details/121914119

developer&learner

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

Anchor Free系列模型13

2021SCSDUSC CenterNet训练自己的数据集(接上文) 注意这里json文件的命名要通过datasets/pascal.py中第44到48行的内容确定的。 self.data_dir os.path

YOLOX训练自己的数据集

旷视的研究者将解耦头、数据增强、无锚点以及标签分类等目标检测领域的优秀进展与 YOLO 进行了巧妙地集成组合,提出了 YOLOX,不仅实现了超越 YOLOv3、YOLOv4 和 YOLOv5 的 AP,