Пример #1
0
        private async void AnalyzeImage(string path)
        {
            //time watcher
            var watch = System.Diagnostics.Stopwatch.StartNew();


            AzureVisionAnalyze analyze = VisionClassFactory.CreateVisionClass(VisionAPIType.AzureVisionAnalyze) as AzureVisionAnalyze;

            analyze.Key = "43ff244f1b5047d49d82aac0b482d939";



            Task <AnalysisResult> resTask = analyze.UploadAndAnalyzeImage(path, AzureTagFeature.Tags, AzureTagFeature.Description, AzureTagFeature.Categories, AzureTagFeature.Color);
            AnalysisResult        res     = await resTask;

            //watch end
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            //write result
            this.richTextBox1.Text  = $"is animal:{res.IsAnimal()}\n";
            this.richTextBox1.Text += $"run with {path}\n";
            this.richTextBox1.Text += $"used {elapsedMs} ms\n";
            this.richTextBox1.Text += VisCommonClass.JsonPrettyPrint(JsonConvert.SerializeObject(res));
            MessageBox.Show($"cost {elapsedMs}ms");
        }
        public static IComputerVision CreateVisionClass(VisionAPIType type, AnalyzeOptions opt)
        {
            IComputerVision res = null;

            switch (type)
            {
            case VisionAPIType.AzureVisionAnalyze: res = new AzureVisionAnalyze(opt); break;

            case VisionAPIType.BaiduAnimalAnalyze: res = new BaiduVisionAnalyze(opt); break;

            case VisionAPIType.BaiduEasyDL: res = new BaiduEazyDL(opt); break;
            }

            return(res);
        }
Пример #3
0
        private async void AnalyzeImage(List <string> pathList)
        {
            //time watcher
            var watch = System.Diagnostics.Stopwatch.StartNew();

            AzureVisionAnalyze analyze = VisionClassFactory.CreateVisionClass(VisionAPIType.AzureVisionAnalyze) as AzureVisionAnalyze;

            analyze.Key = "43ff244f1b5047d49d82aac0b482d939";

            string dir = Path.Combine(Application.StartupPath, "result", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));

            if (!System.IO.Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string dirAnimal = Path.Combine(dir, "aniaml");

            if (!System.IO.Directory.Exists(dirAnimal))
            {
                Directory.CreateDirectory(dirAnimal);
            }
            string dirNoAnimal = Path.Combine(dir, "NoAniaml");

            if (!System.IO.Directory.Exists(dirNoAnimal))
            {
                Directory.CreateDirectory(dirNoAnimal);
            }

            int animalCount   = 0;
            int noAnimalCount = 0;
            int errCount      = 0;
            int maxThread     = 5;

            using (SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(maxThread))
            {
                List <Task> taskList = new List <Task>();
                foreach (string path in pathList)
                {
                    concurrencySemaphore.Wait();
                    Task task = Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            Task <AnalysisResult> resTask = analyze.UploadAndAnalyzeImage(path, AzureTagFeature.Tags,
                                                                                          AzureTagFeature.Description, AzureTagFeature.Categories);

                            AnalysisResult res = resTask.GetAwaiter().GetResult();
                            string fileName    = Path.GetFileName(path);
                            if (res.IsAnimal())
                            {
                                File.Copy(path, Path.Combine(dirAnimal, fileName));
                                animalCount++;
                            }
                            else
                            {
                                File.Copy(path, Path.Combine(dirNoAnimal, fileName));
                                noAnimalCount++;
                            }
                        }
                        catch (Exception ex)
                        {
                            errCount++;
                        }
                        finally
                        {
                            concurrencySemaphore.Release();
                        }
                    });
                    taskList.Add(task);
                }
                Task.WaitAll(taskList.ToArray());
            }



            //watch end
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            //write result
            this.richTextBox1.Text  = $"animal: {animalCount}\n";
            this.richTextBox1.Text += $"no Animal: {noAnimalCount}\n";
            this.richTextBox1.Text += $"error: {errCount}\n";
            this.richTextBox1.Text += $"cost: {elapsedMs} ms\n";
            MessageBox.Show($"done {elapsedMs}ms");
            System.Diagnostics.Process.Start(dir);
        }