Esempio n. 1
0
        /// this method labels or unlables an image
        public void LabelImage(string label)
        {
            // case : removing tag from tagged image
            if (Images[CurrentIndex].isTagged && Images[CurrentIndex].Tag == label)
            {
                Images[CurrentIndex].isTagged = false;
                Images[CurrentIndex].Tag      = string.Empty;
                TaggedImages.Remove(Images[CurrentIndex]);
                CurrentTaggedCount = TaggedImages.Count;
                CurrentProgress    = (int)(((float)TaggedImages.Count / Images.Count) * 100);
                PerTagCount[label]--;
            }
            // case : change tag of tagged image
            else if (Images[CurrentIndex].isTagged)
            {
                PerTagCount[Images[CurrentIndex].Tag]--;
                Images[CurrentIndex].Tag = label;
                PerTagCount[label]++;
            }
            // case : tag untagged image
            else
            {
                Images[CurrentIndex].isTagged = true;
                Images[CurrentIndex].Tag      = label;
                TaggedImages.Add(Images[CurrentIndex]);
                CurrentTaggedCount = TaggedImages.Count;
                CurrentProgress    = (int)(((float)TaggedImages.Count / Images.Count) * 100);
                PerTagCount[label]++;
            }

            // wanted to call these from mainWindow but doesn't update immediately there don't know why....
            _mainWindow.CheckButtonStatus();
            _mainWindow.updateCountText();
        }
Esempio n. 2
0
        /// this method is called when we close the window and it writes the CSV and copies the tagged images in their respective folder
        public void OnWindowClosed(object sender, CancelEventArgs e)
        {
            // cancel the automatic CSV writing in the other thread
            tokenSource.Cancel();



            lock (lockObject)
            {
                // write to CSV and copy images
                using (var writer = new StreamWriter(Path.Combine(ImageLabeling.output_path, ImageLabeling.csv_name)))
                {
                    //hacking this to be able to write the header even if no image is tagged
                    string[] header = new string[ImageLabeling.classes.Length + 1];
                    header[0] = "Filepath";
                    for (int i = 0; i < ImageLabeling.classes.Length; i++)
                    {
                        header[i + 1] = ImageLabeling.classes[i];
                    }
                    writer.WriteLine(string.Join(",", header));

                    // create row for each tagged image and write it to CSV and copy image to correct folder
                    string[] row = new string[ImageLabeling.classes.Length + 1];
                    foreach (var image in TaggedImages.OrderBy(x => x.Filename))
                    {
                        row[0] = image.Filepath;
                        for (int i = 0; i < ImageLabeling.classes.Length; i++)
                        {
                            if (image.Tag == ImageLabeling.classes[i])
                            {
                                row[i + 1] = "1";
                                File.Copy(image.Filepath, Path.Combine(ImageLabeling.output_path, ImageLabeling.classes[i], image.Filename));
                            }

                            else
                            {
                                row[i + 1] = "0";
                            }
                        }
                        writer.WriteLine(string.Join(",", row));
                    }
                }
            }

            tokenSource.Dispose();
        }
Esempio n. 3
0
        /// This method is used to periodically write the CSV in another thread
        private void WriteCSV()
        {
            while (true)
            {
                // sleep for 1 minute
                Thread.Sleep(new TimeSpan(0, 1, 0));

                token.ThrowIfCancellationRequested();

                lock (lockObject)
                {
                    using (var writer = new StreamWriter(Path.Combine(ImageLabeling.output_path, ImageLabeling.csv_name)))
                    {
                        //hacking this to be able to write the header even if no image is tagged
                        string[] header = new string[ImageLabeling.classes.Length + 1];
                        header[0] = "Filepath";
                        for (int i = 0; i < ImageLabeling.classes.Length; i++)
                        {
                            header[i + 1] = ImageLabeling.classes[i];
                        }
                        writer.WriteLine(string.Join(",", header));

                        // create row for each tagged image and write it to CSV
                        string[] row = new string[ImageLabeling.classes.Length + 1];
                        foreach (var image in TaggedImages.OrderBy(x => x.Filename))
                        {
                            row[0] = image.Filepath;
                            for (int i = 0; i < ImageLabeling.classes.Length; i++)
                            {
                                if (image.Tag == ImageLabeling.classes[i])
                                {
                                    row[i + 1] = "1";
                                }
                                else
                                {
                                    row[i + 1] = "0";
                                }
                            }
                            writer.WriteLine(string.Join(",", row));
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// Main view model, which takes width and height of screen to set limits to image size
        public MainWindowViewModel(int width, int height, MainWindow window)
        {
            // get ref of main window
            _mainWindow = window;

            // get cancellation token
            token = tokenSource.Token;

            // get all the image files and fill the different lists
            foreach (var file in Directory.EnumerateFiles(ImageLabeling.input_path)
                     .Where(x => ImageLabeling.extensions.Any(ext => ext == Path.GetExtension(x).ToLower())).OrderBy(x => x))
            {
                Files.Add(file);
                FileNames.Add(Path.GetFileName(file));
                Images.Add(new ImageLabel()
                {
                    Filename = Path.GetFileName(file),
                    Filepath = file,
                    Image    = new Image()
                    {
                        Source    = new Bitmap(file),
                        MaxWidth  = width * 0.9,
                        MaxHeight = height * 0.9,
                        Stretch   = Avalonia.Media.Stretch.Uniform
                    },
                    isTagged = false,
                    Tag      = String.Empty
                });
            }


            // init the currentX properties and per tag count
            CurrentIndex    = 0;
            CurrentProgress = 0;
            CurrentFileName = FileNames[CurrentIndex];

            foreach (var clas in ImageLabeling.classes)
            {
                PerTagCount[clas] = 0;
            }

            // if resuming, read csv, get class names and tagged images
            if (ImageLabeling.isResuming)
            {
                string line;
                // we already checked that the file exists and contains a valid header in startButton handler
                using (System.IO.StreamReader file = new System.IO.StreamReader(Path.Combine(ImageLabeling.output_path, ImageLabeling.csv_name)))
                {
                    // loop through the lines to read the records if there are
                    while ((line = file.ReadLine()) != null)
                    {
                        var splits = line.Split(",");

                        // this is header, skip it
                        if (splits[0] == "Filepath")
                        {
                            continue;
                        }


                        var image = Images.Where(x => x.Filepath == splits[0]).First();
                        image.isTagged = true;
                        for (int i = 1; i < splits.Length; i++)
                        {
                            if (splits[i] == "1")
                            {
                                image.Tag = ImageLabeling.classes[i - 1];
                            }
                        }
                        TaggedImages.Add(image);
                        PerTagCount[image.Tag]++;
                    }
                }

                CurrentTaggedCount = TaggedImages.Count;

                // update the current index to be that of the first untagged image
                for (int i = 0; i < Files.Count; i++)
                {
                    if (!TaggedImages.Select(x => x.Filepath).Contains(Files[i]))
                    {
                        break;
                    }
                    else
                    {
                        CurrentIndex++;
                    }
                }
                // if all images are tagged, set index to first image
                if (CurrentIndex == Files.Count)
                {
                    CurrentIndex = 0;
                }

                CurrentFileName = FileNames[CurrentIndex];
                CurrentProgress = (int)(((float)TaggedImages.Count / Images.Count) * 100);


                // delete all the copied images
                foreach (var directory in Directory.EnumerateDirectories(ImageLabeling.output_path))
                {
                    foreach (var file in Directory.EnumerateFiles(directory))
                    {
                        File.Delete(file);
                    }
                }
            }
            // create the folders
            else
            {
                Directory.CreateDirectory(ImageLabeling.output_path);
                foreach (var clas in ImageLabeling.classes)
                {
                    Directory.CreateDirectory(Path.Combine(ImageLabeling.output_path, clas));
                }
            }

            // start new thread for automatic CSV writing every minute
            Task.Run(() => WriteCSV(), token);
        }