COCO json注释到YOLO txt格式

问题描述 投票:0回答:5

如何将单个COCO JSON注释文件转换为YOLO darknet格式?像下面这样 每个单独的图像都有单独的 filename.txt 文件

tensorflow computer-vision object-detection yolo coco
5个回答
8
投票

我构建了一个工具 https://github.com/tw-yshuang/coco2yolo

下载此存储库并使用以下命令:

python3 coco2yolo.py [OPTIONS]

coc2yolo

Usage: coco2yolo.py [OPTIONS] [CAT_INFOS]...

Options:
  -ann-path, --annotations-path TEXT
                                  JSON file. Path for label.  [required]
  -img-dir, --image-download-dir TEXT
                                  The directory of the image data place.
  -task-dir, --task-categories-dir TEXT
                                  Build a directory that follows the task-required categories.
  -cat-t, --category-type TEXT    Category input type. (interactive | file)  [default: interactive]
  -set, --set-computing-type TEXT
                                  Set Computing for the data. (union | intersection)  [default: union]
  --help                          Show this message and exit.

6
投票

我和我的同学创建了一个名为 PyLabel 的 python 包来帮助其他人完成此任务和其他标记任务。

我们的软件包可以进行这种转换!您可以在此笔记本中查看示例https://github.com/pylabel-project/samples/blob/main/coco2yolov5.ipynb

你的答案应该就在那里!但是您应该能够通过执行以下操作来完成此转换:

!pip install pylabel
from pylabel import importer
dataset = importer.ImportCoco(path=path_to_annotations, path_to_images=path_to_images)
dataset.export.ExportToYoloV5(dataset)

您可以在这里找到幕后使用的源代码https://github.com/pylabel-project/


0
投票

有一个名为 makesense.ai 的开源工具用于注释图像。为图像添加注释后,您可以下载 YOLO txt 格式。但您将无法下载带注释的图像。


0
投票

这对我有用

我已加载images/all_images目录中的所有图像。我对 train/test/val 使用了不同的 coco .json 注释。您可以进行相应的更改。确保相应地创建目录。

import os
import json
import shutil

# load json and save directory for labels train/val/test
coco_file = 'labels/val.json'
save_folder = 'labels/val'



#source of all the images and destination folder for train/test/val
source_path = "images/all_images"
destination_path = "images/val"


# Use os.listdir() to get a list of filenames in the folder
file_names = os.listdir(source_path)

with open(coco_file) as f:
    coco = json.load(f)

images = coco['images']
annotations = coco['annotations'] 
categories = {cat['id']: cat['name'] for cat in coco['categories']}

#os.makedirs(save_folder, exist_ok=True)

for ann in annotations:
    image = next(img for img in images if (img['id'] == ann['image_id']))
    if (image["file_name"] not in file_names):
        continue
    #print(f"image in annotations =   {type(image['id'])}")
    width, height = image['width'], image['height']
    x_center = (ann['bbox'][0] + ann['bbox'][2] / 2) / width
    y_center = (ann['bbox'][1] + ann['bbox'][3] / 2) / height
    bbox_width = ann['bbox'][2] / width
    bbox_height = ann['bbox'][3] / height
    category_id = ann['category_id']
    image_id = ann['image_id']

    filename = image['file_name']
    label_filename = filename.split('.jpg')[0]
    label_path = os.path.join(save_folder, f'{label_filename}.txt')
    with open(label_path, 'a') as f:

        segmentation_points_list = []
        for segmentation in ann['segmentation']:
            # Check if any element in segmentation is a string
            if any(isinstance(point, str) for point in segmentation):
                continue  # Skip this segmentation if it contains strings

            segmentation_points = [str(float(point) / width) for point in segmentation]
            segmentation_points_list.append(' '.join(segmentation_points))
            segmentation_points_string = ' '.join(segmentation_points_list)
            line = '{} {}\n'.format(categories, segmentation_points_string)
            f.write(line)
            segmentation_points_list.clear()
    image_source = source_path + f'/{image["file_name"]}'
    shutil.copy(image_source, destination_path)


-4
投票

共有三种方式。

  1. 使用 roboflow https://roboflow.com/formats(您也可以找到其他解决方案)

您可以找到一些 roboflow 的使用指南。例如 https://medium.com/red-buffer/roboflow-d4e8c4b52515

  1. 搜索“convert coco format to yolo format” -> 你会找到一些将注释转换为yolo格式的开源代码。

  2. 编写自己的代码将coco格式转换为yolo格式

© www.soinside.com 2019 - 2024. All rights reserved.