Deep learning 如何将JSON VGG文件注释转换为YOLOv3注释格式?
我目前正在使用YOLOv3 object detector开发用于平板检测的深度学习模型,我在1470张图像上使用VGG图像注释器,并以JSON和CSV格式导出它们: 正如你所看到的,我使用多边形和矩形,因为有些板的形状不好,我尝试将它们转换成YOLOv3格式的注释,但我在这样做时遇到了困难Deep learning 如何将JSON VGG文件注释转换为YOLOv3注释格式?,deep-learning,computer-vision,data-annotations,yolo,Deep Learning,Computer Vision,Data Annotations,Yolo,我目前正在使用YOLOv3 object detector开发用于平板检测的深度学习模型,我在1470张图像上使用VGG图像注释器,并以JSON和CSV格式导出它们: 正如你所看到的,我使用多边形和矩形,因为有些板的形状不好,我尝试将它们转换成YOLOv3格式的注释,但我在这样做时遇到了困难 任何帮助都将不胜感激。我所知道的Yolo v3有以下格式的注释 [classID,x\u center,y\u center,w,h],除了classID是一个整数外,其余四个数字都是0到1之间的实数,
任何帮助都将不胜感激。我所知道的Yolo v3有以下格式的注释
[classID,x\u center,y\u center,w,h]
,除了classID是一个整数外,其余四个数字都是0到1之间的实数,分别由图像高度(h)
和图像宽度(w)
标准化。因此,要获得[x_min,y_min,x_max,y_max]
,需要
下面的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()
我有一个类似的问题,但是对于TensorFlow对象检测API。首先,YOLO不接受多边形作为输入,只接受矩形,所以您必须首先转换这些多边形。其次,YOLO获取相对于图像大小的对象位置,即对象
xmin
,ymin
,xmax
和ymax
从0变为1。由于VGG图像注释器没有在注释中提供图像维度,因此不可能仅通过使用JSON中的信息来执行所要求的转换。您可以尝试在Python脚本中打开图像,并使用PIL获取其宽度和高度。解释了我提到的一个问题。欢迎使用SO!你的回答不是很清楚。请编辑一下,谢谢茉莉!当您在上找到main.py中的convert()和deconvert()函数时,一切都会很清楚
[x_min, y_min, x_max, y_max] = [x_0, y_0, x_1, y_1] \dot_product [W H W H]
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()