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

关键点检测数据格式转换(.JSON转TXT)

一、 JSON格式转Txt

import json
import osjson_path = r'C:\Users\PC\Desktop\2000张标注完-json文件\Json'  # json文件路径
txt_path = r'C:\Users\PC\Desktop\2000张标注完-json文件\Txt'  # txt文本文件路径
# 关键点数组(这里我写的是人体的7个关键点,大家可以改成自己标注顺序的关键点数组)
pointsArr = ["yading", "jiejian1", "jiejian2", "caizhai", "jiejian3","yezhongxin","jiejian4",]# 坐标归一化,返回中心点坐标和宽高
def coordinates2yolo(xmin, ymin, xmax, ymax, img_w, img_h):x = abs(xmin + xmax) / (2.0 * img_w)y = abs(ymin + ymax) / (2.0 * img_h)w = abs(xmax - xmin) / (1.0 * img_w)h = abs(ymax - ymin) / (1.0 * img_h)return x, y, w, hdef writeJson(rootpath, rootpath1, filename):path = os.path.join(rootpath, filename + '.json')count = 0  # 记录一张图片中人数的多少index = 0  # data索引,用于区分json文件中的label值with open(path) as f:# 读取json格式文件并获取相应信息data = json.load(f)imageHeight = data['imageHeight']imageWidth = data['imageWidth']data = data['shapes']length = len(data)# print('length', length)# 遍历json文件,用变量count记录 data[i]['label']=’类别名‘ 的次数,以此说明图片中有几个人for i in range(0, length):# 类别名换成自己的类别,当有多个类别时,用关键字or进行连接# if data[i]['label'] == '类别名1' or data[i]['label'] == '类别名2'。。。if data[i]['label'] == 'nenshao':count += 1# 将json文件信息写入txt文本文件中file = open(os.path.join(rootpath1, filename + '.txt'), mode='w')for j in range(0, count):# 在txt文本文件中写入类别id、目标框中心坐标以及图片宽高file.write(str(data[index]['group_id']))file.write(" ")points = data[index]['points']xmin = points[0][0]ymin = points[0][1]xmax = points[1][0]ymax = points[1][1]x, y, w, h = coordinates2yolo(xmin, ymin, xmax, ymax, imageWidth, imageHeight)file.write(str(round(x, 6)))file.write(" ")file.write(str(round(y, 6)))file.write(" ")file.write(str(round(w, 6)))file.write(" ")file.write(str(round(h, 6)))file.write(" ")index += 1# 在txt文本文件中写入关键点坐标与对应id值for point in pointsArr:# print(index)if index < length:if data[index]['label'] == point:point = data[index]['points']  # 获取关键点的坐标值file.write(str(round(point[0][0] / imageWidth, 6)))file.write(" ")file.write(str(round(point[0][1] / imageHeight, 6)))file.write(" ")# data[index]['group_id'] == 1,表名为被遮挡的关键点,在txt文档中写入1if data[index]['group_id'] == 1:file.write('1.000000')file.write(" ")# data[index]['group_id'] != 1,表名为正常标记的关键点,在txt文档中写入2else:file.write('2.000000')file.write(" ")index += 1# 若data[index]['label'] != point,则写入(0, 0, 0),前两个代表坐标,最后一个‘0’代表此关键点未被标记else:file.write('0.000000')file.write(" ")file.write('0.000000')file.write(" ")file.write('0.000000')file.write(" ")else:file.write('0.000000')file.write(" ")file.write('0.000000')file.write(" ")file.write('0.000000')file.write(" ")file.write('\n')# 读取path路径中的文件
filenames = os.listdir(json_path)
for item in filenames:# 以'.'为标志分割获取文件名filename = item.split('.')[0]print(filename)writeJson(json_path, txt_path, filename)

二、本人数据标注格式

使用软件为labelme

框为nenshao(ID:0)

第一个点为yading

第二个点为jiejian1

第三个点为jiejian2

第四个点为caizhai

第五个点为jiejian3

第六个点为yezhongxin

第七个点为jiejian4

在图像内且被遮挡住的点(ID:1)

示例:


文章转载自:
http://benignity.sxnf.com.cn
http://capillarity.sxnf.com.cn
http://antihemophilic.sxnf.com.cn
http://bateleur.sxnf.com.cn
http://azeotropism.sxnf.com.cn
http://buddhahood.sxnf.com.cn
http://beerengine.sxnf.com.cn
http://antiscriptural.sxnf.com.cn
http://asphyxiation.sxnf.com.cn
http://calcareousness.sxnf.com.cn
http://alway.sxnf.com.cn
http://barrelage.sxnf.com.cn
http://acari.sxnf.com.cn
http://chemiculture.sxnf.com.cn
http://brushfire.sxnf.com.cn
http://banteringly.sxnf.com.cn
http://accusation.sxnf.com.cn
http://cerography.sxnf.com.cn
http://affiant.sxnf.com.cn
http://androstane.sxnf.com.cn
http://administration.sxnf.com.cn
http://canopied.sxnf.com.cn
http://angeleno.sxnf.com.cn
http://chereme.sxnf.com.cn
http://chiropractic.sxnf.com.cn
http://celtuce.sxnf.com.cn
http://anglo.sxnf.com.cn
http://alexander.sxnf.com.cn
http://cgmp.sxnf.com.cn
http://arala.sxnf.com.cn
http://www.dtcms.com/a/280819.html

相关文章:

  • 【超分论文精读】——LightBSR(ICCV2025)
  • 梳理Bean的创建流程
  • mongoDB的CRUD
  • Visual Studio 现已支持新的、更简洁的解决方案文件(slnx)格式
  • 云服务器如何管理数据库(MySQL/MongoDB)?
  • 基于STM32G431无刷电机驱动FOC软硬件学习
  • iOS高级开发工程师面试——常见第三方框架架构设计
  • C++学习笔记五
  • Gemma-3n-E4B-it本地部署教程:谷歌开源轻量级多模态大模型,碾压 17B 级同类模型!
  • SHAP 值的数值尺度
  • Conda 核心命令快速查阅表
  • 技术演进中的开发沉思-35 MFC系列:消息映射与命令
  • Keepalived双机热备
  • 网络安全职业指南:探索网络安全领域的各种角色
  • 003大模型基础知识
  • React 实现老虎机滚动动画效果实例
  • AutojsPro 9.3.11 简单hook
  • Pixel Reasoner:通过好奇心驱动的强化学习激励像素空间推理
  • 简单2步配置CadenceSkill开发编辑器,支持关键字高亮
  • [AI-video] Web UI | Streamlit(py to web) | 应用配置config.toml
  • (李宏毅)deep learning(五)--learning rate
  • 从底层技术到产业落地:优秘企业智脑的 AI 革命路径解析
  • NAT的核心原理以及配置
  • CCF-GESP 等级考试 2025年6月认证Python四级真题解析
  • RDMA over RoCE V2设计2:系统框架设计考虑
  • Datawhale AI夏令营 机器学习2.1
  • 详解低速容错CAN(附与高速CAN对比表)
  • RabbitMQ第三章(企业级MQ应用方案)
  • 基于uniapp+vue3封装的一个日期选择组件
  • 【图像处理基石】什么是解析力?