public String DoOCR([FromForm] OcrModel request) { string name = request.Image.FileName; var image = request.Image; if (image.Length > 0) { using (var fileStream = new FileStream(folderName + image.FileName, FileMode.Create)) { image.CopyTo(fileStream); } } string tessPath = Path.Combine(trainedDataFolderName, ""); string result = ""; using (var engine = new TesseractEngine(tessPath, request.DestinationLanguage, EngineMode.Default)) { using (var img = Pix.LoadFromFile(folderName + name)) { var page = engine.Process(img); result = page.GetText(); Console.WriteLine(result); } } return(String.IsNullOrWhiteSpace(result) ? "Ocr is finished. Return empty" : result); }
public IActionResult Index(OcrModel ocrModel) { //string contentRootPath = _webHostEnvironment.ContentRootPath; //string tessPath = Path.Combine(contentRootPath, trainedDataFolderName); string filePath = ProcessImage(ocrModel.CroppedImage); string webRootPath = _webHostEnvironment.WebRootPath; var uploads = Path.Combine(webRootPath, "images"); string tessPath = Path.Combine(trainedDataFolderName, ""); Console.WriteLine(tessPath); string result = ""; using (var engine = new TesseractEngine(tessPath, ocrModel.DestinationLanguage, EngineMode.Default)) { using (var img = Pix.LoadFromFile(filePath)) { var page = engine.Process(img); result = page.GetText(); Console.WriteLine(result); } } string imageText = String.IsNullOrWhiteSpace(result) ? "Unreadable image text" : result; return(RedirectToAction(nameof(GetReviews), new { imageText })); }
public String DoOCR([FromForm] OcrModel request) { string name = request.Image.FileName; var image = request.Image; var imageStream = new MemoryStream(); if (image.Length > 0) { image.CopyTo(imageStream); } string tessPath = Path.Combine(trainedDataFolderName, ""); string result = ""; // TODO: Create one instance of engine and inject into app using (var engine = new TesseractEngine(tessPath, request.DestinationLanguage, EngineMode.Default)) { // whitelist numbers only engine.SetVariable("tessedit_char_whitelist", "0123456789"); var img = Pix.LoadFromMemory(imageStream.GetBuffer()); var page = engine.Process(img); result = page.GetText(); Console.WriteLine(result); } return(String.IsNullOrWhiteSpace(result) ? "Ocr is finished. Return empty" : result); }
public FileStreamResult GetOcredPdf([FromForm] OcrModel request) { string tessPath = Path.Combine(trainedDataFolderName, ""); string name = request.Image.FileName; Guid guid = Guid.NewGuid(); string outFile = @$ "c:\solvia\{Guid.NewGuid()}";
private string OcrFormat(OcrModel ocrModel) { return((from reg in ocrModel.regions from line in reg.lines from word in line.words select word) .Aggregate(String.Empty, (current, word) => current + (word.text + " "))); }
public JsonResult GetAwaitList() { using (OcrModel db = new OcrModel()) { var list = db.OcrMaps.Where(_ => _.isMapped == false).ToList <OcrMap>(); return(Json(list, JsonRequestBehavior.AllowGet)); } }
public JsonResult SaveAwait(OcrMap map) { using (OcrModel db = new OcrModel()) { OcrMap temp = db.OcrMaps.Find(map.id); if (temp != null) { temp.isMapped = true; temp.ocr_map = map.ocr_map; db.SaveChanges(); } } return(Json(new { msg = "saved" })); }
private string GetBanknoteDenomination(OcrModel ocrModel) { string result = string.Empty; foreach (var reg in ocrModel.regions) { foreach (var line in reg.lines) { foreach (var word in line.words) { result += word.text + " "; } } } return(result); }
public String DoOCR([FromForm] OcrModel request) { var result = new OcrService().GetLicenseNumber(request.Image, request.DestinationLanguage); return(String.IsNullOrWhiteSpace(result) ? "Ocr is finished. Return empty" : result); }
async Task DisplayImageOption() { var option = await DisplayActionSheet("Add Image", "Cancel", null, "Take a photo"); if (option != null) { string Filename = ""; Filename = Guid.NewGuid().ToString(); Filename = Filename.Substring(0, 6); switch (option.ToString()) { case "Take a photo": try { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("No Camera", ":( No camera available.", "OK"); return; } else { var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Sample", Name = "test.jpg", SaveToAlbum = true, }); if (file == null) { return; } if (imgTemplateImages != null) { lblImagePath.Text = "ImagePath" + file.Path; byte[] testByte = CommonFunctions.ReadStream(file.GetStream()); //////////////////////////// var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "de7609da413944308cde16098d1097f0"); var uri = "https://centralus.api.cognitive.microsoft.com/vision/v2.0/ocr" + queryString; HttpResponseMessage response; // Request body // byte[] byteData = Encoding.UTF8.GetBytes("{testByte}"); using (var content = new ByteArrayContent(testByte)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); var contents = response.Content.ReadAsStringAsync().Result; Debug.WriteLine("OCR response " + contents); } var responseResult = await response.Content.ReadAsStringAsync(); ocrModel = JsonConvert.DeserializeObject <OcrModel>(responseResult); var wordCount = Convert.ToString(ocrModel.regions[0].lines[0].words.Count); StringBuilder numberList = new StringBuilder(); for (int i = 0; i < ocrModel.regions[0].lines[0].words.Count; i++) { numberList.Append(ocrModel.regions[0].lines[0].words[i].text); numberList.Append(""); } await DisplayAlert("Alert", "Car Numebr :" + numberList.ToString(), "Ok"); Debug.WriteLine("Car Numebr " + numberList.ToString()); ////////////////////////////// imgTemplateImages.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); } } } catch (Exception ex) { await DisplayAlert("Alert", " " + ex.StackTrace, "Ok"); } break; default: await DisplayAlert("Alert", "Something Went Wrong", "Ok"); break; } } }
public IActionResult Index() { var model = new OcrModel(); return(View(model)); }
private async void OcrMethodCall() { ScanImage.SetValue(IsVisibleProperty, true); try { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("No Camera", ":( No camera available.", "OK"); return; } else { var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Sample", Name = "test.jpg", SaveToAlbum = true, }); if (file == null) { return; } if (ScanImage != null) { //lblImagePath.Text = "ImagePath" + file.Path; byte[] testByte = CommonFunctions.ReadStream(file.GetStream()); //////////////////////////// var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "de7609da413944308cde16098d1097f0"); var uri = "https://centralus.api.cognitive.microsoft.com/vision/v2.0/ocr" + queryString; HttpResponseMessage response; // Request body // byte[] byteData = Encoding.UTF8.GetBytes("{testByte}"); using (var content = new ByteArrayContent(testByte)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); var contents = response.Content.ReadAsStringAsync().Result; Debug.WriteLine("OCR response " + contents); } var responseResult = await response.Content.ReadAsStringAsync(); ocrModel = JsonConvert.DeserializeObject <OcrModel>(responseResult); var wordCount = Convert.ToString(ocrModel.regions[0].lines[0].words.Count); StringBuilder numberList = new StringBuilder(); for (int i = 0; i < ocrModel.regions[0].lines[0].words.Count; i++) { numberList.Append(ocrModel.regions[0].lines[0].words[i].text); numberList.Append(" "); } VinEnteredNumber.Text = numberList.ToString(); Debug.WriteLine("Car Numebr " + numberList.ToString()); ////////////////////////////// ScanImage.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); // mainSTack.IsVisible = false; } } } catch (Exception ex) { await DisplayAlert("Alert", " " + ex, "OK"); } }
public async Task <JsonResult> uploadFile(HttpPostedFileBase file) { /* MemoryStream target = new MemoryStream(); * file.InputStream.CopyTo(target); * byte[] imageData = target.ToArray(); * string resultTextString = ""; * string error = ""; * try * { * HttpClient httpClient = new HttpClient(); * httpClient.Timeout = new TimeSpan(1, 1, 1); * //Removed the api key from headers * //httpClient.DefaultRequestHeaders.TryAddWithoutValidation("apikey", "5a64d478-9c89-43d8-88e3-c65de9999580"); * * MultipartFormDataContent form = new MultipartFormDataContent(); * //5a64d478-9c89-43d8-88e3-c65de9999580 * form.Add(new StringContent("helloworld"), "apikey"); //The "helloworld" key works, but it has a very low rate limit. You can get your won free api key at https://ocr.space/OCRAPI * form.Add(new StringContent("eng"), "language"); * * form.Add(new ByteArrayContent(imageData, 0, imageData.Length), "image", GetContentType(file.ContentType)); * * //else if (string.IsNullOrEmpty(PdfPath) == false) * //{ * // byte[] imageData = File.ReadAllBytes(PdfPath); * // form.Add(new ByteArrayContent(imageData, 0, imageData.Length), "PDF", "pdf.pdf"); * //} * * HttpResponseMessage response = await httpClient.PostAsync("https://api.ocr.space/Parse/Image", form); * * string strContent = await response.Content.ReadAsStringAsync(); * * * * Rootobject ocrResult = JsonConvert.DeserializeObject<Rootobject>(strContent); * * * if (ocrResult.OCRExitCode == 1) * { * for (int i = 0; i < ocrResult.ParsedResults.Count(); i++) * { * resultTextString += ocrResult.ParsedResults[i].ParsedText; * } * } * else * { * error = strContent; * * } * * * * * } * catch (Exception exception) * { * error = exception.Message; * } * if (string.IsNullOrEmpty(error)) * { * resultTextString = resultTextString.Replace("\\r", "").Replace("\\n", ""); * * }*/ using (OcrModel db = new OcrModel()) { return(Json(db.OcrMaps.ToList()[0])); } }