【问题标题】:How do i convert JSON VGG file annotations into YOLOv3 annotation format?如何将 JSON VGG 文件注释转换为 YOLOv3 注释格式?
【发布时间】:2019-07-15 02:39:37
【问题描述】:

我目前正在研究使用 YOLOv3 对象检测器进行车牌检测的深度学习模型,我在 1470 张图像上使用了 VGG Image Annotator 并将它们导出为 JSON 和 CSV 格式: VGG annotation in JSON format

VGG annotation in CSV format

如您所见,我使用了多边形和矩形,因为有些盘子的形状很尴尬,我尝试将它们转换为 YOLOv3 格式的注释,但我在这样做时遇到了麻烦。

任何帮助将不胜感激。

【问题讨论】:

  • 我遇到了类似的问题,但对于 TensorFlow 对象检测 API。首先,YOLO 不接受多边形作为输入,只有矩形,所以你必须先转换它们。其次,YOLO取对象相对于图像大小的位置,即对象xminyminxmaxymax从0到1。由于VGG Image Annotator没有在注释中提供图像尺寸,仅通过使用 JSON 中的信息来执行您要求的转换是不可能的。您可以尝试在 Python 脚本中打开图像并使用 PIL 获取它们的宽度和高度。
  • This issue 解释了我提到的问题之一。

标签: deep-learning computer-vision data-annotations yolo


【解决方案1】:

以下 Python 脚本允许您将 JSON VGG 文件注释转换为 YOLOv3 注释格式。

from PIL import Image
from os import path, makedirs
import os
import re
import pandas as pd
import sys
import argparse


def get_parent_dir(n=1):
    """returns the n-th parent dicrectory of the current
    working directory"""
    current_path = os.path.dirname(os.path.abspath(__file__))
    for k in range(n):
        current_path = os.path.dirname(current_path)
    return current_path


sys.path.append(os.path.join(get_parent_dir(1), "Utils"))
from Convert_Format import convert_vott_csv_to_yolo

Data_Folder = os.path.join(get_parent_dir(1), "Data")
VoTT_Folder = os.path.join(
    Data_Folder, "Source_Images", "Training_Images", "vott-csv-export"
)
VoTT_csv = os.path.join(VoTT_Folder, "Annotations-export.csv")
YOLO_filename = os.path.join(VoTT_Folder, "data_train.txt")

model_folder = os.path.join(Data_Folder, "Model_Weights")
classes_filename = os.path.join(model_folder, "data_classes.txt")

if __name__ == "__main__":
    # surpress any inhereted default values
    parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
    """
    Command line options
    """
    parser.add_argument(
        "--VoTT_Folder",
        type=str,
        default=VoTT_Folder,
        help="Absolute path to the exported files from the image tagging step with VoTT. Default is "
        + VoTT_Folder,
    )

    parser.add_argument(
        "--VoTT_csv",
        type=str,
        default=VoTT_csv,
        help="Absolute path to the *.csv file exported from VoTT. Default is "
        + VoTT_csv,
    )
    parser.add_argument(
        "--YOLO_filename",
        type=str,
        default=YOLO_filename,
        help="Absolute path to the file where the annotations in YOLO format should be saved. Default is "
        + YOLO_filename,
    )

    FLAGS = parser.parse_args()

    # Prepare the dataset for YOLO
    multi_df = pd.read_csv(FLAGS.VoTT_csv)
    labels = multi_df["label"].unique()
    labeldict = dict(zip(labels, range(len(labels))))
    multi_df.drop_duplicates(subset=None, keep="first", inplace=True)
    train_path = FLAGS.VoTT_Folder
    convert_vott_csv_to_yolo(
        multi_df, labeldict, path=train_path, target_name=FLAGS.YOLO_filename
    )

    # Make classes file
    file = open(classes_filename, "w")

    # Sort Dict by Values
    SortedLabelDict = sorted(labeldict.items(), key=lambda x: x[1])
    for elem in SortedLabelDict:
        file.write(elem[0] + "\n")
    file.close()

【讨论】:

    【解决方案2】:

    我知道的 Yolo v3 有以下格式的注释 [classID, x_center, y_center, w, h],除了classID是一个整数,其余四个数字都是0到1之间的实数,分别由image_height (H)image_width (W)归一化。所以要获得[x_min, y_min, x_max, y_max],需要

    1. 更正偏移量
    [x_0, y_0, x_1, y_1] = [x_center - w/2, y_center - h/2, x_center + w/2, y_center + h/2]
    
    1. 应用尺寸
    [x_min, y_min, x_max, y_max] = [x_0, y_0, x_1, y_1] \dot_product [W H W H]
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2018-10-09
    • 2022-12-25
    • 1970-01-01
    • 2023-03-08
    • 2022-01-28
    • 1970-01-01
    • 2022-07-19
    相关资源
    最近更新 更多