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

Can vott read projects from files that has not been originally exported from it? #1055

Open AhmedHisham1 opened 3 years ago

AhmedHisham1 commented 3 years ago

I have a dataset that has been annotated via multiple different annotation software, is it possible to write a script that converts them all into a vott supported format and read them as a new project? so that I can edit and re-annotate them inside vott?

IanPNewson commented 3 years ago

Yes, I've done it. I wrote a model in C# which can be used for this. Let me know if that would be any help and I'll look into uploading it to github.

AhmedHisham1 commented 3 years ago

Yes, that would be a great help. Thank you.

IanPNewson commented 3 years ago

Here you go:

https://github.com/IanPNewson/VottModel

It was just for my personal use so there's no documentation, and it only supports images, not video.

Here's some sample code that uses it:

    static async Task Main(string[] args)
    {

        var inDir = new DirectoryInfo(@"C:\Users\Ian\Documents\Projects\Output\StfcBot\StfcScreencopyOverlay");
        var dirPrefix = DateTime.Now.Ticks;

        {
            var outDir = inDir.Subdir($"{dirPrefix}.Predict_{predictThreshold}");

            var project = JsonConvert.DeserializeObject<Project>(File.ReadAllText(@"path to existing project"));
            project.name = outDir.Name;

            project.assets.Clear();
            project.sourceConnection.name = outDir.Name;
            project.targetConnection.name = outDir.Name;

            project.sourceConnection.providerOptions.folderPath =
                project.targetConnection.providerOptions.folderPath =
                    outDir.FullName;

            project.securityToken = null;
            project.useSecurityToken = false;

            var projectFile = outDir.File(outDir.Name + ".vott");

            var tasks = new List<Task>();

            foreach (var file in inDir.GetFiles("*.png"))
            {
                tasks.Add(Task.Run(() =>
                {
                    try
                    {
                        var assetImageFile = outDir.File(file.Name);
                        var assetFile = new AssetFile()
                        {
                            asset = new Asset()
                            {
                                name = file.Name,
                                format = "png",
                                path = "file:" + assetImageFile.FullName.Replace("\\", "/")
                            }
                        };

                        file.CopyTo(outDir.File(file.Name).FullName);

                        lock (project)
                            project.assets.Add(assetFile.asset.id, assetFile.asset);

                        assetFile.asset.size = new VottModel.Size
                        {
                            height = bmp.Height,
                            width = bmp.Width
                        };

                        swAssetFileGeneration.Start();
                        foreach (var result in json.Predictions)
                        {
                            var @class = result.Class;
                            var score = result.Score;
                            var rois = result.Bounds;

                            var rect = rois.ToRect(bmp.Size);

                            //g.DrawRectangle(Pens.Red, rect);
                            //g.DrawString(@class, new Font("Arial", 12), Brushes.Red, rect.Location);

                            assetFile.regions.Add(new Region
                            {
                                id = Guid.NewGuid().ToString().Substring(0, 8),
                                tags = new string[] { @class },
                                points = new VottModel.Point[]
                                {
                                new Point() { x = rect.Left, y = rect.Top },
                                new Point() { x = rect.Right, y = rect.Top },
                                new Point() { x = rect.Right, y = rect.Bottom },
                                new Point() { x = rect.Left, y = rect.Bottom }
                                },
                                boundingBox = new BoundingBox
                                {
                                    left = rect.Left,
                                    top = rect.Top,
                                    width = rect.Right - rect.Left,
                                    height = rect.Bottom - rect.Top
                                }
                            });
                        }

                        var assetFileFile = outDir.File($"{assetFile.asset.id}-asset.json");
                        File.WriteAllText(assetFileFile.FullName, JsonConvert.SerializeObject(assetFile, Formatting.Indented));
                        swAssetFileSave.Stop();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    swAssetFileGeneration.Stop();
                    ++count;
                    if (count % 10 == 0)
                    {
                        SaveProjectFile(project, projectFile);
                        Console.WriteLine($"{count} processed in {sw.ElapsedMilliseconds} ms, http: {swHttp.ElapsedMilliseconds / count}, json: {swJson.ElapsedMilliseconds / count}, gen: {swAssetFileGeneration.ElapsedMilliseconds / count}, filesave: {swAssetFileSave.ElapsedMilliseconds / count}");
                        sw.Restart();
                    }
                }));
            }
            await Task.WhenAll(tasks);

            SaveProjectFile(project, projectFile);
            Console.WriteLine($"Finished {outDir.Name}");
        }

        Console.WriteLine("All done creating vott projects");

        static void SaveProjectFile(Project project, FileInfo projectFile)
        {
            lock (project)
                File.WriteAllText(projectFile.FullName, JsonConvert.SerializeObject(project, Formatting.Indented));
        }
    }
bdockbockd commented 3 years ago

@AhmedHisham1 Did you solve it? Can you give me bit instruction how to achieve it?

AhmedHisham1 commented 3 years ago

Sorry for late reply. No I actually switched to CVAT by intel instead.