/// <summary> /// Returns prediction or null if an error occurs. /// </summary> public virtual string Predict(string thumbnailUrl) { Logger.Log($"Making a prediction of {CustomVisionModelName} for: " + thumbnailUrl); predictionApi = new CustomVisionPredictionClient() { ApiKey = CustomVisionKey, Endpoint = CustomVisionEndPoint }; try { PredictionModels.ImageUrl thumbnail = new PredictionModels.ImageUrl(CropThumbnailUrl + thumbnailUrl); var result = predictionApi.ClassifyImageUrl(ProjectId, CustomVisionModelName, thumbnail); //LogPredicitions(result.Predictions); return(GetHighestRankedPrediction(result.Predictions)); } catch (PredictionModels.CustomVisionErrorException e) { Logger.Log($"Error making prediction for {thumbnailUrl}\n\t" + e.Response.Content, e); return(null); } }
public IList <Result> predictingImages(String imgUrl) { IList <Result> predictionResult = new List <Result>(); CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = SouthCentralUsEndpoint }; projectID = new Guid(ConfigurationManager.AppSettings["CustomVisionProjectID"]); Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl testImage = new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(imgUrl); Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction result1 = endpoint.ClassifyImageUrl(projectID, "Veg Classifier", testImage); foreach (var output in result1.Predictions) { Result finalresult = new Result(); finalresult.probability = output.Probability * 100; finalresult.tagName = output.TagName; predictionResult.Add(finalresult); } return(predictionResult); }
public async Task <string> DemoPredictFromUrl(CustomVisionPrediction customVisionPrediction, string projectId, string publishedName) { CustomVisionPredictionClient predictionApi = AuthenticatePrediction(customVisionPrediction.Endpoint, customVisionPrediction.PredictionKey); Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl predictionUrl = new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(customVisionPrediction.ImageUrl); var predictionResult = await predictionApi.DetectImageUrlAsync(Guid.Parse(projectId), publishedName, predictionUrl); IList <PredictionModel> predictionsToKeep = new List <PredictionModel>(); // Download the image first // Set the file metadata var _path = Path.GetTempPath(); var _startPath = $"{_path}/Images/"; string _fileName = $"{projectId}-demo-original.jpg"; string _fileNamePredicted = $"{projectId}-demo-prediction.jpg"; using (WebClient downloader = new WebClient()) { downloader.DownloadFile(new Uri(customVisionPrediction.ImageUrl), $"{_startPath}{_fileName}"); } // Load the image (probably from your stream) using (var image = System.Drawing.Image.FromFile($"{_startPath}{_fileName}")) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image)) { System.Drawing.Color customColor = System.Drawing.Color.FromArgb(50, System.Drawing.Color.Green); using (System.Drawing.Brush br = new System.Drawing.SolidBrush(customColor)) { // Retrieve only relevant predictions foreach (var prediction in predictionResult.Predictions) { if (prediction.Probability > .6) { var Xmin = prediction.BoundingBox.Left * image.Width; var Ymin = prediction.BoundingBox.Top * image.Height; var Xmax = prediction.BoundingBox.Width * image.Width; var Ymax = prediction.BoundingBox.Height * image.Height; predictionsToKeep.Add(prediction); // Create a new pen. System.Drawing.Pen skyBluePen = new System.Drawing.Pen(System.Drawing.Brushes.Lime); // Set the pen's width. skyBluePen.Width = 8.0F; g.DrawRectangle(skyBluePen, (int)Xmin, (int)Ymin, (int)Xmax, (int)Ymax); } } image.Save($"{_startPath}{_fileNamePredicted}"); } } } // Set the blob client blobClient = InitiateBlobClient(); // Set the container if it doesn't exist cloudBlobContainer = await FindOrCreateBlob(blobClient, Guid.Parse(projectId)); CloudBlockBlob cloudBlockBlobImage = cloudBlobContainer.GetBlockBlobReference(_fileNamePredicted); // Check if the file already exits in the blob, if it doesn't, upload it. bool imageUrl = DoesFileExist(_fileNamePredicted, blobClient, projectId.ToString()); if (!imageUrl) { await cloudBlockBlobImage.UploadFromFileAsync($"{_startPath}{_fileNamePredicted}"); } return(cloudBlockBlobImage.Uri.ToString()); }