via标注工具输出的json格式数据,如何转化为yolo格式数据?
3条回答 默认 最新
- Treabhar 2023-05-17 20:33关注
基于GPT4和Treabhar的编写:
在转换数据格式时,你需要知道YOLO训练格式的标准。对于YOLO,标注数据通常由一个.txt文件对应一个图像文件,.txt文件中包含了该图像的所有标注信息。每一行代表一个对象,每一行有5个值,分别是:
类别索引(从0开始计数)
对象中心x坐标(相对于图像宽度的比例)
对象中心y坐标(相对于图像高度的比例)
对象的宽度(相对于图像宽度的比例)
对象的高度(相对于图像高度的比例)
所有的值都是归一化的,范围在0到1之间。
Python脚本,它读取由VIA标注工具生成的JSON文件,并将其转换为YOLO格式:
import json import os from PIL import Image def convert(size, box): dw = 1. / size[0] dh = 1. / size[1] x = (box[0] + box[1]) / 2.0 y = (box[2] + box[3]) / 2.0 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] def convert_to_yolo_label(coco_format_json, yolo_format_path): with open(coco_format_json, "r") as json_file: data = json.load(json_file) images = data["_via_img_metadata"] for key, value in images.items(): filename = value["filename"] regions = value["regions"] for region in regions: x = region["shape_attributes"]["x"] y = region["shape_attributes"]["y"] width = region["shape_attributes"]["width"] height = region["shape_attributes"]["height"] class_id = region["region_attributes"]["class_id"] img = Image.open(os.path.join(yolo_format_path, filename)) w = int(img.size[0]) h = int(img.size[1]) bb = convert((w,h), (x,x+width,y,y+height)) with open(os.path.join(yolo_format_path, filename.split(".")[0] + ".txt"), "a") as yolo_file: yolo_file.write(str(class_id) + " " + " ".join([str(a) for a in bb]) + '\n') convert_to_yolo_label("via_export_json.json", "/path/to/yolo/data")
- 这个脚本假设你已经将VIA工具的输出标记为 "class_id" 的类别属性。如果你使用的是不同的属性名,那么需要修改脚本中的相应部分。此外,确保你的图像文件和VIA的JSON文件都位于正确的路径下。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
- ¥15 个人网站被恶意大量访问,怎么办
- ¥15 Vue3 大型图片数据拖动排序
- ¥15 Centos / PETGEM
- ¥15 划分vlan后不通了
- ¥15 GDI处理通道视频时总是带有白色锯齿
- ¥20 用雷电模拟器安装百达屋apk一直闪退
- ¥15 算能科技20240506咨询(拒绝大模型回答)
- ¥15 自适应 AR 模型 参数估计Matlab程序
- ¥100 角动量包络面如何用MATLAB绘制