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

json转yolo格式

json转yolo格式

视觉分割得一些标注文件是json格式,比如,舌头将这个舌头区域分割出来(用mask二值图的形式),对舌头的分割第一步是需要检测出来,缺少数据集,可以使用分割出来的结果,将分割的结果转化成可以用于目标检测的数据集。
下面是将json文件转化成一个yolov8的数据格式,首先看一下json的数据格式:

json的数据格式

我关注的就是"shapes"这个字段因为它是我舌头区域的坐标点,其次关注的是 “imageHeight”: 图片的高, “imageWidth”: 图片的宽。这些在生成yolov8格式的检测框的时候啥都有用。

{
  "version": "5.2.1",
  "flags": {},
  "shapes": [
    {
      "fill_color": null,
      "line_color": null,
      "label": "tongue",
      "points": [
        [
          700.361963190184,
          510.8926380368097
        ],
		.......
        [
          573.9815950920246,
          515.1871165644171
        ],  
      ],
      "group_id": null,
      "description": null,
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "imagePath": "0000.jpg",
  "imageData": "iVBORw0KGgoA.....................AAAAAElFTkSuQmCC",
  "imageHeight": 777,
  "imageWidth": 1286,
  "fillColor": [
    255,
    0,
    0,
    128
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ]
}

yolo数据格式

对应的yolov8的数据格式就是yolo系列的标签存储形式

yolo系列对应的是[class x y w’ h’]。注意 class也就是label标签, x y 就是(x, y)表示中心横坐标与图像宽度、高度的比值,w’ :检测框box宽度与图像宽度比值,h’:检测框高度与图像高度比值。

# 一个txt文件
0 0.507394403152401 0.5280297826310096 0.49941035598087944 0.33793653425555276
1 0.407394403152401 0.9280297826310096 0.19941035598087944 0.33793653425555276
2 0.37394403152401 0.5280297826310096 0.19941035598087944 0.13793653425555276

代码

def json_to_yolov8(data):
	# 获取原图的宽和高
    image_width = data['imageWidth']
    image_height = data['imageHeight']

    for shape in data['shapes']:
        if shape['label'] == 'tongue':
            points = shape['points']
            x_min = min(point[0] for point in points)
            x_max = max(point[0] for point in points)
            y_min = min(point[1] for point in points)
            y_max = max(point[1] for point in points)

            x_center = (x_min + x_max) / 2
            y_center = (y_min + y_max) / 2
            w = x_max - x_min
            h = y_max - y_min

            x_center /= image_width
            y_center /= image_height
            w /= image_width
            h /= image_height

            yolov8_box = [0, x_center, y_center, w, h]

    return yolov8_box

# Replace 'your_json_file.json' and 'your_image.jpg' with the actual paths
json_folder = "path/to/json"  # 输入json文件的路径位置
yolov8_labels = 'path/to/txt' # 输出的目标文件存放路径
for json_file in os.listdir(json_folder):
       if json_file.endswith('.json'):
            json_name = os.path.basename(json_file).split('.')[0]
            output_file = os.path.join(yolov8_labels, f'{json_name}.txt')
            jsonfile = os.path.join(json_folder, f'{json_name}.json')
            
            with open(jsonfile, 'r') as file:
                data = json.load(file)
                
            yolov8_box = json_to_yolov8(data)
            
            with open(output_file, 'w') as f:
                result_str = ' '.join(str(data) for data in yolov8_box)
                f.write(result_str)
print("over!")

http://www.kler.cn/news/161412.html

相关文章:

  • 大屏可视化基础学习-通用可套用模板【大屏可视化项目案例-15】
  • 每日一题:LeetCode-11.盛水最多的容器
  • Android11适配已安装应用列表
  • ZKP Understanding Nova (2) Relaxed R1CS
  • ESP32-Web-Server编程- 在 Web 上开发动态纪念册
  • 二叉树的层平均值[中等]
  • Python作业答疑_6.22~6.25
  • C语言基础概念考查备忘 - 标识符、关键字、预定义标识符、语法检查、语义检查 ... 左值、右值、对象、副作用、未定义行为、sizeof是什么等等
  • 26.Oracle11g的数据装载
  • Zabbix 6.0部署+自定义监控项+自动发现与自动注册+部署zabbix代理服务器
  • 【跨境营商】创新科技助力数码转型 增强大湾区企业核心竞争力
  • 08-中介者模式-C语言实现
  • HarmonyOS4.0系列——03、声明式UI、链式编程、事件方法、以及自定义组件简单案例
  • 如何配置WinDbg和VMware实现内核的调试
  • appium :输入框控件为android.view.View 时输入内容(如:验证码、密码输入框)
  • Docker容器中的OpenCV:轻松构建可移植的计算机视觉环境
  • Java SpringBoot Controller常见写法
  • SpringMvc集成开源流量监控、限流、熔断降级、负载保护组件Sentinel | 京东云技术团队
  • 【开源视频联动物联网平台】视频接入网关的用法
  • 关于Kotlin Coroutines你可能会犯的 7 个错误
  • JVM 运行时参数
  • Linux C语言 40-进程间通信IPC之消息队列
  • 【微服务】springboot整合quartz使用详解
  • 基于Java+Swing+Mysql图书管理系统(含实训报告)
  • Linux-进程之间的通信
  • 【UE5】使用场系统炸毁一堵墙
  • C# 使用FluentScheduler触发定时任务
  • 视频分割方法:批量剪辑高效分割视频,提取m3u8视频技巧
  • 什么是数据架构
  • uniapp 使用 flex布局 将 图片展示 循环排列两列