Win10 Labelme标注数据转为YOLOV5 训练的数据集

简介: Win10 Labelme标注数据转为YOLOV5 训练的数据集

将Labelme标注的数据复制到工程的根目录,并将其命名为LabelmeData。我的工程根目录是

tt.png

import os
import numpy as np
import json
from glob import glob
import cv2
from sklearn.model_selection import train_test_split
from os import getcwd
classes = ["aircraft", "oiltank"]
# 1.标签路径
labelme_path = "LabelmeData/"
isUseTest = True  # 是否创建test集
# 3.获取待处理文件
files = glob(labelme_path + "*.json")
files = [i.replace("\\", "/").split("/")[-1].split(".json")[0] for i in files]
print(files)
if isUseTest:
    trainval_files, test_files = train_test_split(files, test_size=0.1, random_state=55)
else:
    trainval_files = files
# split
train_files, val_files = train_test_split(trainval_files, test_size=0.1, random_state=55)
def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)
wd = getcwd()
print(wd)
def ChangeToYolo5(files, txt_Name):
    if not os.path.exists('tmp/'):
        os.makedirs('tmp/')
    list_file = open('tmp/%s.txt' % (txt_Name), 'w')
    for json_file_ in files:
        json_filename = labelme_path + json_file_ + ".json"
        imagePath = labelme_path + json_file_ + ".jpg"
        list_file.write('%s/%s\n' % (wd, imagePath))
        out_file = open('%s/%s.txt' % (labelme_path, json_file_), 'w')
        json_file = json.load(open(json_filename, "r", encoding="utf-8"))
        height, width, channels = cv2.imread(labelme_path + json_file_ + ".jpg").shape
        for multi in json_file["shapes"]:
            points = np.array(multi["points"])
            xmin = min(points[:, 0]) if min(points[:, 0]) > 0 else 0
            xmax = max(points[:, 0]) if max(points[:, 0]) > 0 else 0
            ymin = min(points[:, 1]) if min(points[:, 1]) > 0 else 0
            ymax = max(points[:, 1]) if max(points[:, 1]) > 0 else 0
            label = multi["label"]
            if xmax <= xmin:
                pass
            elif ymax <= ymin:
                pass
            else:
                cls_id = classes.index(label)
                b = (float(xmin), float(xmax), float(ymin), float(ymax))
                bb = convert((width, height), b)
                out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
                print(json_filename, xmin, ymin, xmax, ymax, cls_id)
ChangeToYolo5(train_files, "train")
ChangeToYolo5(val_files, "val")
ChangeToYolo5(test_files, "test")
'''
file1 = open("tmp/train.txt", "r")
file2 = open("tmp/val.txt", "r")
file_list1 = file1.readlines()  # 将所有变量读入列表file_list1
file_list2 = file2.readlines()  # 将所有变量读入列表file_list2
file3 = open("tmp/trainval.txt", "w")
for line in file_list1:
    print(line)
    file3.write(line)
for line in file_list2:
    print(line)
    file3.write(line)
'''


 

  • 打开工程,在根目录新建LabelmeToYolov5.py。写入下面的代码

这段代码执行完成会在LabelmeData生成每个图片的txt标注数据,同时在tmp文件夹下面生成训练集、验证集和测试集的txt,txt记录的是图片的路径,为下一步生成YoloV5训练和测试用的数据集做准备。


在tmp文件夹新建makedata.py。执行完成后会在工程的根目录生成VOC数据集。


import shutil
import os
file_List = ["train", "val", "test"]
for file in file_List:
    if not os.path.exists('../VOC/images/%s' % file):
        os.makedirs('../VOC/images/%s' % file)
    if not os.path.exists('../VOC/labels/%s' % file):
        os.makedirs('../VOC/labels/%s' % file)
    print(os.path.exists('../tmp/%s.txt' % file))
    f = open('../tmp/%s.txt' % file, 'r')
    lines = f.readlines()
    for line in lines:
        print(line)
        line = "/".join(line.split('/')[-5:]).strip()
        shutil.copy(line, "../VOC/images/%s" % file)
        line = line.replace('jpg', 'txt')
        shutil.copy(line, "../VOC/labels/%s/" % file)

tt.png


目录
相关文章
|
XML SQL Java
Mybatis之转义符的使用姿势
在 mybatis 的 xml 文件中直接写 sql 比较方便简洁,但是需要注意的是,在 xml 文件中,经常会遇到一些需要转义的场景,比如查询 id < xxx的数据,这个小于号就不能直接写在 sql 中,接下来我们将看一下,mybatis 中的有哪些转义符,可以怎么处理转义问题
1741 0
Mybatis之转义符的使用姿势
|
机器学习/深度学习 网络架构
浅谈神经网络中的bias
1、什么是bias? 偏置单元(bias unit),在有些资料里也称为偏置项(bias term)或者截距项(intercept term),它其实就是函数的截距,与线性方程 y=wx+b 中的 b 的意义是一致的。在 y=wx+b中,b表示函数在y轴上的截距,控制着函数偏离原点的距离,其实在神经网络中的偏置单元也是类似的作用。 因此,神经网络的参数也可以表示为:(W, b),其中W表示参数矩阵,b表示偏置项或截距项。
2447 0
浅谈神经网络中的bias
|
传感器 人工智能 边缘计算
当无人机遇上5G:远程控制再也不卡了
当无人机遇上5G:远程控制再也不卡了
596 8
|
人工智能 自然语言处理 程序员
下载量突破400万,百万开发者首选的 AI 编码工具通义灵码是如何炼成的?
下载量突破400万,百万开发者首选的 AI 编码工具通义灵码是如何炼成的?
1016 3
|
前端开发 搜索推荐 NoSQL
错过就亏大了!这款开源 IM 项目,功能强大、部署简单,适合私有化部署,开发者必收藏!
Lumen IM 是一个基于 Vue3 + Naive UI 前端与 Go 后端的开源即时通讯系统,支持私聊、群聊、文件传输、消息撤回等功能,适用于企业内部沟通及开发者学习。项目采用 WebSocket 实现即时消息推送,技术栈包括 MySQL、Redis 和 Minio 等,确保高效稳定运行。界面简约美观,操作便捷,并支持笔记功能和个性化设置。无论是企业使用还是二次开发,Lumen IM 都是一个优质选择
1359 0
|
人工智能 自然语言处理 并行计算
EchoMimicV2:阿里推出的开源数字人项目,能生成完整数字人半身动画
EchoMimicV2是阿里蚂蚁集团推出的开源数字人项目,能够生成完整的数字人半身动画。该项目基于参考图片、音频剪辑和手部姿势序列,通过音频-姿势动态协调策略生成高质量动画视频,确保音频内容与半身动作的一致性。EchoMimicV2不仅支持中文和英文驱动,还简化了动画生成过程中的复杂条件,适用于虚拟主播、在线教育、娱乐和游戏等多个应用场景。
4972 5
EchoMimicV2:阿里推出的开源数字人项目,能生成完整数字人半身动画
|
机器学习/深度学习 数据采集 数据可视化
使用Python实现深度学习模型:智能植物生长监测与优化
使用Python实现深度学习模型:智能植物生长监测与优化
1236 0
|
Ubuntu C语言
Ubuntu升级Cmake、gcc、g++
以上就是在Ubuntu中升级Cmake、gcc、g++的步骤。
1422 2
|
Web App开发 JavaScript Android开发
微信小程序之内嵌网页(webview)
微信小程序提供了新的开放能力!它终于开放了在小程序中内嵌HTML页面的功能!从微信小程序基础库1.6.4开始,我们就可以在小程序内放置一个组件来链接我们的HTML页面了。
3685 0
|
机器学习/深度学习 存储 计算机视觉
基于YOLOv8深度学习的路面坑洞检测与分割系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标分割(2)
基于YOLOv8深度学习的路面坑洞检测与分割系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标分割