当前位置: 首页 > article >正文

python_json转yolo文件

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

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.kler.cn/a/538517.html

相关文章:

  • 35~37.ppt
  • CMOS 图像传感器市场趋势和新兴应用
  • qt部分核心机制
  • Linux系统命令无法使用(glib库相关问题)
  • (六)C++的函数模板与类模板
  • ZZNUOJ(C/C++)基础练习1081——1090(详解版)
  • 伪分布式Spark3.4.4安装
  • webview_flutter的使用
  • Vite 代理下的 POST 请求跨域问题排查与解决方案
  • 搭建linux qt5.6环境
  • RabbitMQ 从入门到精通:从工作模式到集群部署实战(五)
  • vue封装组件进阶
  • Visual Basic语言的区块链
  • Unity做2D小游戏2------创建地形和背景
  • Spring Boot的常用注解
  • 光伏-报告显示,假期内,硅料端签单顺序发货相对稳定。若3月份下游存提产,则不排除硅料价格有上调预期。
  • Java怎么记录网络安全情报 java转网络安全
  • 微服务日志查询难解决方案-EFK
  • 增加工作台菜单页面,AI问答应用支持上下文设置,数据库表索引优化,zyplayer-doc 2.4.8 发布啦!
  • deepseek+kimi自动生成ppt
  • 【信奥赛CSP-J 需要掌握的STL知识有哪些?】
  • 【HarmonyOS NEXT】systemDateTime 时间戳转换为时间格式 Date,DateTimeFormat
  • C# ASP.NET程序与 Web services 编程介绍
  • Python实现机器学习小项目教程案例
  • Ollama 部署 DeepSeek-R1 及Open-WebUI
  • 响应式编程库Reactor(一)