public async Task <IActionResult> Post([FromForm] ImageInfoViewModel imageViewModel) { var file = imageViewModel.File; try { using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); imageViewModel.Contents = memoryStream.ToArray(); } _context.Images.Add(new ImageInfo { Title = imageViewModel.Title, Contents = imageViewModel.Contents }); await _context.SaveChangesAsync(); } catch (Exception exception) { _logger.LogError(exception, $"Failed saving image"); } return(Ok()); }
/// <summary> /// Display current gray value and image x, y coordinates /// </summary> /// <param name="o"></param> protected void DisplayXYGray(object o) { double grayvalue = -1; int imageX = -1, imageY = -1; ShouldImageInfoDisplay = true; HSmartWindowControlWPF.HMouseEventArgsWPF e = (HSmartWindowControlWPF.HMouseEventArgsWPF)o; try { imageX = (int)e.Column; imageY = (int)e.Row; grayvalue = (InfoImage as HImage)?.GetGrayval(imageY, imageX); } catch (Exception exception) { ShouldImageInfoDisplay = false; } var pointRealWorld = new Point(e.Column, e.Row).Affine(ChangeOfBaseInv); // output grayvalue info GrayValueInfo = new ImageInfoViewModel() { X = UsingActualCoordinate? pointRealWorld.ImageX.ToString("f3") : ((int)e.Column).ToString(), Y = UsingActualCoordinate? pointRealWorld.ImageY.ToString("f3") : ((int)e.Row).ToString(), GrayValue = grayvalue }; // Set the pop-up position MouseX = (int)e.X + 20; MouseY = (int)e.Y + 20; }
public async Task <ImageInfoViewModel> MakeAnalysisRequest(IFormFile localFile = null, string remotePath = null) { byte[] imageByte; if (localFile != null) { imageByte = await convertLocalPathToByte(localFile); } else { imageByte = convertRemotePathToByte(remotePath); } var errors = new List <string>(); ImageInfoViewModel responseData = new ImageInfoViewModel(); try { HttpClient client = new HttpClient(); // Cabeçalho da requisição passando a chave da assinatura do Cognitive Services. client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); // Parametros da Requisição. string requestParameters = "visualFeatures=Categories,Description,Color&details=Celebrities"; // Construção da URI para chamada no REST API. string uri = endPoint + "?" + requestParameters; HttpResponseMessage response; using (ByteArrayContent content = new ByteArrayContent(imageByte)) { // Esse exemplo usa contentType "application/octet-stream". // Mas você pode usar outros contentTypes como o "application/json" // e o "multipart/form-data". content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // Chamada da API Rest. response = await client.PostAsync(uri, content); } // Pegando o retorno em JSON. var result = await response.Content.ReadAsStringAsync(); // Em caso de sucesso deserializa o JSON no objeto ImageInfoView if (response.IsSuccessStatusCode) { responseData = JsonConvert.DeserializeObject <ImageInfoViewModel>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } }); responseData.imageByte = imageByte; responseData.OCR = await AnalisysOCR(imageByte); } } catch (Exception e) { Console.WriteLine("\n" + e.Message); } return(responseData); }
// can only drop data of type ImageInfoViewModel private async void searchResultsGrid_Drop(object sender, DragEventArgs e) // This method searches an image that's dragged into the grid { ImageInfoViewModel iivm = null; iivm = e.Data.GetData(typeof(ImageInfoViewModel)) as ImageInfoViewModel; if (iivm == null) // If the image has no reachable data, terminate the method { return; } await vm.DownloadQueryImageAndResponseFromQueryImageStore(iivm); }
public async Task <ImageAnalysis> MakeAnalysisRequest() { string imageFilePath = @ "C:\Users\Rajeesh.raveendran\Desktop\Rajeesh.jpg"; var errors = new List <string>(); ImageInfoViewModel responeData = new ImageInfoViewModel(); try { HttpClient client = new HttpClient(); // Request headers. client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); // Request parameters. A third optional parameter is "details". string requestParameters = "visualFeatures=Categories,Description,Color"; // Assemble the URI for the REST API Call. string uri = endPoint + "?" + requestParameters; HttpResponseMessage response; // Request body. Posts a locally stored JPEG image. byte[] byteData = GetImageAsByteArray(imageFilePath); using (ByteArrayContent content = new ByteArrayContent(byteData)) { // This example uses content type "application/octet-stream". // The other content types you can use are "application/json" // and "multipart/form-data". content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // Make the REST API call. response = await client.PostAsync(uri, content); } // Get the JSON response. var result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { responeData = JsonConvert.DeserializeObject <ImageAnalysis>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } }); } } catch (Exception e) { Console.WriteLine("\n" + e.Message); } return(responeData); }
public ActionResult Delete(ImageInfoViewModel dbObj) { var imageInfo = db.Images.Where(x => x.Id == dbObj.Id).FirstOrDefault(); if (imageInfo != null) { try { //System.IO.File.Delete(Server.MapPath(Path.Combine("~/SimpleCMS/Data/Images", imageInfo.File.Name))); DeleteObject <SimpleCMS.Models.File, ImageInfo>(imageInfo.File.Id); DeleteObject <ImageInfo, ImageInfo>(imageInfo.Id); return(new HttpStatusCodeResult(HttpStatusCode.OK)); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } } return(new EmptyResult());// DeleteObject<ImageInfo, ImageInfo>(dbObj.Id); }
public async Task <ImageInfoViewModel> MakeAnalysisRequest(string subscriptionKey, string imagePath, string endPoint = "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze") { var errorList = new List <string>(); ImageInfoViewModel responeData = new ImageInfoViewModel(); try { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); string requestParameters = "visualFeatures=Categories,Description,Color"; string uri = endPoint + "?" + requestParameters; HttpResponseMessage httpResponseMessage; byte[] byteImage = ByteImage(imagePath); using (ByteArrayContent imageContent = new ByteArrayContent(byteImage)) { imageContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); httpResponseMessage = await client.PostAsync(uri, imageContent); } string responseResult = await httpResponseMessage.Content.ReadAsStringAsync(); if (httpResponseMessage.IsSuccessStatusCode) { responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(responseResult, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errorList.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } }); } } catch (Exception e) { return(new ImageInfoViewModel()); } return(responeData); }
//"https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr"; /// <summary> /// Gets the text visible in the specified image file by using /// the Computer Vision REST API. /// </summary> public async Task <string> MakeOCRRequest() { string imageFilePath = @"C:\Users\ENG SOON CHEAH\Desktop\HandWritten\bill.jpg"; var errors = new List <string>(); string extractedResult = ""; ImageInfoViewModel responeData = new ImageInfoViewModel(); try { HttpClient client = new HttpClient(); // Request headers. client.DefaultRequestHeaders.Add( "Ocp-Apim-Subscription-Key", subscriptionKey); // Request parameters. string requestParameters = "language=unk&detectOrientation=true"; // Assemble the URI for the REST API Call. string uri = endPoint + "?" + requestParameters; HttpResponseMessage response; // Request body. Posts a locally stored JPEG image. byte[] byteData = GetImageAsByteArray(imageFilePath); using (ByteArrayContent content = new ByteArrayContent(byteData)) { // This example uses content type "application/octet-stream". // The other content types you can use are "application/json" // and "multipart/form-data". content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // Make the REST API call. response = await client.PostAsync(uri, content); } // Get the JSON response. string result = await response.Content.ReadAsStringAsync(); //If it is success it will execute further process. if (response.IsSuccessStatusCode) { // The JSON response mapped into respective view model. responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } } ); var linesCount = responeData.regions[0].lines.Count; for (int i = 0; i < linesCount; i++) { var wordsCount = responeData.regions[0].lines[i].words.Count; for (int j = 0; j < wordsCount; j++) { //Appending all the lines content into one. extractedResult += responeData.regions[0].lines[i].words[j].text + " "; } extractedResult += Environment.NewLine; } } } catch (Exception e) { Console.WriteLine("\n" + e.Message); } return(extractedResult); }
public async Task <List <string> > GetIngredients(string imageBase64) { try { ImageInfoViewModel responeData = new ImageInfoViewModel(); HttpClient client = new HttpClient(); HttpResponseMessage response; string result = ""; byte[] image = Convert.FromBase64String(imageBase64); // Request headers. client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _configuration.GetSection("Cognitive:SubscriptionKey").Value); // Add the byte array as an octet stream to the request body. using (ByteArrayContent content = new ByteArrayContent(image)) { //"application/octet-stream" content type. content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // Asynchronously call the REST API method. response = await client.PostAsync($"{_configuration.GetSection("Cognitive:UriBase").Value}?{_configuration.GetSection("Cognitive:UriParameters").Value}", content); } // Asynchronously get the JSON response. string contentString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { // The JSON response mapped into respective view model. responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(contentString, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { earg.ErrorContext.Handled = true; } } ); var linesCount = responeData.regions[0].lines.Count; for (int i = 0; i < linesCount; i++) { var wordsCount = responeData.regions[0].lines[i].words.Count; for (int j = 0; j < wordsCount; j++) { //Concatenate only the text property if (responeData.regions[0].lines[i].words[j].text.ToUpper() != "INGREDIENTES") { result += responeData.regions[0].lines[i].words[j].text + " "; } } } } return(result.Split(",").ToList()); } catch (Exception) { throw new Exception("Falha ao extrair ingredientes"); } }
public async Task <Results> GetScanData() { string uploadPath = "~/ScanImage/"; var httpRequest = HttpContext.Current.Request; var filePath = string.Empty; foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; filePath = HttpContext.Current.Server.MapPath(uploadPath + postedFile.FileName); postedFile.SaveAs(filePath); // NOTE: To store in memory use postedFile.InputStream } var ci = new ContactInfo(); ImageInfoViewModel responeData = new ImageInfoViewModel(); string extractedResult = ""; var errors = new List <string>(); HttpResponseMessage response = await GetAzureVisionAPIResponse(filePath); // Get the JSON response. string result = await response.Content.ReadAsStringAsync(); //If it is success it will execute further process. if (response.IsSuccessStatusCode) { // The JSON response mapped into respective view model. responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } } ); var linesCount = responeData.regions[0].lines.Count; for (int i = 0; i < linesCount; i++) { var wordsCount = responeData.regions[0].lines[i].words.Count; for (int j = 0; j < wordsCount; j++) { //Appending all the lines content into one. extractedResult += responeData.regions[0].lines[i].words[j].text + " "; } extractedResult += Environment.NewLine; } //dynamic data = await req.Content.ReadAsAsync<object>(); OCRData ocr = JsonConvert.DeserializeObject <OCRData>(result.ToString()); Parallel.ForEach(ocr.Regions, (r) => { ci.Parse(r); }); } var results = new Results(ci.Parsers.Select(p => new { p.Name, p.Value }).ToDictionary(d => d.Name, d => d.Value), ci.UnKnown); return(results); }
//Δημιουργια συναρτησης για την επικοινωνια με το API public async void MakeRequest(string text) { var errors = new List <string>(); string extractedResult = ""; ImageInfoViewModel responeData = new ImageInfoViewModel(); try { HttpClient client = new HttpClient(); // Header αιτηματος client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key); // Παραμετροι αιτηματος. string requestParameters = "language=unk&detectOrientation=true"; // Δημιουργια του URI για την επικοινωνια με το API string uri = endpoint + "vision/v2.0/ocr?" + requestParameters; HttpResponseMessage response; // Μετατροπη της εικονας σε bytes byte[] byteData = GetImageAsByteArray(text); using (ByteArrayContent content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); } // Ληψη της απαντησης και μετατροπη σε string string result = await response.Content.ReadAsStringAsync(); // Ελεγχος για την ορθοτητα της απαντησης if (response.IsSuccessStatusCode) { responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } } ); var linesCount = responeData.regions[0].lines.Count; for (int i = 0; i < linesCount; i++) { var wordsCount = responeData.regions[0].lines[i].words.Count; for (int j = 0; j < wordsCount; j++) { //Δημιουργια ενος string για την εμφανιση των αποτελεσματων extractedResult += responeData.regions[0].lines[i].words[j].text + " "; } extractedResult += Environment.NewLine; } } } catch (Exception e) { Console.WriteLine("\n" + e.Message); } //Εμφανιση των αποτελεσματων στο Label showText.Text = extractedResult; showText.TextColor = Color.Red; }
public async Task <string> CognitiveVisionOCR(byte[] image) { try { string result = string.Empty; ImageInfoViewModel responeData = new ImageInfoViewModel(); HttpClient client = new HttpClient(); HttpResponseMessage response; _logger.LogInformation("Inicia a request"); // Request headers. client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _configuration.GetSection("Cognitive:SubscriptionKey").Value); _logger.LogInformation("Prepara o content"); // Add the byte array as an octet stream to the request body. using (ByteArrayContent content = new ByteArrayContent(image)) { //"application/octet-stream" content type. content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // Asynchronously call the REST API method. response = await client.PostAsync($"{_configuration.GetSection("Cognitive:UriBase").Value}?{_configuration.GetSection("Cognitive:UriParameters").Value}", content); } _logger.LogInformation("Acessa o serviço de OCR"); // Asynchronously get the JSON response. string contentString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { _logger.LogInformation("Retorno com sucesso"); // The JSON response mapped into respective view model. responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(contentString, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { earg.ErrorContext.Handled = true; } } ); _logger.LogInformation("Retorno deserializado"); var linesCount = responeData.regions[0].lines.Count; for (int i = 0; i < linesCount; i++) { var wordsCount = responeData.regions[0].lines[i].words.Count; for (int j = 0; j < wordsCount; j++) { //Concatenate only the text property result += responeData.regions[0].lines[i].words[j].text + " "; } } } return(result); } catch (Exception ex) { _logger.LogError($"Falha no OCR: { ex.ToString()}"); throw new Exception("Falha no OCR", ex); } }
public async Task <ImageInfoViewModel> MakeAnalysisRequest(string name, string path, string sub_key, string api_url) { JObject objurl = new JObject { { "url", path } }; var errors = new List <string>(); ImageInfoViewModel responeData = new ImageInfoViewModel(); var result = ""; try { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", sub_key); var uri = api_url; HttpResponseMessage response; byte[] byteData = Encoding.UTF8.GetBytes(objurl.ToString()); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); response = await client.PostAsync(uri, content); result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { responeData = JsonConvert.DeserializeObject <ImageInfoViewModel>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } }); } string imageTags = string.Empty; try { var Json = JObject.Parse(result); JArray jsonArray = JArray.Parse(Json["objects"].ToString()); int i = 0; foreach (var jsonKey in jsonArray) { var objectName = jsonKey["object"].ToString(); responeData.objects[0].objectName = objectName; i++; } } catch (Exception ex) { ExceptionLog.ErrorLogging(ex); } if (responeData != null) { imageTags = string.Join(',', responeData.Tags.Select(x => x.name).ToList()); } responeData.ImageId = ImgRepo.addImageTags(name, path, imageTags, responeData.RequestId); } } } catch (Exception ex) { ExceptionLog.ErrorLogging(ex); } return(responeData); }