YOLO分割数据集制作:使用Labelme工具制作分割数据集(.json)并转化为YOLO的数据集的格式(.txt)

文章讲述了如何使用Labelme工具制作图像分割数据集,然后将Labelme的JSON格式转换为COCO格式,最后将COCO格式转换为YOLO格式所需的标注文件。这个过程涉及到代码修改和特定格式的文本文件如labels.txt的创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Labelme制作数据集

  • 打开labelme,标注数据,生成文件目录如下:
    • data
      • a.jpg
      • a.json
      • b.jpg
      • b.json

二、将labelme格式的数据转换为coco格式(转换后的coco格式还是json文件)

  • 代码下载:instance_segmentation

  • 修改:

    • 找到78行

      class_name_to_id = {}
      for i, line in enumerate(open(args.labels).readlines()):
          # class_id = i-1  # starts with -1
          class_id = i ## 没有背景,所以索引地址从0开始
          class_name = line.strip()
          if class_id == -1:
              assert class_name == "__ignore__"
              continue
      
    • 创建labels.txt(与data文件夹在同一个文件目录中)

      • data
        • a.jpg
        • a.json
        • b.jpg
        • b.json
      • labels.txt

      labels.txt内容:

      ```dotnetcli
      __ignore__
      class 0
      class 1
      ....
      
      ```
      
  • 开始转换json格式

    python labelme2coco.py ..path/data ..path/data_dataset_coco --labels labels.txt
    

三、将coco格式转换为YOLO格式

  • 文件下载:COCO2YOLO

  • 修改

    • 修改1:找到 306 行
      ## with open((fn / f).with_suffix('.txt'), 'a') as file:
      with open((fn / f[11:]).with_suffix('.txt'), 'a') as file:
      
    • 修改2:找到主函数:修改json路径为你的文件路径
      if source == 'COCO':
          convert_coco_json('..path/data_dataset_coco',  # directory with *.json
                            use_segments=True,
                            cls91to80=True)
      
  • 开始转换json格式

    python general_json2yolo.py
    
  • 生成的yolo标注文件在:./new_dir/labels/annotations/

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值