半自动辅助制作数据集【实例分割】

利用yoloV8的实例分割模型,半自动辅助制作数据集


引言:【主要步骤】

步骤1:无人机航拍,收集基础图片
步骤2:将收集到的图片,全部用yoloV8-seg.pt模型进行实例分割【预测之前,将配置文件default.yaml的save_txt:设置为True】
步骤3:将txt标注文件转换为JSON格式
步骤4:打开labelme开始修改再加工标注

  • 步骤1:【比如,如下规格】
    图片特征数据:车辆、人,等等
    图片拍摄变量:无人机高度、相机水平方向、相机俯仰角度
    (a)固定高度、水平方向,调整俯仰角
    (b)固定水平方向、俯仰角,调整高度
    (c)固定俯仰角、高度,调整水平方向

  • 步骤2:【预测代码及配置】
    default.yaml【save_txt:设置为True】 default.yaml
    使用脚本预测:my_predict.py【source参数:存放图片的目录路径】
    在这里插入图片描述
    得到的预测结果会存放在run文件夹下
    在这里插入图片描述

  • 步骤3:【将txt文件转换为JSON文件】
    利用我在Cityscapes数据集转换为COCO数据集的文章中写的方法,改一下坐标为缩放前的即可:

import os
import cv2
import json
import glob
import numpy as np

def convert_txt_to_labelme_json(txt_path, image_path, output_dir, image_fmt='.jpg'):
    # txt 转labelme json
    txts = glob.glob(os.path.join(txt_path, "*.txt"))
    for txt in txts:
        labelme_json = {
            'version': '4.5.7',
            'flags': {},
            'shapes': [],
            'imagePath': None,
            'imageData': None,
            'imageHeight': None,
            'imageWidth': None,
        }
        txt_name = os.path.basename(txt)
        image_name = txt_name.split(".")[0] + image_fmt
        labelme_json['imagePath'] = image_name
        image_name = os.path.join(image_path, image_name)
        if not os.path.exists(image_name):
            raise Exception('txt 文件={},找不到对应的图像={}'.format(txt, image_name))
        image = cv2.imdecode(np.fromfile(image_name, dtype=np.uint8), cv2.IMREAD_COLOR)
        h, w = image.shape[:2]
        labelme_json['imageHeight'] = h
        labelme_json['imageWidth'] = w
        with open(txt, 'r') as t:
            lines = t.readlines()
            for line in lines:
                content = line.split(' ')
                label = content[0]
                tem_label=str(label)
                #   0: car #车
                #   1: street  #街道、路
                #   2: person #人
                #   3: lawn #草坪
                #   4: construction #建筑
                #   5: tree #树,树林
                mapLabel={
                    "0":"car",
                    "1":"street",
                    "2":"person",
                    "3":"lawn",
                    "4":"construction",
                    "5":"tree"
                }
                shape = {
                    'label': mapLabel.get(str(label)),
                    'flags': {},
                    'points': []
                }
                for i in range(len(content)):
                    if 2 * i + 1 >= len(content):
                        break
                    else:
                        try:
                            shape['points'].append([float(content[2 * i + 1])*w, float(content[2 * i + 2])*h])
                        except Exception as e:
                            print(e)

                labelme_json['shapes'].append(shape)
            json_name = txt_name.split('.')[0] + '.json'
            json_name_path = os.path.join(output_dir, json_name)
            fd = open(json_name_path, 'w')
            json.dump(labelme_json, fd, indent=4)
            fd.close()
            print("save json={}".format(json_name_path))


if __name__=="__main__":
    in_imgs_dir = 'D:\\yoloProject\\ultralytics-registry\\test_11\\imgs'
    in_label_txt_dir = 'D:\\yoloProject\\ultralytics-registry\\test_11\\labels'
    out_labelme_json_dir = 'D:\\yoloProject\\ultralytics-registry\\test_11\\jsons'

    if not os.path.exists(out_labelme_json_dir):
        os.mkdir(out_labelme_json_dir)
    convert_txt_to_labelme_json(in_label_txt_dir,in_imgs_dir,out_labelme_json_dir,image_fmt='.png')

转换前:
在这里插入图片描述
转换后得到:
在这里插入图片描述

  • 步骤4【将生成的JSON文件和原图片文件放到同一个文件夹下,然后lableme打开目录】
    在这里插入图片描述

在这里插入图片描述

相关推荐

  1. YOLOv8分割训练及分割半自动标注

    2024-07-16 14:48:01       47 阅读
  2. DataLoader自定义数据制作

    2024-07-16 14:48:01       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 14:48:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 14:48:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 14:48:01       58 阅读
  4. Python语言-面向对象

    2024-07-16 14:48:01       69 阅读

热门阅读

  1. Spring Boot项目中,对接口请求参数打印日志

    2024-07-16 14:48:01       22 阅读
  2. 第二十一条:为传诸后世而设计接口

    2024-07-16 14:48:01       19 阅读
  3. 墨烯的C语言技术栈-C语言基础-015

    2024-07-16 14:48:01       19 阅读
  4. JWT令牌认证介绍及安全风险

    2024-07-16 14:48:01       21 阅读
  5. 导航专业入门,高考/考研假期预习指南

    2024-07-16 14:48:01       20 阅读
  6. Zookeeper + Kafka 消息队列群集部署

    2024-07-16 14:48:01       21 阅读
  7. 常见的排序方法

    2024-07-16 14:48:01       22 阅读