使用Labelme标注数据

1. 下载labelme

下载地址: https://github.com/labelmeai/labelme/releases/download/v5.5.0/Labelme.exe

2.  打开目录

打开要标注的图片所在的目录,为了对应后面的代码,这里把路径假设为:  d:\imgs

3. 设置选项

  • 1. 菜单栏 => 文件 => 自动保存
  • 2. 菜单栏 => 文件 => 取消勾选 "同时保存图像数据"
  • 3. 菜单栏 => 编辑 => 保留最后的标注

4. 标注数据

这里使用矩形框标注,根据自己的情况选择即可.有个ai标注可以尝试一下

标注数据时,创建矩形框后,鼠标依然处于创建状态,这个时候可以右键或者工具类,选择"编辑多边形"即可退出创建状态.

5. 转换标注数据位yolo格式

把上面打开的目录(d:\imgs)中的所有图片和json文件,一个图片标准对应一个json文件,全部复制到程序所在目录下的imgs目录下,然后运行下面的代码即可.

运行代码后在程序所在的目录生成 train valid 目录,把这两个目录复制到yolo根目录训练即可

import os
import json
import random
from glob import glob
from PIL import Image

# 这里改为自己标注数据用到的标签
# className为标注数据时标签名
# ratio设置0.8表示80%数据用于训练,20%数据用于验证
dic_labels = {'classname': 0,
              'ratio': 0.8}


def generate_labels(dic_labs):
    ratio = dic_labs['ratio']
    for index, labelme_annotation_path in enumerate(glob(f'imgs/*.json')):

        # 读取labelme格式的json文件
        with open(labelme_annotation_path, 'r') as labelme_annotation_file:
            labelme_annotation = json.load(labelme_annotation_file)

        # 获取图像的路径和文件名(不含扩展名)
        image_path = os.path.join('imgs', labelme_annotation['imagePath'])
        image_id, image_ext = os.path.splitext(os.path.basename(image_path))

        # 计算是train 还是 valid
        train_or_valid = 'train' if random.random() < ratio else 'valid'

        # 从本地图像文件读取图像并保存到相应的目录
        with Image.open(image_path) as img:
            img.save(os.path.join(train_or_valid, 'images', f'{image_id}{image_ext}'))

        # yolo 格式的 labels
        yolo_annotation_path = os.path.join(train_or_valid, 'labels', f'{image_id}.txt')
        with open(yolo_annotation_path, 'w') as yolo_annotation_file:

            # 获取位置信息
            for shape in labelme_annotation['shapes']:
                if shape['shape_type'] != 'rectangle':
                    print(f'Invalid type `{shape["shape_type"]}` in annotation `{labelme_annotation_path}`')
                    continue

                points = shape['points']
                scale_width = 1.0 / labelme_annotation['imageWidth']
                scale_height = 1.0 / labelme_annotation['imageHeight']
                width = (points[1][0] - points[0][0]) * scale_width
                height = (points[1][1] - points[0][1]) * scale_height
                x = ((points[1][0] + points[0][0]) / 2) * scale_width
                y = ((points[1][1] + points[0][1]) / 2) * scale_height
                object_class = dic_labels[shape['label']]
                yolo_annotation_file.write(f'{object_class} {x} {y} {width} {height}\n')

        print("creat lab %d : %s" % (index, image_id))


if __name__ == "__main__":
    os.makedirs(os.path.join("train", 'images'), exist_ok=True)
    os.makedirs(os.path.join("train", 'labels'), exist_ok=True)
    os.makedirs(os.path.join("valid", 'images'), exist_ok=True)
    os.makedirs(os.path.join("valid", 'labels'), exist_ok=True)
    generate_labels(dic_labels)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值