首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将boundingPoly格式转换为yolo格式

将boundingPoly格式转换为yolo格式
EN

Stack Overflow用户
提问于 2021-12-20 11:00:47
回答 1查看 667关注 0票数 1

Yolov5不支持分段标签,我需要将它转换成正确的格式。

您将如何将此转换为yolo格式?

代码语言:javascript
复制
        "boundingPoly": {
            "normalizedVertices": [{
                "x": 0.026169369
            }, {
                "x": 0.99525446
            }, {
                "x": 0.99525446,
                "y": 0.688811
            }, {
                "x": 0.026169369,
                "y": 0.688811
            }]
        }

yolo格式如下所示

代码语言:javascript
复制
0 0.588196 0.474138 0.823607 0.441645
<object-class> <x> <y> <width> <height>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-21 00:34:52

在我们的评论后,我有足够的信息来回答你的问题。这是来自的输出。normalizedVertices类似于YOLO格式,因为它们是“规范化”的,这意味着坐标是在0到1之间缩放的,而不是从1到n的像素。在YOLO格式中,第2列和第3列中的X和Y值指的是包围框的中心,而不是其中的一个角。

下面是一个代码片段,它将在https://ghostbin.com/hOoaz/raw上示例成以下YOLO格式的字符串'0 0.5080664305 0.5624289849999999 0.9786587390000001 0.56914843‘

代码语言:javascript
复制
#Sample annotation output 
json_annotation = """
      [
        {
          "mid": "/m/01bjv",
          "name": "Bus",
          "score": 0.9459266,
          "boundingPoly": {
            "normalizedVertices": [
              {
                "x": 0.018737061,
                "y": 0.27785477
              },
              {
                "x": 0.9973958,
                "y": 0.27785477
              },
              {
                "x": 0.9973958,
                "y": 0.8470032
              },
              {
                "x": 0.018737061,
                "y": 0.8470032
              }
            ]
          }
        }
      ]
"""

import json
json_object = json.loads(json_annotation, strict=False)

#Map all class names to class id
class_dict = {"Bus": 0}
#Get class id for this record
class_id = class_dict[json_object[0]["name"]]

#Get the max and min values from segmented polygon points 
normalizedVertices = json_object[0]["boundingPoly"]["normalizedVertices"]
max_x = max([v['x'] for v in normalizedVertices])
max_y = max([v['y'] for v in normalizedVertices])
min_x = min([v['x'] for v in normalizedVertices])
min_y = min([v['y'] for v in normalizedVertices])

width = max_x - min_x
height = max_y - min_y 
center_x = min_x + (width/2)
center_y = min_y + (height/2)

yolo_row = str(f"{class_id} {center_x} {center_y} {width} {height}")
print(yolo_row)

如果您想要训练一个YOLO模型,还需要执行几个步骤:您需要在特定的文件夹结构中设置图像和注释。但这将帮助您转换注释。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70420807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档