labelme数据集制作成coco数据集

1.labelme制作样本生成每个图像的json文件

2.将生成的json文件和原始图像jpg,放入同一个文件夹中(…\labelme-master\examples\instance_segmentation\data_annotated)也就是input_dir

3."…\labelme-master\examples\instance_segmentation"中修改labels.txt文件,保留__ignore__类别(否则会报错,源代码中可以修改也可以)。

4.打开终端并切换路径至…\labelme-master\examples\instance_segmentation,运行如下代码

python labelme2coco.py data_annotated output_dir --labels labels.txt

5.运行后会生成output_dir文件夹

  此文件内包含JPEGImages文件夹(包含了原始图像)、Visulaization文件夹以及annotations.json

运行coco_txt.py 将生成对象图片的txt图像信息保存至JPEGImages

from __future__ import print_function
import os, sys, zipfile
import json


def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]

    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)


json_file = 'annotations.json'  # # Object Instance 类型的标注

data = json.load(open(json_file, 'r'))

ana_txt_save_path = "save_path"  # 保存的路径
if not os.path.exists(ana_txt_save_path):
    os.makedirs(ana_txt_save_path)

for img in data['images']:
    # print(img["file_name"])
    filename = img["file_name"]
    img_width = img["width"]
    img_height = img["height"]
    # print(img["height"])
    # print(img["width"])
    img_id = img["id"]
    ana_txt_name = filename.split(".")[0] + ".txt"  # 对应的txt名字,与jpg一致
    print(ana_txt_name.replace('\\', '/'))
    with open(os.path.join(ana_txt_save_path, ana_txt_name).replace('\\', '/'), 'w') as f_txt:
        #   f_txt = open()
        for ann in data['annotations']:
            if ann['image_id'] == img_id:
                # annotation.append(ann)
                # print(ann["category_id"], ann["bbox"])
                box = convert((img_width, img_height), ann["bbox"])
                f_txt.write("%s %s %s %s %s\n" % (ann["category_id"], box[0], box[1], box[2], box[3]))
        f_txt.close()

使用train_val_txt.py生成train和val的绝对路径,该文件只是生成了train.txt,需要将train.txt中的txt改为jpg,因为针对的是image,这个是根据网上教程来的,其实是可以直接一步到位的。再在train.txt中选取部分作为val.txt的内容。新建labels文件夹,并将JPEGImages文件夹中的txt文件导入其中。到此coco数据集制作完毕。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值