Exemple #1
0
        static void Main(string[] args)
        {
            var options     = new InferOptions();
            var showOptions = new ShowOptions();

            Parser.Default.ParseArguments <InferOptions, ShowOptions>(args)
            .WithParsed <InferOptions>(Infer)
            .WithParsed <ShowOptions>(ShowPlugins);
        }
Exemple #2
0
        static void Infer(InferOptions options)
        {
            if (!Directory.Exists(options.InputDir))
            {
                Console.WriteLine("Invalid input directory {0}", options.InputDir);
                return;
            }

            if (!Directory.Exists(options.OutputDir))
            {
                Console.WriteLine("Invalid output directory {0}", options.OutputDir);
                return;
            }
            var pluginsDir = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "plugins");
            var pm         = new PluginManager(pluginsDir);
            IObjectDetectionPlugin plugin;

            try
            {
                plugin = pm.FindPlugins()[options.PluginName];
            }
            catch
            {
                Console.WriteLine("Unable to load plugin with index {0}. Use `show` to show available plugins.", options.PluginName);
                return;
            }

            Console.WriteLine("Plugin info:");
            Console.WriteLine("\tName: {0}", plugin.Name);
            Console.WriteLine("\tAuthor: {0}", plugin.Author);
            Console.WriteLine("\tDescription: {0}", plugin.Description);
            Console.WriteLine("\tUrl: {0}", plugin.Url);
            Console.WriteLine("\tVersion: {0}", plugin.Version.ToString());
            Console.WriteLine("\tInference Type: {0}", plugin.InferenceType);
            var operatingSystems = "";

            foreach (var os in plugin.OperatingSystems)
            {
                operatingSystems += os + " ";
            }
            Console.WriteLine("\tSupported Platforms: {0}", operatingSystems);
            var inputDir  = options.InputDir;
            var outputDir = options.OutputDir;

            string[] extensions = { ".jpg", ".jpeg", ".png", ".bmp" };
            using (var model = plugin.LoadModel(options.Threshold))
            {
                foreach (var imagePath in Directory.GetFiles(inputDir).Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray())
                {
                    var metadata = ImageMetadataReader.ReadMetadata(imagePath);
                    var height   = 0;
                    var width    = 0;
                    foreach (var dir in metadata)
                    {
                        foreach (var tag in dir.Tags)
                        {
                            if (tag.Name == "Image Height")
                            {
                                if (tag.Description != null)
                                {
                                    height = int.Parse(tag.Description.Split(' ').First());
                                }
                            }
                            if (tag.Name == "Image Width")
                            {
                                if (tag.Description != null)
                                {
                                    width = int.Parse(tag.Description.Split(' ').First());
                                }
                            }
                        }
                        if (height > 0 && width > 0)
                        {
                            break;
                        }
                    }
                    Console.WriteLine("Process image {0} [{1},{2},3]", imagePath, width, height);
                    var startTime   = DateTime.Now;
                    var predictions = model.Infer(imagePath, width, height).ToList();
                    Console.WriteLine("Find {0} objects at {1} s", predictions.Count(), DateTime.Now - startTime);
                    foreach (var prediction in predictions)
                    {
                        Console.WriteLine("{0}: [{1}, {2}, {3}, {4}] @ {5}",
                                          prediction.Label,
                                          prediction.XMin,
                                          prediction.YMin,
                                          prediction.XMax,
                                          prediction.YMax,
                                          prediction.Score);
                    }
                    var imageBaseName = Path.GetFileName(imagePath);
                    var outImagePath  = Path.Join(outputDir, imageBaseName);
                    var outXmlPath    = outImagePath + ".xml";
                    var annotation    = DetectionsToAnnotation(predictions, width, height, outImagePath);
                    File.Copy(imagePath, outImagePath, true);
                    annotation.SaveToXml(outXmlPath);
                }
            }
        }