/// <summary> /// Submit the receipt understanding job to the Forms Recognizer Cognitive Service /// The service will return the endpoint (operation-location) to monitor for the result /// </summary> /// <param name="message"></param> /// <param name="stoppingToken"></param> /// <returns></returns> private async Task ProcessMessage(CloudQueueMessage message, CancellationToken stoppingToken) { Receipt receiptRequest = JsonSerializer.Deserialize <Receipt>(message.AsString); if (receiptRequest != null) { var ocrRequest = new OCRRequest() { Source = receiptRequest.Url }; using var content = new StringContent(JsonSerializer.Serialize(ocrRequest), Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync($"{_configuration["FormRecognizerEndpoint"]}analyze", content); if (!response.IsSuccessStatusCode) { var serviceError = JsonSerializer.Deserialize <OCRError>(await response.Content.ReadAsStringAsync()); _logger.LogError($"OCR Request status code: {response.StatusCode} : Error Code: {serviceError.error.code}, Error Message: {serviceError.error.message}"); } else { var requestUrl = ((string[])response.Headers.FirstOrDefault(n => n.Key.Equals("Operation-Location")).Value)[0]; receiptRequest.OCRRequestUrl = requestUrl; await PollForCompletion(receiptRequest, stoppingToken); } } }
/// <summary> /// 调用百度OCR小票识别服务 返回原始数据 /// </summary> /// <returns></returns> public static Result BaiDuReceiptOCR(OCRRequest oCRRequest) { try { var value = ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:AllMallOCRRule"); var hour = double.Parse(value); List <MallOCRRule> MallRuleList = CacheHandle <List <MallOCRRule> >( Key: "AllMallOCRRule", Hour: int.Parse(ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:AllMallOCRRule")), sqlWhere: ""); var MallRuleModel = MallRuleList.Where(s => s.MallID == Guid.Parse(oCRRequest.mallId)).FirstOrDefault(); if (MallRuleModel == null) { return(new Result(false, "请定义Mall的OCR规则", null)); } var result = HttpHelper.HttpPost(string.Format(MallRuleModel.OCRServerURL, MallRuleModel.OCRServerToken), $"image={HttpUtility.UrlEncode(oCRRequest.base64)}"); if (result.Contains("error_msg")) { var OCRErrorModel = JsonConvert.DeserializeObject <OCRErrorResult>(result); return(new Result(false, OCRErrorModel.error_msg, null)); } else { return(new Result(true, "", result)); } } catch (Exception ex) { Log.Error("BaiDuReceiptOCR", ex); return(new Result(false, ex.Message, null)); } }
public object Any(OCRRequest request) { OCRResponse myResponse = new OCRResponse(); if (request.Base64UploadFile == null || string.IsNullOrWhiteSpace(request.Base64UploadFile.ToString())) { myResponse.jsonResponse = "There was no file attached or no file name was specified."; return(myResponse); } try { // get the image data and save to local storage string base64ImgStr = request.Base64UploadFile.ToString().Split(',')[1]; byte[] tmpBytes = Convert.FromBase64String(base64ImgStr); Image image = (Bitmap)((new ImageConverter()).ConvertFrom(tmpBytes)); // now do the OCR junk here var ocr = new Tesseract(); ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.,/"); ocr.Init(@"C:\source\innovation\Content\tessdata", "eng", false); var resizedImage = (Bitmap)Resize(image, (3000), (3000), false); resizedImage.SetResolution(300, 300); var blackAndWhiteImage = BlackAndWhite(resizedImage, new Rectangle(0, 0, resizedImage.Width, resizedImage.Height)); var result = ocr.DoOCR(blackAndWhiteImage, Rectangle.Empty); OCRRawDataModel dataItems = new OCRRawDataModel(); dataItems.DataList = new List <OCRRawDataModel.RawDataItem>(); foreach (Word word in result) { var item = new OCRRawDataModel.RawDataItem(); item.Value = word.Text; item.Confidence = (int)word.Confidence; item.LineIndex = word.LineIndex; dataItems.DataList.Add(item); } var mapper = new IdentificationCardMapper(); var mappedObjects = mapper.MapDriversLicenseData(dataItems); myResponse.jsonResponse = JsonConvert.SerializeObject(mappedObjects); // cleanup image.Dispose(); resizedImage.Dispose(); ocr.Dispose(); } catch (Exception ex) { // i don't know what to do here... myResponse.jsonResponse = ex.Message; } return(myResponse); }
/// <summary> /// 图片识别 /// </summary> /// <param name="oCRRequest"></param> /// <returns>识别结果</returns> public static async Task <Result> ReceiptOCR(OCRRequest oCRRequest) { return(await Task.Run(() => { var OcrResult = BaiDuReceiptOCR(oCRRequest); if (OcrResult.Success) { return RecognitOCRResult(OcrResult.Data.ToString()); } else { return OcrResult; } })); }
/// <summary> /// 获取OCR分析结果 /// </summary> /// <returns></returns> public string GetOCRResultByToken(byte[] image, string fileName, string taskId = "") { string result = ""; try { string token = ""; string urlStr = ""; try { string loginInOutInfos = string.Format(@"{0}\LoginInOutInfo.xml", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\WordAndImgOCR\\LoginInOutInfo\\"); var ui = CheckWordUtil.DataParse.ReadFromXmlPath <string>(loginInOutInfos); if (ui != null && ui.ToString() != "") { try { var loginInOutInfo = JsonConvert.DeserializeObject <LoginInOutInfo>(ui.ToString()); if (loginInOutInfo != null && loginInOutInfo.Type == "LoginIn") { token = loginInOutInfo.Token; urlStr = loginInOutInfo.UrlStr; } } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } } } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } string url = urlStr + "ocr"; OCRRequest ocrRequest = new OCRRequest(); ocrRequest.image = System.Convert.ToBase64String(image); ocrRequest.fileName = fileName; ocrRequest.taskId = taskId; string json = JsonConvert.SerializeObject(ocrRequest); result = HttpHelper.HttpUrlSend(url, "POST", json, token); } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } return(result); }
/// <summary> /// 获取OCR分析结果 /// </summary> /// <param name="token"></param> /// <returns></returns> public string GetOCRResultByToken(string token, byte[] image) { string result = ""; try { string apiName = "ocr"; OCRRequest ocrRequest = new OCRRequest(); ocrRequest.image = System.Convert.ToBase64String(image); string json = JsonConvert.SerializeObject(ocrRequest); result = HttpHelper.HttpUrlSend(apiName, "POST", json, token); } catch (Exception ex) { WPFClientCheckWordUtil.Log.TextLog.SaveError(ex.Message); } return(result); }