Yolov5旋转目标检测数据集labelme生成的json转txt

利用labelme工具进行四点标注生成的json文件,结合原图,通过Python脚本转换成适合Yolov5训练的txt格式,包含坐标(x, y)、宽(w)、高(h)及角度(angle)信息。" 108269768,8600784,MATLAB中函数重载nargin和nargout的使用,"['MATLAB编程', '函数设计', '参数处理', '编程技巧']

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

用labelme polygons标出四个点生成的json文件和原图一起放到data文件夹中,同级目录下运行下面的python文件生成txt


 

import os

import numpy as np

import json

from glob import glob

import cv2

import math

from sklearn.model_selection import train_test_split

from os import getcwd


classes = ["0", "1"]#写自己数据集的标签


labelme_path = "data/" #数据集路径


isUseTest = True # 是否创建test集


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


train_files, val_files = train_test_split(trainval_files, test_size=0.1, random_state=55)

contours=[]

rects=[]


def convert(size, box):

dw = 1. / (size[0])

dh = 1. / (size[1])

x1 = ((box[0] + box[1]) / 2.0 - 1)*dw#归一化

y1 = ((box[2] + box[3]) / 2.0 - 1)*dh

for rect in rects:

x, y = rect[0]

width,height=rect[1]

angle=rect[2]

if width<height:

### 关于旋转目标检测中的汽车数据集 对于旋转目标检测领域,尤其是涉及汽车的目标检测任务,可以考虑以下几个公开的数据集或者方法来获取相关资源: #### 1. **DOTA 数据集** DOTA 是一个大规模的遥感图像数据集,涵盖了多种类别,其中包括车辆(car)类别的标注。该数据集中包含了大量具有方向角信息的对象框标注,非常适合用于旋转目标检测的研究[^2]。 - 官方网站:http://captain.whu.edu.cn/DOTAweb/ - 特点:提供丰富的遥感场景图片以及对应的旋转矩形标注。 #### 2. **HRSC2016 数据集** HRSC2016 是另一个专注于高分辨率船舶和车辆识别的遥感数据集。虽然其主要关注的是船只,但也包含部分车辆的相关标注,适合扩展到其他类型的旋转物体检测研究中[^3]。 - GitHub 链接:https://github.com/SysCV/hrsc2016 - 特点:提供了详细的旋转边界框标注,并支持多尺度下的对象检测。 #### 3. **DIOR 数据集** DIOR 提供了一个多样化的遥感图像集合,覆盖了多个领域内的不同种类目标物,包括但不限于飞机、船舰、车辆等。此数据集同样具备精确的方向角度标签,能够很好地满足旋转目标检测的需求[^4]。 - 论文链接:https://arxiv.org/abs/2009.07856 - 下载页面:https://www.rsipr.com/dior/ #### 工具推荐与自定义数据准备 如果上述现有数据无法完全匹配具体需求,则可以通过工具自行创建适配的数据集。例如提到过的 roLabelImg 可帮助快速完成带方向信息的目标标记工作;而 labelme 则允许通过简单的界面操作生成 JSON 文件后再换成 TXT 形式的标注文件以便后续处理[^1][^4]。 ```python import json from pathlib import Path def convert_labelme_to_txt(json_file, output_dir): with open(json_file) as f: data = json.load(f) shapes = data['shapes'] img_width = data['imageWidth'] img_height = data['imageHeight'] txt_content = [] for shape in shapes: points = shape["points"] class_name = shape["label"] # Convert polygon points into bounding box format xs = [p[0] for p in points] ys = [p[1] for p in points] bbox_min_x = min(xs) / img_width bbox_max_x = max(xs) / img_width bbox_min_y = min(ys) / img_height bbox_max_y = max(ys) / img_height center_x = (bbox_min_x + bbox_max_x) / 2 center_y = (bbox_min_y + bbox_max_y) / 2 width = abs(bbox_max_x - bbox_min_x) height = abs(bbox_max_y - bbox_min_y) line = f"{class_name} {center_x:.6f} {center_y:.6f} {width:.6f} {height:.6f}\n" txt_content.append(line) output_path = Path(output_dir) / (Path(json_file).stem + ".txt") with open(output_path, "w") as out_f: out_f.writelines(txt_content) # Example usage convert_labelme_to_txt('example.json', './output') ``` 以上脚本展示了如何将由 LabelMe 创建的 JSON 文件化为 YOLOv5 或者类似的框架所需要的 TXT 格式标注文件。 ---
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shelly-you

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值