private void MakePredictionRequest(string filepath) { CustomVisionTrainingClient trainingClient = new CustomVisionTrainingClient() { ApiKey = trainingKey, Endpoint = SouthCentralUsEndpoint }; // Create a prediction endpoint, passing in obtained prediction key CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = SouthCentralUsEndpoint }; var project = trainingClient.GetProjects().FirstOrDefault(); // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, new MemoryStream(File.ReadAllBytes(filepath))); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}"); } }
public async Task <IActionResult> Post(List <IFormFile> img) { var requestStream = Request.HttpContext.Items; const string scue = "https://southcentralus.api.cognitive.microsoft.com"; string trainingKey = "4f473807b7434dd5a1bdb45cb9104b38"; CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient() { ApiKey = trainingKey, Endpoint = scue }; // Find the object detection domain var domains = trainingApi.GetDomainsAsync(); //var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); var project = trainingApi.GetProject(Guid.Parse("b911d77a-ef25-47fd-86ed-87db4500ef7b")); const string southcentralus = "https://southcentralus.api.cognitive.microsoft.com"; string predictionKey = "a58f3ca5856c491db0b73b87cb1118cf"; CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = southcentralus }; var c = new List <PredictionModel>(); var result = endpoint.PredictImage(Guid.Parse("cbfa66a3-9815-47d6-a389-7438e468ac15"), img[0].OpenReadStream()); ImageUrl imgUrl = new ImageUrl(); imgUrl.Url = "https://http2.mlstatic.com/guitarra-tagima-pr-200-special-pr200-sunburst-D_NQ_NP_894387-MLB26271081482_112017-F.jpg"; var resultImageUrl = endpoint.PredictImageUrl(Guid.Parse("cbfa66a3-9815-47d6-a389-7438e468ac15"), imgUrl); foreach (var item in result.Predictions.OrderBy(x => x.Probability)) { var pm = new PredictionModel(Math.Round(item.Probability * 100), item.TagId, item.TagName, item.BoundingBox); if (pm.Probability > 70) { pm.BoundingBox.Top = Convert.ToInt32(pm.BoundingBox.Top * 380); pm.BoundingBox.Height = Convert.ToInt32(pm.BoundingBox.Height * 380); pm.BoundingBox.Left = Convert.ToInt32(pm.BoundingBox.Left * 700); pm.BoundingBox.Width = Convert.ToInt32(pm.BoundingBox.Width * 700); c.Add(pm); } } return(Ok(c)); }
protected void Button1_Click(object sender, EventArgs e) { try { //trainmodel(); if (FileUpload1.HasFile) { string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName); //Response.Redirect(Request.Url.AbsoluteUri); // Create the Api, passing in the training key //CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient() //{ // ApiKey = trainingKey, // Endpoint = SouthCentralUsEndpoint //}; //var project = trainingApi.GetProject(projectid); using (var stream = new MemoryStream(File.ReadAllBytes(Server.MapPath("~/Images/") + fileName))) { // Create a prediction endpoint, passing in obtained prediction key CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = SouthCentralUsEndpoint }; var predictions = new ImagePredictionResultModel(); // Make a prediction against the new project TextBox1.Text = string.Format("Making a prediction:") + System.Environment.NewLine; var result = endpoint.PredictImage(projectid, stream); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { TextBox1.Text += string.Format($"\t{c.TagName}: {c.Probability:P1}" + System.Environment.NewLine); } } } } catch (Exception ex) { showerror(ex); //throw ex; } }
public async Task <IActionResult> Post(List <IFormFile> img) { var requestStream = Request.HttpContext.Items; const string scue = "https://southcentralus.api.cognitive.microsoft.com"; string trainingKey = "4f473807b7434dd5a1bdb45cb9104b38"; CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient() { ApiKey = trainingKey, Endpoint = scue }; // Find the object detection domain var domains = trainingApi.GetDomainsAsync(); //var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); var project = trainingApi.GetProject(Guid.Parse("b911d77a-ef25-47fd-86ed-87db4500ef7b")); const string southcentralus = "https://southcentralus.api.cognitive.microsoft.com"; string predictionKey = "a58f3ca5856c491db0b73b87cb1118cf"; CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = southcentralus }; //foreach (var item in img) //{ // var result = endpoint.PredictImage(project.Id, item.OpenReadStream()); // foreach (var c in result.Predictions) // { // //Console.WriteLine($"\t{c.TagName}: {c.Probability:P1} [ {c.BoundingBox.Left}, {c.BoundingBox.Top}, {c.BoundingBox.Width}, {c.BoundingBox.Height} ]"); // return Ok(c); // } //} var result = endpoint.PredictImage(project.Id, img[0].OpenReadStream()); //var a = Math.Round(result.Predictions[0].Probability, 5); //var b = Math.Round(result.Predictions[1].Probability, 5); //var c = result.Predictions[0]; //var d = result.Predictions[1]; return(Ok(result.Predictions)); }
public async void objectdetect() { try { //string testImageFile = @"D:\Pictures\test_img1.jpg"; //string subscriptionKey = ConfigurationManager.AppSettings["subscriptionKey"].ToString(); byte[] data = System.Convert.FromBase64String(Request.Form["formfield"]); if (data.Length > 0) { using (Stream s = new MemoryStream(data)) { // Create a prediction endpoint, passing in obtained prediction key CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = SouthCentralUsEndpoint }; // Make a prediction against the new project TextBox1.Text = string.Format("Making a prediction:") + System.Environment.NewLine; var result = endpoint.PredictImage(projectid, s); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { TextBox1.Text += string.Format($"\t{c.TagName}: {c.Probability:P1}" + System.Environment.NewLine); } } } } catch (Exception ex) { showerror(ex); //throw ex; } }
public static string PredictImage(Stream imageStream) { if (busy) { App.Log.WriteInfo("Skip image while robot is in process"); return(null); } App.Log.WriteInfo(String.Format("ComputerVision call. images size {0}", imageStream.Length)); ImagePrediction results = endpoint.PredictImage(projectId, imageStream); PredictionModel model = results.Predictions.OrderByDescending <PredictionModel, double>(m => m.Probability).First <PredictionModel>(); App.Log.WriteInfo(String.Format("ComputerVision recognize an images as {0} with probability {1}", model.TagName, model.Probability)); if (model.Probability > predictionThreshold) { DoRobotAction(model.TagName); return(model.TagName); } else { return(null); } }
static void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "<your training key here>"; string predictionKey = "<your prediction key here>"; // Create the Api, passing in the training key CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient() { ApiKey = trainingKey, Endpoint = SouthCentralUsEndpoint }; // Find the object detection domain var domains = trainingApi.GetDomains(); var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My New Project", null, objDetectionDomain.Id); // Make two tags in the new project var forkTag = trainingApi.CreateTag(project.Id, "fork"); var scissorsTag = trainingApi.CreateTag(project.Id, "scissors"); Dictionary <string, double[]> fileToRegionMap = new Dictionary <string, double[]>() { // FileName, Left, Top, Width, Height { "scissors_1", new double[] { 0.4007353, 0.194068655, 0.259803921, 0.6617647 } }, { "scissors_2", new double[] { 0.426470578, 0.185898721, 0.172794119, 0.5539216 } }, { "scissors_3", new double[] { 0.289215684, 0.259428144, 0.403186262, 0.421568632 } }, { "scissors_4", new double[] { 0.343137264, 0.105833367, 0.332107842, 0.8055556 } }, { "scissors_5", new double[] { 0.3125, 0.09766343, 0.435049027, 0.71405226 } }, { "scissors_6", new double[] { 0.379901975, 0.24308826, 0.32107842, 0.5718954 } }, { "scissors_7", new double[] { 0.341911763, 0.20714055, 0.3137255, 0.6356209 } }, { "scissors_8", new double[] { 0.231617644, 0.08459154, 0.504901946, 0.8480392 } }, { "scissors_9", new double[] { 0.170343131, 0.332957536, 0.767156839, 0.403594762 } }, { "scissors_10", new double[] { 0.204656869, 0.120539248, 0.5245098, 0.743464053 } }, { "scissors_11", new double[] { 0.05514706, 0.159754932, 0.799019635, 0.730392158 } }, { "scissors_12", new double[] { 0.265931368, 0.169558853, 0.5061275, 0.606209159 } }, { "scissors_13", new double[] { 0.241421565, 0.184264734, 0.448529422, 0.6830065 } }, { "scissors_14", new double[] { 0.05759804, 0.05027781, 0.75, 0.882352948 } }, { "scissors_15", new double[] { 0.191176474, 0.169558853, 0.6936275, 0.6748366 } }, { "scissors_16", new double[] { 0.1004902, 0.279036, 0.6911765, 0.477124184 } }, { "scissors_17", new double[] { 0.2720588, 0.131977156, 0.4987745, 0.6911765 } }, { "scissors_18", new double[] { 0.180147052, 0.112369314, 0.6262255, 0.6666667 } }, { "scissors_19", new double[] { 0.333333343, 0.0274019931, 0.443627447, 0.852941155 } }, { "scissors_20", new double[] { 0.158088237, 0.04047389, 0.6691176, 0.843137264 } }, { "fork_1", new double[] { 0.145833328, 0.3509314, 0.5894608, 0.238562092 } }, { "fork_2", new double[] { 0.294117659, 0.216944471, 0.534313738, 0.5980392 } }, { "fork_3", new double[] { 0.09191177, 0.0682516545, 0.757352948, 0.6143791 } }, { "fork_4", new double[] { 0.254901975, 0.185898721, 0.5232843, 0.594771266 } }, { "fork_5", new double[] { 0.2365196, 0.128709182, 0.5845588, 0.71405226 } }, { "fork_6", new double[] { 0.115196079, 0.133611143, 0.676470637, 0.6993464 } }, { "fork_7", new double[] { 0.164215669, 0.31008172, 0.767156839, 0.410130739 } }, { "fork_8", new double[] { 0.118872553, 0.318251669, 0.817401946, 0.225490168 } }, { "fork_9", new double[] { 0.18259804, 0.2136765, 0.6335784, 0.643790841 } }, { "fork_10", new double[] { 0.05269608, 0.282303959, 0.8088235, 0.452614367 } }, { "fork_11", new double[] { 0.05759804, 0.0894935, 0.9007353, 0.3251634 } }, { "fork_12", new double[] { 0.3345588, 0.07315363, 0.375, 0.9150327 } }, { "fork_13", new double[] { 0.269607842, 0.194068655, 0.4093137, 0.6732026 } }, { "fork_14", new double[] { 0.143382356, 0.218578458, 0.7977941, 0.295751631 } }, { "fork_15", new double[] { 0.19240196, 0.0633497, 0.5710784, 0.8398692 } }, { "fork_16", new double[] { 0.140931368, 0.480016381, 0.6838235, 0.240196079 } }, { "fork_17", new double[] { 0.305147052, 0.2512582, 0.4791667, 0.5408496 } }, { "fork_18", new double[] { 0.234068632, 0.445702642, 0.6127451, 0.344771236 } }, { "fork_19", new double[] { 0.219362751, 0.141781077, 0.5919118, 0.6683006 } }, { "fork_20", new double[] { 0.180147052, 0.239820287, 0.6887255, 0.235294119 } } }; // Add all images for fork var imagePath = Path.Combine("Images", "fork"); var imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var fileName in Directory.EnumerateFiles(imagePath)) { var region = fileToRegionMap[Path.GetFileNameWithoutExtension(fileName)]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(forkTag.Id, region[0], region[1], region[2], region[3]) }))); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); // Add all images for scissors imagePath = Path.Combine("Images", "scissors"); imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var fileName in Directory.EnumerateFiles(imagePath)) { var region = fileToRegionMap[Path.GetFileNameWithoutExtension(fileName)]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(scissorsTag.Id, region[0], region[1], region[2], region[3]) }))); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); // 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 its 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 the obtained prediction key CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = SouthCentralUsEndpoint }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var imageFile = Path.Combine("Images", "test", "test_image.jpg"); using (var stream = File.OpenRead(imageFile)) { var result = endpoint.PredictImage(project.Id, File.OpenRead(imageFile)); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1} [ {c.BoundingBox.Left}, {c.BoundingBox.Top}, {c.BoundingBox.Width}, {c.BoundingBox.Height} ]"); } } Console.ReadKey(); }
static void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "<your training key here>"; string predictionKey = "<your prediction key here>"; // Create the Api, passing in the training key CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient() { ApiKey = trainingKey, Endpoint = SouthCentralUsEndpoint }; // 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 CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { ApiKey = predictionKey, Endpoint = SouthCentralUsEndpoint }; // 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(); }
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result) { /* When MessageReceivedAsync is called, it's passed an IAwaitable<IMessageActivity>. To get the message, * await the result. */ var message = await result as Activity; if (message.Attachments.Count > 0) { // Create a prediction endpoint, passing in obtained prediction key CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient() { //Prediction Key from custom vision web site ApiKey = "6c3cfebc453243f987a3966b781f6faf", Endpoint = SouthCentralUsEndpoint }; /* Handle the attachment file from user */ foreach (var file in message.Attachments) { byte[] attachmentBytes = new System.Net.WebClient().DownloadData(file.ContentUrl); MemoryStream attachmentMemoryStream = new MemoryStream(attachmentBytes); //Id as below is the project ID from custom vision web site var predictImgResult = endpoint.PredictImage(Guid.Parse("a4fe9af2-e1b9-45d9-84b9-0abc141f32e2"), attachmentMemoryStream); string replyMsg = string.Empty; foreach (var c in predictImgResult.Predictions) { replyMsg += c.Probability.ToString("0.00") + " could be " + c.TagName + Environment.NewLine; } await context.PostAsync("File received!And the prediction result is: " + replyMsg); } } else { /* When message return, call Text Analutics service to identify language type */ ITextAnalyticsClient textanalyticsClient = new TextAnalyticsClient(new ApiKeyServiceClientCredentials()) { Endpoint = ConfigurationManager.AppSettings["TextAnalyticsEndPoint"] }; var languageDetectResult = textanalyticsClient.DetectLanguageAsync(new BatchInput( new List <Input>() { new Input("id", message.Text) })).Result; if (languageDetectResult.Documents != null && languageDetectResult.Documents.Count > 0) { //ch-ZN support if (languageDetectResult.Documents[0].DetectedLanguages.Count(c => c.Name.Equals("Chinese_Simplified")) > 0) { await context.Forward(new BasicQnAMakerZNDialog(), AfterAnswerAsync, message, CancellationToken.None); } //en-UK support else { await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None); if (message.Text.ToLower().Contains("mail") || message.Text.ToLower().Contains("manager")) { Mail.SendMail(); } } } //Default support language is en-UK also else { await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None); } } }