python_json转yolo文件

devtools/2025/2/13 2:57:59/

文章内容
将labelme的内容转换成yolo需要的txt文件

python">import json
import os
import shutil
import random# Convert label to idx
# with open("labels.txt", "r") as f:
#     classes = [c.strip() for c in f.readlines()]
#     idx_dict = {c: str(i) for i, c in enumerate(classes)}# 传入3个路径
# 获取名称,通过名称和路径来确定生成的地址
def onefile_labelme_to_yolo(json_path,txt_path,img_path):# 检查输入的路径是否为有效的文件夹路径if os.path.isdir(json_path):# 遍历文件夹中的所有文件和文件夹for root, dirs, files in os.walk(json_path):for file in files:# 分离文件名和扩展名file_name, file_extension = os.path.splitext(file)# 生成xml文件路径txt_file = os.path.join(txt_path, file_name + '.txt')json_file = os.path.join(json_path, file)print(txt_file)# 读取json文件with open(json_file, 'r') as f:labelme_data = json.load(f)# 解析json文件image_filename = labelme_data["imagePath"]image_width = labelme_data["imageWidth"]image_height = labelme_data["imageHeight"]with open(txt_file, 'w') as f:for shape in labelme_data["shapes"]:label = shape["label"]points = shape["points"]x_min, y_min = points[0]x_max, y_max = points[1]center_x = round(((x_min + x_max) / 2) / image_width, 2)center_y = round(((y_min + y_max) / 2) / image_height, 2)width = round(abs(x_min - x_max) / image_width, 2)height = round(abs(y_min - y_max) / image_height, 2)# class_id = label_dict[label]f.write(f"{0}  {center_x:.6f} {center_y:.6f} {width} {height}\n")print(f"Converted {file} to {txt_path}")else:print(f"{json_path} 不是一个有效的文件夹路径。")if __name__ == '__main__':labelme_folder = "G:\\2025_study\\json"  # labelme生成的标注文件所在的文件夹txt_folder = "G:\\2025_study\\txt"  # 存储yolo标注文件的文件夹img_folder = "G:\\2025_study\\img"onefile_labelme_to_yolo(labelme_folder,txt_folder,img_folder)

http://www.ppmy.cn/devtools/157772.html

相关文章

WebRTC 客户端与ZLMediaKit通讯

1 web浏览器js方式 要使用 WebRTC 客户端与 ZLMediaKit 通讯,您需要设置一个 WebRTC 客户端并与 ZLMediaKit 进行连接。以下是一个基本的步骤和示例代码,帮助您实现这一目标。 ### 步骤 1. **安装 ZLMediaKit**:确保您已经在服务器上安装并…

[Linux] 关于路径

背景 在终端执行命令时,如何理解这件事?并且如何理解附加的一些参数? 回答 路径的概念 绝对路径与相对路径 绝对路径(Absolute Path):以根目录 / 开始,完整地描述了从根目录到目标文件或目录的层次关系。例如&am…

JavaScript 基础语法:变量、数据类型、运算符、条件语句、循环

JavaScript 是一种动态类型的脚本语言,广泛用于前端开发。以下是 JavaScript 基础语法的核心内容,包括变量、数据类型、运算符、条件语句和循环。 --- ### 1. 变量 变量用于存储数据。JavaScript 中有三种声明变量的方式: - **var**&…

轻量级服务器http-server

安装 sudo npm install http-server -g 运行 1. 直接去到要跑起来的目录,在终端输入 cd xxxx文件夹http-server //只输入http-server的话,更新了代码后,页面不会同步更新http-server -c-1 //同步更新页面http-server -a 127.0.0.1 -p 808…

Vue(7)

一.Vuex (1)概述 1.是什么 vuex是一个vue的状态管理工具,状态就是数据,可以帮助管理vue通用的数据(多组件共享的数据) 2.场景 ①某个状态在很多个组件来使用(个人信息) ②多个组…

CSS 相关知识

1、高度已知&#xff0c;三栏布局&#xff0c;左右宽度 200&#xff0c;中间自适应&#xff0c;如何实现&#xff1f; <body><div class"box"><div class"box1">高度已知</div><div class"box2">左右宽度 200&…

Redis 事务的介绍

1. Redis 事务介绍 Redis 的事务和 MySQL 的事务概念上是类似的&#xff0c;不过也有一定区别&#xff1a; 弱化的原子性&#xff1a;Redis 没有 “回滚机制”&#xff0c;只能批量执行操作&#xff0c;但如果其中一个操作失败&#xff0c;不会恢复到初始状态。不保证一致性&a…

构建高效Facebook广告矩阵:精准营销与广告投放的全新策略

随着社交媒体广告成为企业营销不可或缺的一部分&#xff0c;Facebook作为全球最大的社交平台之一&#xff0c;已成为企业营销的重要阵地。在Facebook上成功的广告投放&#xff0c;往往不只是依赖于单一广告&#xff0c;而是通过构建一个精准的广告矩阵来提升品牌曝光、增强用户…