コード例 #1
0
        private void PredictImageDetection()
        {
            treeListDetection.Clear();
            //var attachmentStream = await httpClient.GetStreamAsync(imageUrl);

            MemoryStream testImage = GetStream(pictureBox1.BackgroundImage);

            //MemoryStream testImage = new MemoryStream(File.ReadAllBytes("test.jpg"));

            try
            {
                var result = endpoint.PredictImage(project.Id, testImage);
                // Loop over each prediction and write out the results
                foreach (var c in result.Predictions)
                {
                    treeListDetection.Add(new TreeData(c.TagName, c.Probability, new RectangleBox(c.BoundingBox.Left, c.BoundingBox.Top, c.BoundingBox.Width, c.BoundingBox.Height)));
                }
            }
            catch (Microsoft.Rest.HttpOperationException exception)
            {
                labelInfo.Text = exception.ToString();
            }
            if (treeListDetection.Count > 1)
            {
                treeListDetection.Sort((a, b) => b.Probability.CompareTo(a.Probability));
            }
            if (treeListDetection.Count > 0)
            {
                pictureBox1.Invalidate();
            }
        }
コード例 #2
0
        static void EfetuarTesteImagem(string caminho, bool eURL)
        {
            string chaveAPI     = "0c91a890892d40a0ba9f9e22a5e358de";
            string chaveProjeto = "4a1a0670-7689-4d01-ac59-fc56227cc3ed";

            // Crio o client do Custom View Service.
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = chaveAPI
            };
            ImagePrediction resultado = null;

            // Verifico se o projeto é uma URL ou um arquivo local para usar o método correto.
            if (eURL)
            {
                resultado = endpoint.PredictImageUrl(new Guid(chaveProjeto), new ImageUrl(caminho));
            }
            else
            {
                resultado = endpoint.PredictImage(new Guid(chaveProjeto), File.OpenRead(caminho));
            }

            // Percorro as analises.
            foreach (var possibilidade in resultado.Predictions)
            {
                Console.WriteLine($"Tag: {possibilidade.TagName}");
                Console.WriteLine($"Possibilidade: {possibilidade.Probability:P1}");
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: fcatae/haranohara
        static void Main(string[] args)
        {
            var predictionKey = "insert your prediction key here";


            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);

            PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials);



            // Make a prediction against the new project

            Console.WriteLine("Making a prediction:");

            var testImage = new MemoryStream(File.ReadAllBytes(@"insert the picture you want to compare here"));


            var projectId = new Guid("insert the project guid here");

            var result = endpoint.PredictImage(projectId, testImage, null, null);



            // Loop over each prediction and write out the results

            foreach (var c in result.Predictions)

            {
                Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}");
            }

            Console.ReadKey();
        }
コード例 #4
0
        private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

            _customVisionPredictionKey = umbracoHelper.TypedContent(1127).GetPropertyValue <string>("customVisionPredictionKey");
            _customVisionApiProjectId  = umbracoHelper.TypedContent(1127).GetPropertyValue <string>("customVisionProjectId");


            if (!string.IsNullOrWhiteSpace(_customVisionPredictionKey) && !string.IsNullOrWhiteSpace(_customVisionApiProjectId))
            {
                PredictionEndpoint endpoint = new PredictionEndpoint()
                {
                    ApiKey = _customVisionPredictionKey
                };

                foreach (IMedia media in e.SavedEntities.Where(a => a.ContentType.Name.Equals(Constants.Conventions.MediaTypes.Image)))
                {
                    string relativeImagePath = ImagePathHelper.GetImageFilePath(media);

                    using (Stream imageFileStream = _fs.OpenFile(relativeImagePath))
                    {
                        var result = endpoint.PredictImage(Guid.Parse(_customVisionApiProjectId), imageFileStream);
                        IEnumerable <string> tags = result.Predictions.Where(a => a.Probability > 0.75).Select(a => a.Tag);
                        media.SetTags("customVisionTags", tags, true);
                    }
                }
            }
        }
コード例 #5
0
        private string connectToAnalyze()
        {
            // custom vision
            TrainingApi trainingAPI = new TrainingApi()
            {
                ApiKey = "1360135bcfaa45d5b6d69dc06dcb04e4"
            };
            Guid _projectkey = new Guid("ea8591bc-bff1-47d6-b46d-64938d2d8b58");

            project = trainingAPI.GetProject(_projectkey);

            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = "65125a9d5a774acaaeaa0f9705404307"
            };
            // 28bf5d1c-1025-4d3f-a7d7-4dfd4b65f4a1   iteration key


            ArrayList lists = new ArrayList();

            byte[] imgArr = GetFileAsByteArray(App._file.AbsolutePath);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(imgArr);
            var result = endpoint.PredictImage(project.Id, stream, new Guid("28bf5d1c-1025-4d3f-a7d7-4dfd4b65f4a1"));

            foreach (var prediction in result.Predictions)
            {
                lists.Add($"{prediction.Tag}");
            }
            return(lists.Get(0).ToString());
        }
コード例 #6
0
        // GET: RecognitionModels
        public async Task <ActionResult> Index([Bind(Include = "Id,Key,ImageUpload")] RecognitionModel recognitionModel)
        {
            ViewBag.Message = "Result will show here.";
            Trace.WriteLine("Starting Prediction...");

            if (recognitionModel.ImageUpload != null)
            {
                // Create image.
                Guid projectid = new Guid("57471653-6e79-455f-b874-ee00d1014c37");

                // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key

                PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(recognitionModel.Key);
                PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

                if (recognitionModel.ImageUpload != null && recognitionModel.ImageUpload.ContentLength > 0)
                {
                    //var uploadDir = "~/uploads";
                    //var imagePath = Path.Combine(Server.MapPath(uploadDir), recognitionModel.ImageUpload.FileName);
                    //var imageUrl = Path.Combine(uploadDir, recognitionModel.ImageUpload.FileName);
                    //Trace.WriteLine("ImageUrl:" + imageUrl);
                    //Trace.WriteLine("Image path:" + imagePath);
                    //recognitionModel.ImageUpload.SaveAs(imagePath);

                    Trace.WriteLine("createing memory stream");
                    //MemoryStream memStream = new MemoryStream(System.IO.File.ReadAllBytes(imagePath));

                    MemoryStream memStream = new MemoryStream();
                    recognitionModel.ImageUpload.InputStream.Position = 0;
                    recognitionModel.ImageUpload.InputStream.CopyTo(memStream);
                    memStream.Position = 0;

                    // Make a prediction against the new project
                    Trace.WriteLine("Making a prediction:");
                    try
                    {
                        var result = endpoint.PredictImage(projectid, memStream);

                        // Loop over each prediction and write out the results
                        var predicition = result.Predictions.FirstOrDefault(P => P.Probability >= 0.8);
                        if (predicition == null)
                        {
                            ViewBag.Message = "Could not find a good match for the uploaded image. Please try again.";
                        }
                        else
                        {
                            ViewBag.Message = "Your image is of type " + predicition.Tag + " with the probability of " + predicition.Probability;
                        }
                    }
                    catch (Exception e)
                    {
                        ViewBag.Message = "Could not make a predicition of image " + recognitionModel.ImageUpload.FileName + "! Error message: " + e.Message;
                    }
                }
            }
            return(View(recognitionModel));
        }
コード例 #7
0
 private ImageTagPredictionModel GetBestTag(MediaFile file)
 {
     using (var stream = file.GetStream())
     {
         return(_endpoint.PredictImage(ApiKeys.ProjectId, stream)
                .Predictions
                .OrderByDescending(p => p.Probability)
                .FirstOrDefault(p => p.Probability > ProbabilityThreshold));
     }
 }
コード例 #8
0
        public List <ImageTagPrediction> GetListOfPredictions(Bitmap image)
        {
            MemoryStream memoryStream = new MemoryStream();

            image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            //We need to seek to begin
            memoryStream.Seek(0, SeekOrigin.Begin);
            var result = endpoint.PredictImage(projectId, memoryStream, iterationId);

            return(result.Predictions.ToList());
        }
コード例 #9
0
        /// <summary>
        /// Process an image: perform object detection for price tags, crop each detected object, perform OCR on each object.
        /// OCR is performed on a cropped image to improve accuracy of the OCRing.
        /// </summary>
        /// <param name="imagePath"></param>
        /// <param name="saveLocation"></param>
        /// <returns></returns>
        public async Task ProcessImage(string imagePath, string saveLocation)
        {
            using (var stream = File.OpenRead(imagePath))
            {
                Console.WriteLine($"Processing file {imagePath}...");
                Console.WriteLine("\tDetecting price tags...");

                // Create a Custom Vision prediction endpoint
                PredictionEndpoint endpoint = new PredictionEndpoint()
                {
                    ApiKey = this._customVisionPredictionKey
                };

                // Invoke the prediction endpoint to detect price tags
                var result = endpoint.PredictImage(this._customVisionProjectId, stream);

                int tagCount = 0;

                Console.WriteLine($"\tDetected {result.Predictions.Count} tags:");

                // Loop over each prediction and write out the results
                foreach (var c in result.Predictions)
                {
                    // Filter the detected objects to be of the right class and high enough probability
                    if (_detectionLabels.Contains(c.TagName) && c.Probability > this._probabilityThreshold)
                    {
                        // Recalculate bounding boxes from normalized values [0;1]
                        Image     sourceImage = Image.FromFile(imagePath);
                        Rectangle box         = CalculateBoundingBox(c.BoundingBox, sourceImage.Width, sourceImage.Height);

                        // Crop image to the recalculated bounded box
                        var cropped = CropImage(sourceImage, box);

                        Console.WriteLine($"\t\t{c.TagName}: {c.Probability:P1} [ {box.Left}, {box.Top}, {box.Width}, {box.Height} ]");

                        // Save the extracted objects to disk
                        cropped.Save(Path.Combine(saveLocation, $"tag_{tagCount}.jpg"));

                        Console.WriteLine("\t\tPerforming OCR...");

                        // Perform OCR on the detected price tags
                        await ReadText(cropped);

                        // Increment the counter
                        tagCount++;
                    }
                }
            }
        }
コード例 #10
0
        public IList <Prediction> PredictImage(Stream imageStream)
        {
            var account       = TrainingApi.GetAccountInfo();
            var predictionKey = account.Keys.PredictionKeys.PrimaryKey;

            var predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
            var endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            var result = endpoint.PredictImage(ProjectGuid, imageStream);

            return(result.Predictions.Select(p => new Prediction
            {
                Tag = Enum.Parse <Models.Training.Tag>(p.Tag),
                Probability = p.Probability
            }).ToList());
        }
コード例 #11
0
        public async Task Procesar(Stream img)
        {
            Indicador.IsRunning = true;
            var sproject = trainingApi.GetProjects().ToArray();
            var project  = sproject[0];
            var result   = endpoint.PredictImage(project.Id, img);
            var c        = result.Predictions.ToArray();
            await Task.Delay(10000);

            for (int i = 0; i < c.Length; i++)
            {
                Informacion = Informacion + "Con: " + c[i].Tag + "," + String.Format(" Total: {0:P2}.", c[i].Probability) + "\n";
            }
            Indicador.IsRunning = false;
            Mensaje.Text        = "Listo";
        }
コード例 #12
0
        public static bool IsValidCard(string url)
        {
            try
            {
                string trainingKey = Constants.CustomVisionTrainingAPIKey;

                // Create the Api, passing in a credentials object that contains the training key
                TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
                TrainingApi            trainingApi         = new TrainingApi(trainingCredentials);

                var          projects = trainingApi.GetProjects();
                ProjectModel project  = projects.FirstOrDefault(e => e.Name == "IDVision");

                // Get the prediction key, which is used in place of the training key when making predictions
                var account       = trainingApi.GetAccountInfo();
                var predictionKey = account.Keys.PredictionKeys.PrimaryKey;

                // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key
                PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
                PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

                byte[] byteData = Utilities.Utilities.GetImagesAsByteArrayFromUri(url);

                MemoryStream stream = new MemoryStream(byteData);
                // Make a prediction against the new project
                var predictionResult = endpoint.PredictImage(project.Id, stream);

                // Loop over each prediction and write out the results
                foreach (var c in predictionResult.Predictions)
                {
                    if ((c.Probability * 100) > 80)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(false);
        }
コード例 #13
0
        static void Main(string[] args)
        {
            MemoryStream testImage = null;
            Guid         projectId;
            string       predictionKey;

            try
            {
                Console.Write("Enter a local image URL: ");
                var imagePath = Console.ReadLine();
                testImage = new MemoryStream(File.ReadAllBytes(imagePath));

                Console.Write("Enter Project Id Guid: ");
                var projectIdGuid = Console.ReadLine();
                projectId = Guid.Parse(projectIdGuid);

                Console.Write("Enter Prediction Key: ");
                predictionKey = Console.ReadLine();

                Console.WriteLine();
                Console.WriteLine();

                // Utilizing the Microsoft.Cognitive.CustomVision NuGet Package to establish Credentials and the Endpoint
                PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
                PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

                ImagePredictionResultModel result = endpoint.PredictImage(projectId, testImage);

                Console.WriteLine("Prediction results for the given image are...");
                foreach (ImageTagPrediction prediction in result.Predictions)
                {
                    Console.WriteLine($"{prediction.Tag}: {prediction.Probability:P1}");
                }

                Console.WriteLine();
                exitInNSeconds(5);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                Console.WriteLine();
                exitInNSeconds(5);
            }
        }
コード例 #14
0
        public int Execute()
        {
            List <string> imagePaths = _pathArgument.Values;

            foreach (string imagePath in imagePaths)
            {
                if (!File.Exists(imagePath))
                {
                    return(Util.Fail($"The path '{imagePath}' does not exist."));
                }
            }

            TrainingApi trainingApi = new TrainingApi()
            {
                ApiKey = _trainingKey
            };
            IList <Iteration> iterations = trainingApi.GetIterations(_projectId);

            if (iterations.Count == 0)
            {
                return(Util.Fail("You must train the classifier before trying to make any prediction.\r\nUse the [upload] command to upload at least 5 images to a given classification (tag), and then use the command [train] to train the classifier."));
            }

            Guid iterationId = iterations.OrderByDescending(i => i.IsDefault).ThenByDescending(i => i.TrainedAt).First().Id;

            PredictionEndpoint predictionEndpoint = new PredictionEndpoint()
            {
                ApiKey = _predictionKey
            };

            foreach (string imagePath in imagePaths)
            {
                Console.WriteLine($"\r\n{imagePath}:");
                ImagePredictionResultModel result;
                using (var imageStream = new MemoryStream(File.ReadAllBytes(imagePath)))
                    result = predictionEndpoint.PredictImage(_projectId, imageStream, iterationId);

                foreach (ImageTagPredictionModel predictedTag in result.Predictions)
                {
                    Console.WriteLine($"\t{predictedTag.Tag}: {predictedTag.Probability:P2}");
                }
            }

            return(0);
        }
        public async Task <ImageClassificationResult> ClassifyImage(SoftwareBitmap bitmap)
        {
            var endpoint = new PredictionEndpoint()
            {
                ApiKey = _Configuration.CognitiveServicesCustomVisionApiKey
            };

            using (var stream = new InMemoryRandomAccessStream())
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                encoder.BitmapTransform.Flip = BitmapFlip.Horizontal;
                encoder.SetSoftwareBitmap(bitmap);

                await encoder.FlushAsync();

                var result = endpoint.PredictImage(
                    _Configuration.CognitiveServicesCustomVisionProjectId,
                    stream.AsStream()
                    );

                double highestProbability = 0;
                ImageTagPredictionModel predictedModel = null;

                foreach (var prediction in result.Predictions)
                {
                    if (prediction.Probability > highestProbability)
                    {
                        highestProbability = prediction.Probability;
                        predictedModel     = prediction;
                    }
                }

                if (predictedModel == null)
                {
                    return(null);
                }

                return(new ImageClassificationResult()
                {
                    Tag = predictedModel.Tag,
                    Probability = predictedModel.Probability
                });
            }
        }
コード例 #16
0
        static void Main(string[] args)
        {
            var keys = GetApiKeys();

            var trainingApi = new TrainingApi {
                ApiKey = keys.TrainingKey
            };
            var predictionEndpoint = new PredictionEndpoint {
                ApiKey = keys.PredictionKey
            };

            var projects    = trainingApi.GetProjects();
            var herbProject = projects.Where(p => p.Name == "Herbs").FirstOrDefault();

            Console.WriteLine("Input path to image to test:");
            var imagePath = Console.ReadLine();

            if (!File.Exists(imagePath))
            {
                Console.WriteLine("File does not exist. Press enter to exit.");
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Image predictions:");

            var imageFile = File.OpenRead(imagePath);

            if (herbProject != null)
            {
                var result = predictionEndpoint.PredictImage(herbProject.Id, imageFile);

                foreach (var prediction in result.Predictions)
                {
                    Console.WriteLine($"Tag: {prediction.Tag} Probability: {String.Format("Value: {0:P2}.", prediction.Probability)}");
                }
            }
            else
            {
                Console.WriteLine("Project doesn't exist.");
            }

            Console.ReadLine();
        }
コード例 #17
0
        public bool IsValid(Stream imageData)
        {
            if (IsInitialized)
            {
                var predictionEndpoint = new PredictionEndpoint()
                {
                    ApiKey = AzureCredentials.PredictionKey
                };

                var result = predictionEndpoint.PredictImage(CustomVisionProject.Id, imageData);

                var overallProbability = result.Predictions.Average(x => x.Probability);

                return(overallProbability >= PredictionThreashold);
            }
            else
            {
                throw new InvalidOperationException($"{nameof(NZDriverLicenceSmartIdReader)} is not yet initialized. You can monitor the status of the smart reader via the ${nameof(IsInitialized)} property.");
            }
        }
コード例 #18
0
        public static FoodSimResult CalcSim(string fullPath)
        {
            // Add your prediction key from the settings page of the portal
            // The prediction key is used in place of the training key when making predictions
            string predictionKey = "72de5d5f661e4114a737d5f56561b604";

            // Create a prediction endpoint, passing in obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            // Make a prediction against the new project
            var  testImage    = new MemoryStream(System.IO.File.ReadAllBytes(fullPath));
            Guid strProjectId = new Guid("6248af60-83de-4488-837e-6ba0d9e5ca14");

            var result = endpoint.PredictImage(strProjectId, testImage);

            //// Loop over each prediction and write out the results
            //foreach (var c in result.Predictions)
            //{
            //    Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}");
            //}

            double max  = 0;
            var    name = "";

            foreach (var c in result.Predictions)
            {
                if (c.Probability > max)
                {
                    max  = c.Probability;
                    name = c.Tag;
                }
            }

            return(new FoodSimResult()
            {
                Name = name, Similarity = max
            });
        }
コード例 #19
0
        static void Main(string[] args)
        {
            Guid   projectId     = Guid.Parse("[Insert project ID]");
            string predictionKey = "[Insert prediction Key]";
            string imagePath     = @"d:\sample.jpg";

            MemoryStream testImage = new MemoryStream(File.ReadAllBytes(imagePath));

            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
            PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            ImagePredictionResultModel result = endpoint.PredictImage(projectId, testImage);

            // Loop over each prediction and write out the results
            foreach (ImageTagPrediction prediction in result.Predictions)
            {
                Console.WriteLine($"\t{prediction.Tag}: {prediction.Probability:P1}");
            }

            Console.ReadKey();
        }
コード例 #20
0
        static void Main(string[] args)
        {
            Keys keys = null;

            using (var reader = new StreamReader("App.json"))
            {
                var json = reader.ReadToEnd();

                keys = JsonConvert.DeserializeObject <Keys>(json);
            }

            var trainingApi = new TrainingApi {
                ApiKey = keys.TrainingKey
            };
            var predictionEndpoint = new PredictionEndpoint {
                ApiKey = keys.PredictionKey
            };

            var projects    = trainingApi.GetProjects();
            var herbProject = projects.Where(p => p.Name == "Herbs").FirstOrDefault();

            Console.WriteLine("Predicting basil image");
            var imageFile = File.OpenRead("basil_test.jpg");

            if (herbProject != null)
            {
                var result = predictionEndpoint.PredictImage(herbProject.Id, imageFile);

                foreach (var prediction in result.Predictions)
                {
                    Console.WriteLine($"Tag: {prediction.Tag} Probability: {prediction.Probability}");
                }
            }
            else
            {
                Console.WriteLine("Project doesn't exist.");
            }

            Console.ReadLine();
        }
コード例 #21
0
ファイル: Prediction.cs プロジェクト: fcatae/haranohara
        public static void MakePrediction()
        {
            var predictionKey = "031439b0f9ec4549995e53e32971c605";


            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);

            PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials);



            // Make a prediction against the new project

            Console.WriteLine("Making a prediction:");

            var testImage = HomeController.HaraFinalImage;



            var projectId = new Guid("62ccee84-08f9-44e7-89f4-6b5b76894a8a");

            var result = endpoint.PredictImage(projectId, testImage, null, null);



            // Loop over each prediction and write out the results

            foreach (var c in result.Predictions)

            {
                Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}");

                PredictionResults pr = new PredictionResults();

                pr.tag        = c.Tag;
                pr.prediction = c.Probability;
            }
        }
コード例 #22
0
        public static List <PredictionModel> PredictionImage(string base64)
        {
            string predictionKey = "c5bda34631dc4e399c3114f35fc7f980";
            var    projectId     = new Guid("9ad813c1-4f29-432a-9c9e-e0c53d51a1e9");

            // Create a prediction endpoint, passing in the obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            byte[]                 data        = System.Convert.FromBase64String(base64);
            MemoryStream           ms          = new MemoryStream(data);
            var                    result      = endpoint.PredictImage(projectId, ms);
            List <PredictionModel> predictions = new List <PredictionModel>();
            // Loop over each prediction and write out the results
            var filterResult = result.Predictions.Where(p => p.Probability > 0.7).ToList();

            foreach (var p in filterResult)
            {
                predictions.Add(p);
            }
            return(predictions);
        }
コード例 #23
0
        private static IList <PredictionModel> GetPredictions(string documentbody, TraceWriter log)
        {
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            log.Info("Making prediction(s):");
            IList <PredictionModel> predictions;

            using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(documentbody)))
            {
                var result = endpoint.PredictImage(new Guid(projectId), ms, iterationId: new Guid(iterationId));
                predictions = result.Predictions;

                foreach (var p in result.Predictions)
                {
                    log.Info($"\t{p.TagName}: {p.Probability:P1} [ {p.BoundingBox.Left}, {p.BoundingBox.Top}, {p.BoundingBox.Width}, {p.BoundingBox.Height} ]");
                }
            }
            log.Info("Got prediction(s):");

            return(predictions);
        }
コード例 #24
0
        public static void RunCustomVisionService(Image image)
        {
            var predictionEndpointCredentials = new PredictionEndpointCredentials(
                Config.Default.CustomVisionPredictionKey
                );

            var endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            var result = endpoint.PredictImage(
                new Guid(Config.Default.CustomVisionProjectId),
                new MemoryStream(image.Bytes),
                new Guid(Config.Default.CustomVisionIterationId));

            var waldo = result
                        .Predictions
                        .FirstOrDefault(p => p.Tag == "waldo");

            if (waldo != null && waldo.Probability > 0.25d)
            {
                image.WaldoDetected = true;
            }

            image.ProcessedCustomVision = true;
        }
コード例 #25
0
        public void Main(string[] args)
        {
            // Add your training & prediction key from the settings page of the portal
            string trainingKey = "d7ba782c8051443c8557ff464418949f";
            //string predictionKey = "<your prediction key here>";

            // Create the Api, passing in the training key
            TrainingApi trainingApi = new TrainingApi()
            {
                ApiKey = trainingKey
            };

            // Create a new project
            Console.WriteLine("Creating new project:");
            //var project = trainingApi.CreateProject("My New Project");

            // Make two tags in the new project
            var hemlockTag        = trainingApi.CreateTag(project.Id, "Hemlock");
            var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry");

            // Add some images to the tags
            Console.WriteLine("\tUploading images");
            LoadImagesFromDisk();

            // Images can be uploaded one at a time
            foreach (var image in hemlockImages)
            {
                using (var stream = new MemoryStream(File.ReadAllBytes(image)))
                {
                    trainingApi.CreateImagesFromData(project.Id, stream, new List <string>()
                    {
                        hemlockTag.Id.ToString()
                    });
                }
            }

            // Or uploaded in a single batch
            var imageFiles = japaneseCherryImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();

            trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
            {
                japaneseCherryTag.Id
            }));

            // Now there are images with tags start training the project
            Console.WriteLine("\tTraining");
            var iteration = trainingApi.TrainProject(project.Id);

            // The returned iteration will be in progress, and can be queried periodically to see when it has completed
            while (iteration.Status == "Training")
            {
                Thread.Sleep(1000);

                // Re-query the iteration to get it's updated status
                iteration = trainingApi.GetIteration(project.Id, iteration.Id);
            }

            // The iteration is now trained. Make it the default project endpoint
            iteration.IsDefault = true;
            trainingApi.UpdateIteration(project.Id, iteration.Id, iteration);
            Console.WriteLine("Done!\n");

            // Now there is a trained endpoint, it can be used to make a prediction

            // Create a prediction endpoint, passing in obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            // Make a prediction against the new project
            Console.WriteLine("Making a prediction:");
            var result = endpoint.PredictImage(project.Id, testImage);

            // Loop over each prediction and write out the results
            foreach (var c in result.Predictions)
            {
                Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
            }
            Console.ReadKey();
        }
コード例 #26
0
        static void Main(string[] args)
        {
            // Add your training and prediction key from the settings page of the portal
            string trainingKey   = "<add your training key here>";
            string predictionKey = "<add your prediction key here>";

            // Create the Api, passing in the training key

            TrainingApi trainingApi = new TrainingApi()
            {
                ApiKey = trainingKey
            };

            // Create a new project
            Console.WriteLine("Creating new project:");
            var project = trainingApi.CreateProject("My First Project");

            // Make two tags in the new project
            var hemlockTag        = trainingApi.CreateTag(project.Id, "Hemlock");
            var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry");

            // Add some images to the tags
            Console.WriteLine("\\tUploading images");
            LoadImagesFromDisk();

            // Images are then uploaded
            foreach (var image in hemlockImages)
            {
                trainingApi.CreateImagesFromData(project.Id, image, new List <string>()
                {
                    hemlockTag.Id.ToString()
                });
            }

            foreach (var image in japaneseCherryImages)
            {
                trainingApi.CreateImagesFromData(project.Id, image, new List <string>()
                {
                    japaneseCherryTag.Id.ToString()
                });
            }

            // Now there are images with tags start training the project
            Console.WriteLine("\\tTraining");
            var iteration = trainingApi.TrainProject(project.Id);

            // The returned iteration will be in progress, and can be queried periodically to see when it has completed
            while (iteration.Status == "Training")
            {
                Thread.Sleep(1000);

                // Re-query the iteration to get it's updated status
                iteration = trainingApi.GetIteration(project.Id, iteration.Id);
            }

            // The iteration is now trained. Make it the default project endpoint
            iteration.IsDefault = true;
            trainingApi.UpdateIteration(project.Id, iteration.Id, iteration);
            Console.WriteLine("Done!\n");

            // Now there is a trained endpoint, it can be used to make a prediction
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            // Make a prediction against the new project
            Console.WriteLine("Making a prediction:");
            var result = endpoint.PredictImage(project.Id, testImage);

            // Loop over each prediction and write out the results
            foreach (var c in result.Predictions)
            {
                Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
            }
            Console.ReadKey();
        }
コード例 #27
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;

            //// calculate something for us to return
            //int length = (activity.Text ?? string.Empty).Length;

            //// return our reply to the user
            //await context.PostAsync($"You sent {activity.Text} which was {length} characters");

            //context.Wait(MessageReceivedAsync);

            var imageAttachment = activity.Attachments?.FirstOrDefault(a => a.ContentType.Contains("image"));

            var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            //if (imageAttachment != null)
            //{
            //    using (var stream = await GetImageStream(connector, imageAttachment))
            //    {
            //        //return await this.captionService.GetCaptionAsync(stream);
            //        var textOfStream = await this.captionService.GetCaptionAsync(stream);
            //        await context.PostAsync($"{textOfStream}");
            //    }
            //}
            ////ELSE --??
            //context.Wait(MessageReceivedAsync);


            string predictionProjectId = "3e152a33-92ee-4326-b4c9-c8068edab9d8"; //"YOUR_PROJECT_ID", //This is the custom vision project we are predicting against
            string predictionKey       = "eafbd7b247bd41c1afd6d6da352f7099";

            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            if (imageAttachment != null)
            {
                using (var stream = await GetImageStream(connector, imageAttachment))
                {
                    var           predictionImageResult       = endpoint.PredictImage(new Guid(predictionProjectId), stream);
                    StringBuilder predictionImageResultString = new StringBuilder();

                    var resultsList = new List <PredictionResult>();

                    foreach (var c in predictionImageResult.Predictions)
                    {
                        if (c.Probability > .50)
                        {
                            predictionImageResultString.Append(c.Tag);
                            predictionImageResultString.Append(", ");
                            var probability      = (decimal)c.Probability * 100;
                            var probabilityFixed = probability.ToString("#.##");
                            predictionImageResultString.Append(probabilityFixed);
                            predictionImageResultString.Append("%");
                            predictionImageResultString.Append("; ");
                        }
                        //resultsDictionary.Add(c.Tag, (decimal)c.Probability);
                        //resultsList.Add(new PredictionResult() { PredictionTag = c.Tag, PredictionProbability = (decimal)c.Probability});
                    }

                    await context.PostAsync($"Azure Custom Vision results: {predictionImageResultString}");

                    // var resultsDictionaryString = resultsDictionary.ToString();
                    // await context.PostAsync($"{resultsDictionaryString}");

                    var resultsListToString = String.Join <PredictionResult>(", ", resultsList.ToArray());
                    //await context.PostAsync($"{resultsListToString}");
                }
                context.Done(true);
            }
            else
            {
                context.Done(false);
            }

            //Original
            //context.Wait(MessageReceivedAsync);

            //if (imageAttachment != null)
            //{
            //    using (var stream = await GetImageStream(connector, imageAttachment))
            //    {
            //        //return await this.captionService.GetCaptionAsync(stream);
            //        var textOfStream = await this.captionService.GetCaptionAsync(stream);
            //        await context.PostAsync($"{textOfStream}");
            //    }
            //}
        }
コード例 #28
0
        private static void TestingTheModel(string testingSetPath, Project project)
        {
            var predictionKey      = ConfigurationManager.AppSettings["CustomVision_PredictionKey"];
            var predictionEndpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            var testModel = new List <Model>();

            var testSet          = Directory.GetDirectories(testingSetPath);
            var predictionResult = new Dictionary <string, int>();
            var labels           = new List <string>();

            foreach (var subdirectory in testSet)
            {
                var images = Directory.GetFiles($"{subdirectory}").Select(f =>
                {
                    testModel.Add(new Model()
                    {
                        Label = subdirectory, Path = f
                    });
                    return(new MemoryStream(File.ReadAllBytes(f)));
                }).ToList();
                foreach (var testImage in images)
                {
                    try
                    {
                        var dir   = new DirectoryInfo(subdirectory);
                        var label = dir.Name;
                        labels.Add(label);

                        Console.WriteLine($"\tActual tag: {label}");

                        var result          = predictionEndpoint.PredictImage(project.Id, testImage);
                        var highProbability = result.Predictions.OrderByDescending(x => x.Probability).First();


                        var predictedClass = highProbability.Tag;
                        var predictedProb  = highProbability.Probability;

                        var key = $"{label}|{predictedClass}";
                        if (!predictionResult.ContainsKey(key))
                        {
                            predictionResult.Add(key, 0);
                        }
                        predictionResult[key] = predictionResult[key] + 1;

                        // Loop over each prediction and write out the results
                        foreach (var c in result.Predictions)
                        {
                            Console.WriteLine($"\t{c.Tag}: {c.Probability:P2}");
                        }
                    }
                    catch (Exception e)
                    {
                        //kill exception and carry on
                        Console.WriteLine(e);
                    }
                }
            }
            try
            {
                using (TextWriter writer = new StreamWriter($"{testingSetPath}\\testModel.csv"))
                {
                    var csv = new CsvWriter(writer);

                    csv.WriteRecords(testModel);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            var array2D = GenerateConfusionMatrix(labels, predictionResult);

            //pretty print
            PrettyPrint(array2D);


            ExporttoCSV(testingSetPath, array2D);
        }
コード例 #29
0
        static void Main(string[] args)
        {
            var keys = GetApiKeys();

            var trainingApi = new TrainingApi {
                ApiKey = keys.TrainingKey
            };
            var predictionEndpoint = new PredictionEndpoint {
                ApiKey = keys.PredictionKey
            };

            var projects    = trainingApi.GetProjects();
            var herbProject = projects.FirstOrDefault(p => p.Name == "Herbs");

            Console.WriteLine("Press 1 to predict and 2 to train:");
            var pathChoice = Console.ReadLine();

            if ("1".Equals(pathChoice))
            {
                Console.WriteLine("Press 1 to predict on a URL or 2 to predict on a local file:");
                var predictType = Console.ReadLine();

                if ("1".Equals(predictType))
                {
                    Console.WriteLine("Input the URL to an image to test:");
                    var imageUrl = Console.ReadLine();

                    if (herbProject != null)
                    {
                        var result = predictionEndpoint.PredictImageUrl(herbProject.Id, new Microsoft.Cognitive.CustomVision.Prediction.Models.ImageUrl(imageUrl));

                        PrintResults(result);
                    }
                }
                else
                {
                    Console.WriteLine("Input path to image to test:");
                    var imagePath = Console.ReadLine();

                    if (!File.Exists(imagePath))
                    {
                        Console.WriteLine("File does not exist. Press enter to exit.");
                        Console.ReadLine();
                        return;
                    }

                    Console.WriteLine("Image predictions:");

                    var imageFile = File.OpenRead(imagePath);

                    if (herbProject != null)
                    {
                        var result = predictionEndpoint.PredictImage(herbProject.Id, imageFile);

                        PrintResults(result);
                    }
                    else
                    {
                        Console.WriteLine("Project doesn't exist.");
                    }
                }

                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Input path to image to train model with:");
                var imagePath = Console.ReadLine();

                Console.WriteLine("What tag would you give this image? Rosemary, cilantro, or basil?");
                var imageTag = Console.ReadLine();

                var capitilizedTag = char.ToUpper(imageTag.First()) + imageTag.Substring(1).ToLower();

                if (!File.Exists(imagePath))
                {
                    Console.WriteLine("File does not exist. Press enter to exit.");
                    Console.ReadLine();
                    return;
                }

                var imageFile = File.OpenRead(imagePath);

                var tags = trainingApi.GetTags(herbProject.Id);

                var matchedTag = tags.Tags.FirstOrDefault(t => t.Name == capitilizedTag);

                var memoryStream = new MemoryStream();
                imageFile.CopyTo(memoryStream);

                var fileCreateEntry = new ImageFileCreateEntry(imageFile.Name, memoryStream.ToArray());
                var fileCreateBatch = new ImageFileCreateBatch {
                    Images = new List <ImageFileCreateEntry> {
                        fileCreateEntry
                    }, TagIds = new List <Guid> {
                        matchedTag.Id
                    }
                };

                var result = trainingApi.CreateImagesFromFiles(herbProject.Id, fileCreateBatch);

                var resultImage = result.Images.FirstOrDefault();

                switch (resultImage.Status)
                {
                case "OKDuplicate":
                    Console.WriteLine("Image is already used for training. Please use another to train with");
                    Console.ReadLine();
                    break;

                default:
                    break;
                }

                var iteration = trainingApi.TrainProject(herbProject.Id);

                while (iteration.Status != "Completed")
                {
                    System.Threading.Thread.Sleep(1000);

                    iteration = trainingApi.GetIteration(herbProject.Id, iteration.Id);
                }

                iteration.IsDefault = true;
                trainingApi.UpdateIteration(herbProject.Id, iteration.Id, iteration);
                Console.WriteLine("Done training!");

                Console.ReadLine();
            }
        }
コード例 #30
0
        static void Main(string[] args)
        {
            // You can either add your training key here, pass it on the command line, or type it in when the program runs
            string trainingKey = GetTrainingKey("<your key here>", args);

            // Create the Api, passing in a credentials object that contains the training key
            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            TrainingApi            trainingApi         = new TrainingApi(trainingCredentials);

            // Create a new project
            Console.WriteLine("Creating new project:");
            var project = trainingApi.CreateProject("Car Assessment");

            // Make two tags in the new project
            var WriteOffTag = trainingApi.CreateTag(project.Id, "WriteOff");
            var DentTag     = trainingApi.CreateTag(project.Id, "Dent");

            // Add some images to the tags
            Console.WriteLine("\\tUploading images");
            LoadImagesFromDisk();

            // Images can be uploaded one at a time
            foreach (var image in WriteOffImages)
            {
                trainingApi.CreateImagesFromData(project.Id, image, new List <string> ()
                {
                    WriteOffTag.Id.ToString()
                });
            }

            // Or uploaded in a single batch
            trainingApi.CreateImagesFromData(project.Id, DentImages, new List <Guid> ()
            {
                DentTag.Id
            });

            // Now there are images with tags start training the project
            Console.WriteLine("\\tTraining");
            var iteration = trainingApi.TrainProject(project.Id);

            // The returned iteration will be in progress, and can be queried periodically to see when it has completed
            while (iteration.Status == "Training")
            {
                Thread.Sleep(1000);

                // Re-query the iteration to get it's updated status
                iteration = trainingApi.GetIteration(project.Id, iteration.Id);
            }

            // The iteration is now trained. Make it the default project endpoint
            iteration.IsDefault = true;
            trainingApi.UpdateIteration(project.Id, iteration.Id, iteration);
            Console.WriteLine("Done!\\n");

            // Now there is a trained endpoint, it can be used to make a prediction

            // Get the prediction key, which is used in place of the training key when making predictions
            var account       = trainingApi.GetAccountInfo();
            var predictionKey = account.Keys.PredictionKeys.PrimaryKey;

            // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key
            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
            PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            // Make a prediction against the new project
            Console.WriteLine("Making a prediction:");
            var result = endpoint.PredictImage(project.Id, testImage);

            // Loop over each prediction and write out the results
            foreach (var c in result.Predictions)
            {
                Console.WriteLine($"\\t{c.Tag}: {c.Probability:P1}");
            }
            Console.ReadKey();
        }