YoloV5划分自己的数据集并将json文件(矩形标注)转换为yolo可以识别的txt文件(全过程/服务器上搭建)

1. 划分数据集
将图片数据集划随机划分为训练:验证:测试=6:2:2的比例,同时划分对应的json目录文件。废话不多说,直接上代码:

import os
import random
import shutil

# 指定原始数据目录
images_dir = "images"
json_dir = "Annotations"

# 指定目标输出目录
output_dir_images = "../../datasets/fish/images"
output_dir_json = "../../datasets/fish/annotations"

# 创建目标输出目录
if not os.path.exists(output_dir_images):
    os.makedirs(output_dir_images)
if not os.path.exists(output_dir_json):
    os.makedirs(output_dir_json)

# 创建训练集、验证集和测试集子目录
subdirectories = ["train", "val", "test"]

for subdir in subdirectories:
    subdir_path_images = os.path.join(output_dir_images, subdir)
    subdir_path_json = os.path.join(output_dir_json, subdir)

    if not os.path.exists(subdir_path_images):
        os.makedirs(subdir_path_images)
    if not os.path.exists(subdir_path_json):
        os.makedirs(subdir_path_json)

# 获取图像文件和 JSON 文件列表
image_files = [f for f in os.listdir(images_dir) if f.endswith(".jpg")]
json_files = [f for f in os.listdir(json_dir) if f.endswith(".json")]

# 打乱数据的顺序
random.shuffle(image_files)

# 计算划分的数据集大小
total_samples = len(image_files)
train_size = int(total_samples * 0.6)
val_size = int(total_samples * 0.2)
test_size = total_samples - train_size - val_size

# 划分数据集
train_images = image_files[:train_size]
val_images = image_files[train_size:train_size + val_size]
test_images = image_files[train_size + val_size:]

# 移动图像文件到对应的子目录
for image_file in train_images:
    src_path_images = os.path.join(images_dir, image_file)
    dest_path_images = os.path.join(output_dir_images, "train", image_file)
    shutil.copy(src_path_images, dest_path_images)

for image_file in val_images:
    src_path_images = os.path.join(images_dir, image_file)
    dest_path_images = os.path.join(output_dir_images, "val", image_file)
    shutil.copy(src_path_images, dest_path_images)

for image_file in test_images:
    src_path_images = os.path.join(images_dir, image_file)
    dest_path_images = os.path.join(output_dir_images, "test", image_file)
    shutil.copy(src_path_images, dest_path_images)

# 移动 JSON 文件到对应的子目录
for json_file in json_files:
    base_name = os.path.splitext(json_file)[0]
    if f"{base_name}.jpg" in train_images:
        src_path_json = os.path.join(json_dir, json_file)
        dest_path_json = os.path.join(output_dir_json, "train", json_file)
        shutil.copy(src_path_json, dest_path_json)
    elif f"{base_name}.jpg" in val_images:
        src_path_json = os.path.join(json_dir, json_file)
        dest_path_json = os.path.join(output_dir_json, "val", json_file)
        shutil.copy(src_path_json, dest_path_json)
    elif f"{base_name}.jpg" in test_images:
        src_path_json = os.path.join(json_dir, json_file)
        dest_path_json = os.path.join(output_dir_json, "test", json_file)
        shutil.copy(src_path_json, dest_path_json)

print("数据集划分完成。")

路径都用的是相对路径,如果不理解相对路径,可以这么理解:就是以此文件的路径开始,**举个例子:**我的images图片目录和此划分数据集的文件在同一个目录之下,则输入图片目录的时候,就直接写入图片的目录名称即可:
在这里插入图片描述

可以从图中看到,我的划分代码和images目录在在同一个目录VOCData下面。所以输入的图片目录就是:
在这里插入图片描述
…/ 这里的…(两个.)代表上级目录,因为我是用linux服务器进行划分的。
以下是我的目录格式,可以进行参考:
在这里插入图片描述
执行过后,两个数据集(images,json)就被划分为了train,val,test三个子目录。并且这两个数据集的子目录的文件都是一一对应的。如图所示:
在这里插入图片描述
此刻,划分数据集到此结束!
2. json转txt
因为上一步把json的文件夹划分为了train,val和test三个子目录,接下来要做的就是将这三个子目录的json文件都转换为txt文件。这个可以参考我的上一篇文章:python实现将一个文件夹中的多个 JSON 文件批量转换为 YOLO 可以识别的 TXT 文件

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSON格式的多边形转换YOLO格式矩形需要进行以下几个步骤: 1. 解析JSON文件,获取多边形的顶点坐标和标注类别; 2. 将多边形转换矩形,即获取矩形的左上角坐标、右下角坐标和标注类别; 3. 将矩形的坐标转换为相对坐标,并计算矩形的中心点坐标和宽高比例; 4. 将相对坐标、标注类别、中心点坐标和宽高比例保存为YOLO格式txt文件。 下面是一个Python代码示例,可以实现将JSON格式的多边形转换YOLO格式矩形: ```python import json def polygon_to_yolo(json_file, output_file): with open(json_file, 'r') as f: data = json.load(f) for item in data: # 获取多边形的顶点坐标和标注类别 points = item['points'] label = item['label'] # 将多边形转换矩形 x_coords = [p[0] for p in points] y_coords = [p[1] for p in points] left = min(x_coords) top = min(y_coords) right = max(x_coords) bottom = max(y_coords) # 计算矩形的中心点坐标和宽高比例 x_center = (left + right) / 2 y_center = (top + bottom) / 2 width = right - left height = bottom - top aspect_ratio = width / height # 将相对坐标、标注类别、中心点坐标和宽高比例保存为YOLO格式txt文件 with open(output_file, 'a') as f_out: f_out.write(f"{label} {x_center} {y_center} {width} {height} {aspect_ratio}\n") ``` 在上述代码中,我们首先使用json.load()函数将JSON文件解析为Python对象,然后遍历每一个对象,获取多边形的顶点坐标和标注类别。接着,我们将多边形转换矩形,并计算矩形的中心点坐标和宽高比例。最后,我们将相对坐标、标注类别、中心点坐标和宽高比例保存为YOLO格式txt文件。在保存时,我们使用“标注类别 x_center y_center width height aspect_ratio”的格式

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值