microsoft / VoTT

Visual Object Tagging Tool: An electron app for building end to end Object Detection Models from Images and Videos.
MIT License
4.31k stars 838 forks source link

Yolo Export Support #1034

Open NHodgesVFX opened 3 years ago

NHodgesVFX commented 3 years ago

Is your feature request related to a problem? Please describe. I want to use vott to label images for a yolov5 model. Instead of having to convert in an external program or service it would be nice if VOTT could export to the yolo format directly

Describe the solution you'd like the ability to export to the yolo format.

Describe alternatives you've considered python scripting or roboflow, python is fine but just an extra step, roboflow is paid and a extra step.

tovi911 commented 3 years ago

Make your own Python script to convert the CSV output to whatever you want.

phaabe commented 3 years ago

Checkout https://roboflow.com/formats/yolo-darknet-txt

pullmyleg commented 3 years ago

Here is a pyhton script to convert from VoTT CSV to Yolo label txt files.

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()
jeanbmar commented 3 years ago

Here is an NPM module I've written to do the conversion: https://www.npmjs.com/package/vott2yolo
One-liner : vott2yolo /path/to/labeled-files/**/*.json /path/to/project.vott