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

跑模型——labelme的json文件转成yolo使用的txt文件(语义分割)

前言
将labelme多边形标注的json文件转换成yolo使用的txt文件

import os
import json
import numpy as np
from tqdm import tqdm#实现函数
def json2txt(path_json, path_txt):  # 可修改生成格式with open(path_json, 'r') as path_json:jsonx = json.load(path_json)with open(path_txt, 'w+') as ftxt:width,height = jsonx['imageWidth'],jsonx['imageHeight']for shape in jsonx['shapes']:label_name = shape['label']label_index = str(classes_name.index(label_name))label = label_index + ' 'xy = np.array(shape['points'])strxy = ''for x, y in xy:x = float(x)/widthy = float(y)/heightstrxy += str(x) + ' ' + str(y) + ' 'label += strxyftxt.writelines(label + "\n")dir_json = 'D:/data_val/phone_1_dai/new/output/'  # json存储的文件目录 这里需要在最后加'/'
dir_txt = 'D:/data_val/phone_1_dai/new/txt'  # txt存储目录#如果不存在则创建txt存储目录
os.makedirs(dir_txt,exist_ok=True)
list_json = os.listdir(dir_json)#标签名
classes_name = ["_background_", 'phone']pbar = tqdm(total=len(list_json))
error_list=[]
for cnt, json_name in enumerate(list_json):try:# print('cnt=%d,name=%s' % (cnt, json_name))path_json = dir_json + json_namepath_txt = dir_txt +'/'+ json_name.replace('.json', '.txt')json2txt(path_json, path_txt)except Exception as e:# print('cnt=%d,name=%s' % (cnt, json_name))error_list.append({json_name:e})pbar.update(1)for error in error_list:print(error)
print(f"error length={len(error_list)}")

如果有什么不懂的,可以在评论区底下评论哦,我会努力解答的

欢迎大家点赞或收藏,点赞或收藏可以鼓励作者更新哟~

http://www.lryc.cn/news/335577.html

相关文章:

  • 一个项目仿京东商场代码
  • 计算机网络——WEB服务器编程实验
  • 蓝桥杯算法题:最大比例
  • 【堡垒机】堡垒机的介绍
  • 通过 ffmpeg命令行 调节视频播放速度
  • SQLite数据库在Linux系统上的使用
  • Spring中依赖注入的方法有几种,分别是什么?
  • 【面试精讲】MyBatis设计模式及源码分析,MyBatis设计模式实现原理
  • Acrel-1000DP光伏监控系统在尚雷仕(湖北)健康科技有限公司5.98MW分布式光伏10KV并网系统的应用
  • 电脑远程控制esp32上的LED
  • ARXML处理 - C#的解析代码(一)
  • OJ 栓奶牛【C】【Python】【二分算法】
  • Spring6-单元测试:JUnit
  • ubuntu系统安装k8s1.28精简步骤
  • 探讨Java和Go语言的缺点
  • 短剧在线搜索PHP网站源码
  • Python map遍历
  • 数据结构—红黑树
  • MES实施之工控机和电脑的选择
  • 京东云服务器4核8G主机租用价格418元一年,1899元3年
  • 【多模态融合】MetaBEV 解决传感器故障 3D检测、BEV分割任务
  • [通俗易懂]《动手学强化学习》学习笔记1-第1章 初探强化学习
  • centOS如何升级python
  • 【MYSQL锁】透彻地理解MYSQL锁
  • 【静态分析】静态分析笔记01 - Introduction
  • 使用的sql
  • 【ZZULIOJ】1052: 数列求和4(Java)
  • 【Linux】tcpdump P3 - 过滤和组织返回信息
  • vscode免费登录ssh ,linux git配置免密码
  • Netty 心跳(heartbeat)——服务源码剖析(上)(四十一)