public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { CustomVision ct = new CustomVision(); PredictionQueries pq = new PredictionQueries(); var url = "https://http2.mlstatic.com/D_NP_674831-MLB26409090054_112017-Q.jpg"; // parse query parameter string name = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) .Value; if (name == null) { // Get request body dynamic data = await req.Content.ReadAsAsync <object>(); name = data?.name; } var result = await ct.PredictionImages(url); await pq.Add(result, url); var jsonToReturn = JsonConvert.SerializeObject(name); return(name == null ? req.CreateResponse(HttpStatusCode.BadRequest, "Status: " + "BadRequest", "application/json") : req.CreateResponse(HttpStatusCode.OK, "Status: " + "Sucess", "application/json")); }
private async void MakePredictionAsync(string fileName) { string url = "https://landmark.cognitiveservices.azure.com/customvision/v3.0/Prediction/88779494-c57a-4a82-a167-acf39c557f34/classify/iterations/Iteration3/image"; string predictionKey = "e2a39733301d4598973ca569788eb665"; string contentType = "application/octet-stream"; var file = File.ReadAllBytes(fileName); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Prediction-Key", predictionKey); using (var content = new ByteArrayContent(file)) { content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); var response = await client.PostAsync(url, content); var responseString = await response.Content.ReadAsStringAsync(); CustomVision customVisionResponse = JsonConvert.DeserializeObject <CustomVision>(responseString); List <Prediction> predictions = customVisionResponse.predictions; predictionsListView.ItemsSource = predictions; } } }
// Form Classification using Custom Vision AI public static async Task <string> MakeCustomVisionRequestByUrl(string imageUrl) { var client = new HttpClient(); // Request headers string urlBaseCusvomVision = Environment.GetEnvironmentVariable("CustomVisionUrlBase"); string keyCustomVision = Environment.GetEnvironmentVariable("CustomVisionPredictionKey"); //Prediction API endpoint will be here client.DefaultRequestHeaders.Add("Prediction-key", keyCustomVision); Uri uri = new Uri(urlBaseCusvomVision); HttpResponseMessage response; // Request body byte[] byteData = Encoding.UTF8.GetBytes("{\"Url\": \"" + imageUrl + "\"}"); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); response = await client.PostAsync(uri, content); //JSON Deserialization string jsoncust = response.Content.ReadAsStringAsync().Result; CustomVision custobjectVision = JsonConvert.DeserializeObject <CustomVision>(jsoncust); //Bring the highest scoring tag var result = (from p in custobjectVision.Predictions orderby p.Probability descending select p.TagName).FirstOrDefault <string>(); return(result); } }
private async Task RecuperarImagensAlbuns(IDialogContext contexto) { var activity = contexto.Activity as Activity; try { var customVision = new CustomVision(null, StorageProvider, RetrievalProvider); var reply = await customVision.RetrieveImagesAsync(activity.Text ?? "Sem Nome"); var filesCount = reply.Files.Count(); await contexto.PostAsync(filesCount > 0?$"Obaaa, encontrei {reply.Files.Count()} fotos no album." : "Infelizmente eu não encontrei nenhuma foto no álbum :("); if (filesCount == 0) { contexto.Wait(MessageReceivedAsync); return; } var message = AttachFiles(activity, reply); await contexto.PostAsync(message); } catch (Exception err) { await contexto.PostAsync("Ops! Deu algo errado na hora de analisar sua imagem!"); } contexto.Wait(MessageReceivedAsync); }
private async Task ProcessarImagemAsync(IDialogContext contexto) //IAwaitable<IMessageActivity> argument { var activity = contexto.Activity as Activity; var hasAttachment = activity.Attachments?.Any() == true; var uri = string.Empty; IExtractHttpContent content = null; if (hasAttachment) { uri = activity.Attachments[0].ContentUrl; content = new StreamFileContent(uri); } else { uri = activity.Text; content = new UriFileContent(uri); } try { customVision = new CustomVision(content, StorageProvider, RetrievalProvider); var reply = await customVision.ProcessImage(); await contexto.PostAsync(reply); contexto.Wait((c, a) => customVision.StoreImage()); } catch (Exception) { await contexto.PostAsync("Ops! Deu algo errado na hora de analisar sua imagem!"); } }
static async Task <List <string> > MakeAnalysisWithImage(Stream stream) { List <string> result = new List <string>(); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Prediction-Key", "YOUR_PREDICTION_KEY"); // Assemble the URI for the REST API Call. var uri = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.1/Prediction/7fd5e693-b9ff-44db-879f-28e77fae08c8/image"; HttpResponseMessage response; BinaryReader binaryReader = new BinaryReader(stream); byte[] byteData = binaryReader.ReadBytes((int)stream.Length); using (ByteArrayContent content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // Execute the REST API call. response = await client.PostAsync(uri, content); string jsoncust = response.Content.ReadAsStringAsync().Result; CustomVision custobjectVision = JsonConvert.DeserializeObject <CustomVision>(jsoncust); result.AddRange(custobjectVision.Predictions.Where(p => (decimal)p.Probability > 0.6m).Select(a => a.Tag)); return(result); } }
private async Task Update(bool stockTaking) { if (stockTaking) { this.Stock.Clear(); } else { this.NewStock.Clear(); } foreach (var camera in this?.CameraContainer?.Cameras) { // Check to make sure stream is not shutdown await camera.TakeSnapshotAsync(); using (var stream = new InMemoryRandomAccessStream()) { try { // Create an encoder with the desired format BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetSoftwareBitmap(camera.LastImage); await encoder.FlushAsync(); } catch (Exception ex) { } var result = await CustomVision.Predict(stream.AsStream(), this.PredictionKey, this.PredictionUri); var predictions = result.Predictions.OrderBy(p => p.Probability); foreach (var prediction in predictions.Where(p => p.Probability >= 0.20)) { if (stockTaking) { this.Stock.Add(ProductItems.Items.Single(p => p.Name == prediction.TagName)); } else { this.NewStock.Add(ProductItems.Items.Single(p => p.Name == prediction.TagName)); } } } } }
private static async Task <List <string> > MakeAnalysisWithURL(Activity activity) { List <string> result = new List <string>(); var client = new HttpClient(); // Request headers // Buraya prediction API Key'inizi girebilirsiniz. client.DefaultRequestHeaders.Add("Prediction-key", "YOUR_PREDICTION_KEY"); //Buraya URL ile sorgu yapmayı sağlayan Prediction API endpoint'inizi ekleyin var uri = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.1/Prediction/7fd5e693-b9ff-44db-879f-28e77fae08c8/url"; HttpResponseMessage response; // Request body byte[] byteData = Encoding.UTF8.GetBytes("{\"Url\": \"" + activity.Text + "\"}"); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); response = await client.PostAsync(uri, content); //Console.WriteLine(await response.Content.ReadAsStringAsync()); //JSON Deserialization string jsoncust = response.Content.ReadAsStringAsync().Result; CustomVision custobjectVision = JsonConvert.DeserializeObject <CustomVision>(jsoncust); //Search objesi için gerekli sonuçların alınması Console.WriteLine("---\n\nCustom Vision Detayları"); foreach (Prediction p in custobjectVision.Predictions) { Console.WriteLine(p.Tag + " - " + p.Probability); } result.AddRange(custobjectVision.Predictions.Where(p => (decimal)p.Probability > 0.6m).Select(a => a.Tag)); return(result); } }
public async Task CarPhotoReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument) { var message = await argument; if (message.Attachments != null && message.Attachments.Count > 0) { var attachment = message.Attachments[0]; if (attachment.ContentType == "image/png" || attachment.ContentType == "image/jpeg") { var contentStream = await ImageStream.GetImageStream(attachment.ContentUrl); JObject json = await CustomVision.GetCustomVisionJson(contentStream); // check is car Boolean isCar = false; foreach (var prediction in json["predictions"]) { //var tag = prediction["tagName"]; //var percent = decimal.Parse(prediction["probability"].ToString()) * 100; //var percentStr = percent.ToString("0.##"); //await context.PostAsync($"{tag} {percentStr}%"); var tag = prediction["tagName"].ToString(); var percent = decimal.Parse(prediction["probability"].ToString()) * 100; if (tag == "car" && percent >= 50) { isCar = true; break; } } if (isCar) { foreach (var prediction in json["predictions"]) { var tag = prediction["tagName"].ToString(); if (tag == "car") { continue; } else { SuggestCarConditionFromModel(tag); this.user.likedCar = this.user.suggestCar3; var outMessage = context.MakeMessage(); outMessage.Attachments = new List <Attachment>() { new HeroCard { Title = this.user.suggestCar3, Text = $"จากรูปรถของ{this.user.genderThai} รถยนต์ที่เหมาะสมและใกล้เคียงกับความต้องการของ{this.user.genderThai} คือ {this.user.suggestCar3}", Images = new List <CardImage> { new CardImage(this.user.suggestImage3) }, Buttons = new List <CardAction> { new CardAction(ActionTypes.OpenUrl, "ข้อมูล", value: this.user.suggestUrl3) } }.ToAttachment() }; await context.PostAsync(outMessage); var quiz = $"{this.user.genderThai}ชอบรถยนต์คันนี้หรือไม่"; PromptDialog.Choice(context, this.OnLikeVisionCarSelected, yesNoOptions, quiz, "ออเจ้าเลือกไม่ถูกต้อง", 3); break; } } } else { await context.PostAsync($"ข้าว่านี่ไม่ใช่รถนะเจ้าคะ โปรดส่งรูปรถให้ข้าเถิดเจ้าค่ะ"); context.Wait(CarPhotoReceivedAsync); } } else { await context.PostAsync($"ข้าว่านี่ไม่ใช่รูปนะเจ้าคะ โปรดส่งรูปรถให้ข้าเถิดเจ้าค่ะ"); context.Wait(CarPhotoReceivedAsync); } } else { await context.PostAsync($"ข้าไม่เห็นรูปอะไรเลยเจ้าค่ะ"); context.Wait(CarPhotoReceivedAsync); } }