Exemplo n.º 1
0
        public igorForm()
        {
            InitializeComponent();

            igorCore.igorCore igor = new igorCore.igorCore();
            gotFileIn  = false;
            gotFileOut = false;
            RecursiveSearchCheckbox.Checked = true;

            try
            {
                string currentDir = Directory.GetCurrentDirectory();
                igor.Werewolf(Path.Combine(currentDir, "modelDat", "modelSelect.txt"));
                cfgTextBox.Text     = Path.GetFullPath(igor.modelDetailsCfg);
                weightsTextBox.Text = Path.GetFullPath(igor.modelDetailsWeights);
                namesTextBox.Text   = Path.GetFullPath(igor.modelDetailsNames);

                cfgTextBox.SelectionStart      = cfgTextBox.Text.Length;
                cfgTextBox.SelectionLength     = 0;
                weightsTextBox.SelectionStart  = weightsTextBox.Text.Length;
                weightsTextBox.SelectionLength = 0;
                namesTextBox.SelectionStart    = namesTextBox.Text.Length;
                namesTextBox.SelectionLength   = 0;

                numberOfObjectsLabel.Text = "The selected model will code images for " + igor.theBags.Count() + " different object classes.";

                igor.WalkThisWay();

                if (igor.gpuAvailable)
                {
                    gpuLabel.BackColor = Color.LightGreen;
                    cpuLabel.BackColor = Color.DarkGray;
                }
                else
                {
                    cpuLabel.BackColor = Color.LightGreen;
                    gpuLabel.BackColor = Color.Red;
                }

                gotCfg    = true;
                gotWeight = true;
                gotNames  = true;
            }
            catch (Exception ex)
            {
                gotCfg    = false;
                gotWeight = false;
                gotNames  = false;
            }
            finally
            {
                igor.WhatHump();
            }
        }
Exemplo n.º 2
0
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Bags bags = (Bags)e.Argument;

            igorCore.igorCore igor = new igorCore.igorCore();

            try
            {
                igor.ThereWolfThereCastle(bags.GetCfg(), bags.GetWeight(), bags.GetNames());
                igor.WalkThisWay();
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem initializing your YOLO model. Please share the following information with the software developer:" + Environment.NewLine + ex.ToString(),
                                "Damn Your Eyes!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }



            var inputFiles = Directory.EnumerateFiles(bags.GetFolderIn(), searchPattern: "*.*", searchOption: bags.getDirDepth())
                             .Where(s => s.EndsWith(".jpg") || s.EndsWith(".png"));



            using (FileStream fileStream = new FileStream(bags.GetFileOut(), FileMode.Append, FileAccess.Write, FileShare.Read))
                using (StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8))
                {
                    string headerRow = "\"Filename\"";
                    for (int i = 0; i < igor.bagCount; i++)
                    {
                        headerRow += ",\"" + igor.theBags[i] + "\"";
                    }

                    streamWriter.WriteLine(headerRow);


                    foreach (string file in inputFiles)
                    {
                        if (bgworker.CancellationPending)
                        {
                            break;
                        }
                        bgworker.ReportProgress(0, file.Replace(bags.GetFolderIn(), ""));

                        Dictionary <string, int> imageObjects = new Dictionary <string, int>();
                        for (int i = 0; i < igor.bagCount; i++)
                        {
                            imageObjects.Add(igor.theBags[i], 0);
                        }

                        try
                        {
                            List <string> items = igor.Blücher(file);

                            foreach (string item in items)
                            {
                                imageObjects[item]++;
                            }

                            #region build/write output

                            StringBuilder outputRow = new StringBuilder();

                            //add the filename to the output
                            outputRow.Append("\"" + file.Replace(bags.GetFolderIn(), "").Replace("\"", "\"\"") + "\"");
                            for (int i = 0; i < igor.bagCount; i++)
                            {
                                outputRow.Append(',');
                                outputRow.Append(imageObjects[igor.theBags[i]]);
                            }

                            streamWriter.WriteLine(outputRow.ToString());
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            using (FileStream fs = new FileStream(bags.GetFileOut() + ".log", FileMode.Create, FileAccess.Write, FileShare.Read))
                                using (StreamWriter logWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8))
                                {
                                    logWriter.WriteLine("================================================");
                                    logWriter.WriteLine("Error processing " + file);
                                    logWriter.WriteLine(Environment.NewLine);
                                    logWriter.WriteLine(ex.ToString());
                                }
                        }
                    }
                }

            igor.WhatHump();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            string execpath = Directory.GetCurrentDirectory();



            //Console.WriteLine(args.Length.ToString());
            //foreach (string arg in args) Console.WriteLine(arg);


            //Console.WriteLine(args.Length.ToString());
            //Thread.Sleep(3000);

            //print the header
            PrintHeader();



            //bring an Igor() into the mix. This wakes him up,
            //tells him where the model details are, etc.


            igorCore.igorCore igor = new igorCore.igorCore();
            PrintStrongLine();
            Console.ForegroundColor = exampleColor;
            try
            {
                igor.Werewolf(Path.Combine(execpath, "modelDat", "modelSelect.txt"));
            }
            catch (Exception ex)
            {
                WriteText(ex.ToString(), errColor);
                EndProg();
            }
            Console.ForegroundColor = neutralColor;
            Thread.Sleep(1000);
            PrintStrongLine();

            int numObjectCategories = igor.theBags.Count();

            bool ableToLog = false;



            //double check that we have the right number of arguments
            #region check arguments
            if (args.Length != 2)
            {
                WriteText("Igor requires 2 arguments: the input folder and", errColor);
                WriteText("output file location. For example:", errColor);
                WriteText("  igor.exe \"C:\\imagefiles\" \"C:\\OutputData.csv\" ", exampleColor);
                Console.WriteLine();
                WriteText("In some cases, the error is caused by unintentionally ending your", errColor);
                WriteText("input folder parameter with an escape. For example:", errColor);
                WriteText("  igor.exe \"C:\\imagefiles\\\" \"C:\\OutputData.csv\" ", exampleColor);
                Console.WriteLine();
                WriteText("Note the extra \"\\\" that is is not in the original example.", errColor);
                EndProg();
            }

            try
            {
                if (!Directory.Exists(args[0]))
                {
                    WriteText("Your input folder does not appear to be valid.", errColor);
                    EndProg();
                }
            }
            catch (Exception ex)
            {
                PrintStrongLine();
                Console.WriteLine(ex.ToString(), errColor);
                PrintStrongLine();

                WriteText("Your input parameters appear to be incorrect. For example:", errColor);
                WriteText("  igor.exe \"C:\\imagefiles\" \"C:\\OutputData.csv\" ", exampleColor);
                EndProg();
            }

            #region try to initialize file
            try
            {
                // Create the file, or overwrite if the file exists.
                using (FileStream fileStream = new FileStream(args[1], FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8))
                    {
                        string headerRow = "\"Filename\"";
                        for (int i = 0; i < numObjectCategories; i++)
                        {
                            headerRow += ",\"" + igor.theBags[i] + "\"";
                        }

                        streamWriter.WriteLine(headerRow);
                    }
            }
            catch (Exception ex)
            {
                PrintStrongLine();
                Console.WriteLine(ex.ToString(), errColor);
                PrintStrongLine();
                WriteText("Your output file does not appear to be valid. Please check to ensure that", errColor);
                WriteText("the file location correct and that the file is not open in another program.", errColor);
                EndProg();
            }
            #endregion

            // now that we've vetted the arguments, we can rename them to actual
            //variables for better readability
            string inputDir   = args[0];
            string outputFile = args[1];
            string logFile    = outputFile + ".log";

            #region check to see if we can create a log
            try
            {
                // Create the file, or overwrite if the file exists.
                using (FileStream fileStream = new FileStream(logFile, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8))
                    {
                    }

                ableToLog = true;
            }
            catch (Exception ex)
            {
                //do nothing since ableToLog is false by default
            }
            #endregion



            #endregion


            //this actually instantiates the model. very important stuff.
            Console.ForegroundColor = exampleColor;
            igor.WalkThisWay();
            Thread.Sleep(1000);
            Console.ForegroundColor = neutralColor;


            var inputFiles = Directory.EnumerateFiles(inputDir, searchPattern: "*.*", searchOption: SearchOption.AllDirectories)
                             .Where(s => s.EndsWith(".jpg") || s.EndsWith(".png"));;



            using (FileStream fileStream = new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.Read))
                using (StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8))
                {
                    WriteText(" -> ...model has been loaded.");

                    foreach (string file in inputFiles)
                    {
                        Dictionary <string, int> imageObjects = new Dictionary <string, int>();
                        for (int i = 0; i < numObjectCategories; i++)
                        {
                            imageObjects.Add(igor.theBags[i], 0);
                        }

                        PrintFile(file, inputDir);

                        try
                        {
                            List <string> items = igor.Blücher(file);

                            foreach (string item in items)
                            {
                                imageObjects[item]++;
                            }

                            #region build/write output
                            //write our output
                            string headerRow = "\"Filename\"";
                            for (int i = 0; i < numObjectCategories; i++)
                            {
                                headerRow += ",\"" + igor.theBags[i] + "\"";
                            }

                            StringBuilder outputRow = new StringBuilder();

                            //add the filename to the output
                            outputRow.Append("\"" + file.Replace(inputDir, "").Replace("\"", "\"\"") + "\"");
                            for (int i = 0; i < numObjectCategories; i++)
                            {
                                outputRow.Append(',');
                                outputRow.Append(imageObjects[igor.theBags[i]]);
                            }

                            streamWriter.WriteLine(outputRow.ToString());
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            WriteText("     -> There was an error processing this file.", errColor);
                            WriteText("     -> Please see .log file for details.", errColor);

                            if (ableToLog)
                            {
                                using (FileStream fs = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read))
                                    using (StreamWriter logWriter = new StreamWriter(fs, System.Text.Encoding.UTF8))
                                    {
                                        logWriter.WriteLine(strongLine);
                                        logWriter.WriteLine("Error processing " + file);
                                        logWriter.WriteLine(Environment.NewLine);
                                        logWriter.WriteLine(ex.ToString());
                                    }
                            }
                        }
                    }
                }

            igor.WhatHump();

            EndProg();
        }