private void ButtonCutPic_Click(object sender, EventArgs e) { this.Visible = false; System.Threading.Thread.Sleep(200); ShowCutPic(); try { if (Clipboard.ContainsImage()) { ButtonPart.BackgroundImage = 自动分段ToolStripMenuItem.Image; toolTip1.SetToolTip(ButtonPart, "自动分段"); Image img = Clipboard.GetImage(); RichTextBoxValue.Text = OCRHelper.BaiduBasic("", img); this.Visible = true; } else { this.Visible = true; } } catch (Exception ex) { this.Visible = true; MessageBox.Show(this, ex.Message, "错误"); } }
static async void GetData(string WeekDay) { List <string> weekDayNames = new List <string> { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; string _weekDay = String.Empty; if (!String.IsNullOrEmpty(WeekDay)) { if (!weekDayNames.Contains(WeekDay.ToUpper())) { WeekDay = null; } } if (String.IsNullOrEmpty(WeekDay)) { DateTime today = DateTime.Now; _weekDay = today.AddDays(1).DayOfWeek.ToString().ToUpper(); if ((_weekDay.Equals("SATURDAY") || (_weekDay.Equals("SUNDAY")))) { _weekDay = "MONDAY"; } } else { _weekDay = WeekDay; } OCRHelper ocr = new OCRHelper(); string dayMenu = await ocr.GetMenuByDay(_weekDay.Substring(0, 3)); Console.WriteLine(dayMenu); }
private void ButtonApiTest_Click(object sender, EventArgs e) { try { //string url = "https://aip.baidubce.com/oauth/2.0/token"; //string data = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}", TextBoxApiKey.Text, TextBoxSecretKey.Text); //string result = HttpClient.Post(data, url); //BaiduToken token = JsonConvert.DeserializeObject<BaiduToken>(result); string token = OCRHelper.GetBaiduToken(TextBoxApiKey.Text, TextBoxSecretKey.Text); if (token.Contains("错误")) { MessageBox.Show(this, token, "错误"); } else { TextBoxToken.Text = token; IniHelper.SetValue("百度接口", "API Key", TextBoxApiKey.Text.Trim()); IniHelper.SetValue("百度接口", "Secret Key", TextBoxSecretKey.Text.Trim()); IniHelper.SetValue("百度接口", "Access Token", TextBoxToken.Text.Trim()); IniHelper.SetValue("百度接口", "Date Token", DateTime.Now.ToString("yyyy-MM-dd")); MessageBox.Show(this, "已生成并保存密钥,有效期30天!", "提示"); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } }
public string SaveFileOCRFormat() { OCRHelper ocrHelper = new OCRHelper(); ocrHelper.OCR(false); return("abc"); }
private void Btn_orc_Click(object sender, EventArgs e) { var text = OCRHelper.Ocr((Bitmap)this.picBox.Image); Console.WriteLine(text); MessageBox.Show(text); }
private void ButtonCutPic_Click(object sender, EventArgs e) { if (Setting.FormHide) { this.Visible = false; // 截图时隐藏本窗体 System.Threading.Thread.Sleep(200); // 延时,避免把本窗体也截下来 } ShowCutPic(); this.Visible = true; this.WindowState = FormWindowState.Normal; this.Activate(); Application.DoEvents(); // DoEvents()将强制处理消息队列中的所有消息 try { if (Clipboard.ContainsImage()) { ButtonPart.Text = "自动分段"; Image img = Clipboard.GetImage(); // 获取剪切板图片 RichTextBoxValue.Text = OCRHelper.BaiduBasic("", img).Trim(); // 识别剪切板图片的文字 if (Setting.TextCopy) { ButtonCopy_Click(null, null); } } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } }
private void ResultOutput(string path, Image img = null) { if (img == null) { OCRHelper.BaiduBasic(path); } else { OCRHelper.BaiduBasic("", img); } // 段落 switch (Setting.Paragraph) { case 1: RichTextBoxValue.Text = OCRHelper.typeset_txt.Trim(); break; case 2: RichTextBoxValue.Text = OCRHelper.split_txt.Trim(); break; case 3: RichTextBoxValue.Text = OCRHelper.typeset_txt.Trim().Replace("\n", "").Replace("\r", ""); break; } // 识别后自动复制 if (Setting.TextCopy) { ButtonCopy_Click(null, null); } }
private void Btn_jtyReg_Click(object sender, EventArgs e) { var text = OCRHelper.OcrJty(this.picSrc.GetFirstRegionRect()); this.tb_ocrResult.Text = text; //OcrInvoke SVM sVM = new SVM(); }
private void ButtonFile_Click(object sender, EventArgs e) { openFileDialog1.Filter = "图片文件 (*.jpg,*.jpeg,*.png,*.bmp)|*.jgp;*.jpeg;*.png;*.bmp;"; //设置多文件格式 if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { ButtonPart.BackgroundImage = 自动分段ToolStripMenuItem.Image; RichTextBoxValue.Text = OCRHelper.BaiduBasic(openFileDialog1.FileName); } }
static void Main(string[] args) { // List<List<int>> data = new List<List<int>>(); // ProcessData(data); //Method2(); Extrarctor extractor = new Extrarctor(); extractor.api(); Parking.ParkingPhysicalCashLogic(); var test = DateTime.ParseExact("16.07.19 04:31", "dd.MM.yy HH:mm", CultureInfo.InvariantCulture); var atmRecieptMetaData = new ATMRecieptMetaDataFactory().CreateMetaData(); var ocrRequest = OCRHelper.BuildRequest(null); var temp = OCRHelper.GetOCRResult(ocrRequest).Result; var textAnnotations = temp.responses.FirstOrDefault()?.textAnnotations; var resultText = ATMReciepts.ExtractATMReciept(atmRecieptMetaData, textAnnotations); //var resultText = ATMReciepts.ExtractATMReciept(textAnnotations); Parking.ParkingLogic(); Console.ReadKey(); using (var disposeTest = new TestDisposable()) { } ExpressionExtensions.ToPostfixString(x => Math.Sin(1 + 2 * x)); int n = 1; int a = 0; String m; for (string m2; n > 0;) { var temp2 = Enumerable.Range(0, ++a); foreach (var x in temp2) { var temp3 = (m = x + "" + (a - x) + x); var revers = temp3.Reverse(); } } if (Enumerable.Range(0, ++a).All(x => !(m = x + "" + (a - x) + x).Reverse().SequenceEqual(m))) { n--; } Console.WriteLine(a); }
private void ResultOutput(int type, string path, Image img = null) { if (Setting.IsOffline) { RichTextBoxValue.Text = OCROffline.OCROffile(path, img); } else { // 文字识别 if (type == 1) { if (img == null) { OCRHelper.BaiduBasic(path); } else { OCRHelper.BaiduBasic("", img); } // 段落 switch (Setting.Paragraph) { case 1: RichTextBoxValue.Text = OCRHelper.typeset_txt.Trim(); break; case 2: RichTextBoxValue.Text = OCRHelper.split_txt.Trim(); break; case 3: RichTextBoxValue.Text = OCRHelper.typeset_txt.Trim().Replace("\n", "").Replace("\r", ""); break; } } else // 表格识别 { if (img == null) { RichTextBoxValue.Text = OCRHelper.BaiduForm(path); } else { RichTextBoxValue.Text = OCRHelper.BaiduForm("", img); } } } // 识别后自动复制 if (Setting.TextCopy) { ButtonCopy_Click(null, null); } }
private void FormMain_Load(object sender, EventArgs e) { // RichTextBox 段落缩进 RichTextBoxValue.SelectionIndent = 40; RichTextBoxValue.SelectionHangingIndent = -35; // RichTextBox 拖放事件绑定 RichTextBoxValue.AllowDrop = true; RichTextBoxValue.DragEnter += new DragEventHandler(FormMain_DragEnter); RichTextBoxValue.DragDrop += new DragEventHandler(FormMain_DragDrop); // 读取 ini 配置 IniHelper.IniLoad("Setting.ini"); OCRHelper.ApiKey = IniHelper.GetValue("百度接口", "API Key"); OCRHelper.SecretKey = IniHelper.GetValue("百度接口", "Secret Key"); OCRHelper.AccessToken = IniHelper.GetValue("百度接口", "Access Token"); string check = IniHelper.GetValue("百度接口", "使用高精度接口"); OCRHelper.Accurate = check == "" ? false : bool.Parse(check); // 判断 token 是否过期 OCRHelper.DateToken = IniHelper.GetValue("百度接口", "Date Token"); TimeSpan day = DateTime.Now - DateTime.Parse(OCRHelper.DateToken); if (day.Days >= 30 && OCRHelper.ApiKey != "" && OCRHelper.SecretKey != "") { try { string token = OCRHelper.GetBaiduToken(OCRHelper.ApiKey, OCRHelper.SecretKey); if (token.Contains("错误")) { MessageBox.Show(this, token, "错误"); } else { IniHelper.SetValue("百度接口", "Access Token", token); IniHelper.SetValue("百度接口", "Date Token", DateTime.Now.ToString("yyyy-MM-dd")); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } } // 注册热键 string value = IniHelper.GetValue("热键", "截图识别"); if (value != "" && value != "请按下快捷键") { HotKey.SetHotkey(Handle, "None", "F4", value, 200); } }
private void ButtonFile_Click(object sender, EventArgs e) { openFileDialog1.Filter = "图片文件 (*.jpg,*.jpeg,*.png,*.bmp)|*.jgp;*.jpeg;*.png;*.bmp;"; //设置多文件格式 if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { ButtonPart.Text = "自动分段"; RichTextBoxValue.Text = OCRHelper.BaiduBasic(openFileDialog1.FileName).Trim(); if (Setting.TextCopy) { ButtonCopy_Click(null, null); } } }
private void FormMain_DragDrop(object sender, DragEventArgs e) { try { ButtonPart.BackgroundImage = 自动分段ToolStripMenuItem.Image; string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); RichTextBoxValue.Text = OCRHelper.BaiduBasic(path); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } }
private void FormMain_DragDrop(object sender, DragEventArgs e) { try { ButtonPart.Text = "自动分段"; string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); RichTextBoxValue.Text = OCRHelper.BaiduBasic(path).Trim(); if (Setting.TextCopy) { ButtonCopy_Click(null, null); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } }
private async void ExecuteBeginOcrCommand(object obj) { if (SelectedIndex >= 0) { ProgressOCRVisibility = Visibility.Visible; OcrString = string.Empty; OcrString = await OCRHelper.ScanImageOCR(ImageCollection.ListWriteImages, ProcessPages, SelectedIndex, SelectedLanguage); if (OcrString.Contains("Supported image dimensions are between 40 and 2600 pixels.")) { TextblockForeground.Color = Colors.Red; } else { TextblockForeground.Color = Colors.Black; } ProgressOCRVisibility = Visibility.Collapsed; } }
private void ButtonCutPic_Click(object sender, EventArgs e) { this.Visible = false; // 截图时隐藏本窗体 System.Threading.Thread.Sleep(200); // 延时,避免把本窗体也截下来 ShowCutPic(); // 截图功能 this.Visible = true; Application.DoEvents(); // DoEvents()将强制处理消息队列中的所有消息 try { if (Clipboard.ContainsImage()) { ButtonPart.BackgroundImage = 自动分段ToolStripMenuItem.Image; toolTip1.SetToolTip(ButtonPart, "自动分段"); Image img = Clipboard.GetImage(); // 获取剪切板图片 RichTextBoxValue.Text = OCRHelper.BaiduBasic("", img); // 识别剪切板图片的文字 } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } }
public async System.Threading.Tasks.Task <string> GetAsync(string WeekDay) { List <string> weekDayNames = new List <string> { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; string _weekDay = String.Empty; if (!String.IsNullOrEmpty(WeekDay)) { if (!weekDayNames.Contains(WeekDay.ToUpper())) { WeekDay = null; } } if (String.IsNullOrEmpty(WeekDay)) { DateTime today = DateTime.Now; _weekDay = today.AddDays(1).DayOfWeek.ToString().ToUpper(); if ((_weekDay.Equals("SATURDAY") || (_weekDay.Equals("SUNDAY")))) { _weekDay = "MONDAY"; } } else { _weekDay = WeekDay; } OCRHelper ocr = new OCRHelper(); menuThread = new Thread(new ThreadStart(ocr.ResolveMenu)); menuThread.IsBackground = true; menuThread.Start(); string dayMenu = await ocr.GetMenuByDay(_weekDay.Substring(0, 3)); return(dayMenu); }
private void FormMain_Load(object sender, EventArgs e) { // RichTextBox 段落缩进 RichTextBoxValue.SelectionIndent = 40; RichTextBoxValue.SelectionHangingIndent = -35; // RichTextBox 拖放事件绑定 RichTextBoxValue.AllowDrop = true; RichTextBoxValue.DragEnter += new DragEventHandler(FormMain_DragEnter); RichTextBoxValue.DragDrop += new DragEventHandler(FormMain_DragDrop); // 读取 ini 配置 IniHelper.IniLoad("Setting.ini"); Setting.TextPlus = IniHelper.GetValue("常规", "识别后文本累加") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "识别后文本累加")); Setting.TextCopy = IniHelper.GetValue("常规", "识别后自动复制") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "识别后自动复制")); Setting.FormHide = IniHelper.GetValue("常规", "截图时隐藏窗体") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "截图时隐藏窗体")); Setting.FormTray = IniHelper.GetValue("常规", "右下角显示托盘") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "右下角显示托盘")); this.Notify.Visible = Setting.FormTray; Setting.SelfStart = IniHelper.GetValue("常规", "开机自启") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "开机自启")); OCRHelper.ApiKey = IniHelper.GetValue("百度接口", "API Key"); OCRHelper.SecretKey = IniHelper.GetValue("百度接口", "Secret Key"); OCRHelper.AccessToken = IniHelper.GetValue("百度接口", "Access Token"); OCRHelper.Accurate = IniHelper.GetValue("百度接口", "使用高精度接口") == "" ? false : bool.Parse(IniHelper.GetValue("百度接口", "使用高精度接口")); // 判断 token 是否过期 OCRHelper.DateToken = IniHelper.GetValue("百度接口", "Date Token"); TimeSpan day = OCRHelper.DateToken == "" ? TimeSpan.MaxValue : DateTime.Now - DateTime.Parse(OCRHelper.DateToken); if (day.Days >= 30 && OCRHelper.ApiKey != "" && OCRHelper.SecretKey != "") { try { string token = OCRHelper.GetBaiduToken(OCRHelper.ApiKey, OCRHelper.SecretKey); if (token.Contains("错误")) { MessageBox.Show(this, token, "错误"); } else { OCRHelper.AccessToken = token; IniHelper.SetValue("百度接口", "Access Token", token); IniHelper.SetValue("百度接口", "Date Token", DateTime.Now.ToString("yyyy-MM-dd")); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "错误"); } } Setting.TranItem = IniHelper.GetValue("翻译", "默认网址") == "" ? 0 : int.Parse(IniHelper.GetValue("翻译", "默认网址")); Setting.TranOption = IniHelper.GetValue("翻译", "翻译选项") == "" ? 0 : int.Parse(IniHelper.GetValue("翻译", "翻译选项")); // 注册热键 Setting.HotkeyCut = IniHelper.GetValue("热键", "截图识别"); if (Setting.HotkeyCut != "" && Setting.HotkeyCut != "请按下快捷键") { HotKey.SetHotkey(Handle, "None", "F4", Setting.HotkeyCut, 200); } Setting.HotkeyShow = IniHelper.GetValue("热键", "显示/隐藏"); if (Setting.HotkeyShow != "" && Setting.HotkeyShow != "请按下快捷键") { HotKey.SetHotkey(Handle, "None", "F4", Setting.HotkeyShow, 201); } }
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) { if (turnContext.Activity.Type == ActivityTypes.Message) { ConnectorClient connector = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl)); var activity = turnContext.Activity; var reply = activity.CreateReply(); var imageAttachment = turnContext.Activity.Attachments?.FirstOrDefault(a => a.ContentType.Contains("image")); if (imageAttachment != null) { //Show typing activity to User await UIHelper.SendTypingMessage(turnContext); Task.Run(async() => { var ocrresult = await OCRHelper.GetCaptionAsync(turnContext.Activity, connector, _subscriptionKey, _uriEndPoint); var ocroutputtext = String.Join(" ", ocrresult.ToArray()); try { // Create Adaptive Card for confirmation view string jsonCardPath = @"Dialogs\Main\Resources\ConfirmMail.json"; var cardAttachment = await UIHelper.CreateAdaptiveCardUserInfo(jsonCardPath, turnContext, ocrresult); reply = turnContext.Activity.CreateReply(); reply.Attachments = new List <Attachment>() { cardAttachment }; await turnContext.SendActivityAsync(reply, cancellationToken).ConfigureAwait(false);; } catch { await turnContext.SendActivityAsync("An error occured during OCR parsing", null, null, cancellationToken); } }).Wait(); } else if (string.IsNullOrEmpty(turnContext.Activity.Text)) { // Check response comes from Adaptive Card Actions dynamic value = turnContext.Activity.Value; string text = value["adaptiveResponse"]; // The property will be named after your text input's ID text = string.IsNullOrEmpty(text) ? "." : text; // In case the text input is empty reply = turnContext.Activity.CreateReply(); reply.Text = text; await turnContext.SendActivityAsync(reply, cancellationToken).ConfigureAwait(false);; } } else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate) { // Check for user first visit if (turnContext.Activity.MembersAdded != null) { foreach (var member in turnContext.Activity.MembersAdded) { if (member.Id != turnContext.Activity.Recipient.Id) { await turnContext.SendActivityAsync("Welcome to Form Extract Bot. I'm here to help you!"); await UIHelper.SendSuggestedActionsAsync(turnContext, cancellationToken); } } } } }
public static void ParkingPhysicalCashLogic() { var possibleKeys = new String[] { "MACHINE ID", "SEQUENCE NO", "EUR" }; string dateRegex = @"([0-2][0-9]|(3)[0-1])(\.)(((0)[0-9])|((1)[0-2]))(\.)\d{2,4} "; string timeRegex = @"([01]\d|2[0-3]):([0-5]\d)"; var ocrRequest = OCRHelper.BuildRequest(null); var temp = OCRHelper.GetOCRResult(ocrRequest).Result; var textAnnotations = temp.responses.FirstOrDefault().textAnnotations; ParkingPhysicalCashData physicalCashData = new ParkingPhysicalCashData(); List <ParkingPhysicalCashElement> parkingElements = new List <ParkingPhysicalCashElement>(); int index = 1; while (index < textAnnotations.Count) { var textBlock = textAnnotations[index]; var y1 = textBlock.boundingPoly.vertices[0].y; var y2 = textBlock.boundingPoly.vertices[2].y; var x1 = textBlock.boundingPoly.vertices[1].x; var x2 = textBlock.boundingPoly.vertices[3].x; var matchingY = textAnnotations.Where(textB => (textB.boundingPoly.vertices[0].y == y1 && textB.boundingPoly.vertices[2].y == y2) || (Math.Abs(textB.boundingPoly.vertices[0].y - y1) <= 25 && Math.Abs(textB.boundingPoly.vertices[2].y - y2) <= 25)) .OrderBy(x => x.boundingPoly.vertices[0].x); var matchingX = textAnnotations.Where(textB => ( (textB.boundingPoly.vertices[1].x == x1 && textB.boundingPoly.vertices[3].x == x2) || (Math.Abs(textB.boundingPoly.vertices[1].x - x1) <= 100 && Math.Abs(textB.boundingPoly.vertices[3].x - x2) <= 100) ) && textB.boundingPoly.vertices[0].y > y1 && textB.boundingPoly.vertices[2].y > y2) .OrderBy(x => x.boundingPoly.vertices[1].y); //var labelsToFind = new String[] { "MACHINEID:", "Date:" }; //var labelsToFind = typeof(ParkingPhysicalCashLabels) // .GetFields() // .Select(f => f.GetCustomAttributes(typeof(DescriptionAttribute), false).First()) // .Cast<DescriptionAttribute>() // .Where(x => String.Equals(x.Description, ParkingPhysicalCashLabels.Unknown.ToString(), // StringComparison.OrdinalIgnoreCase)) // .Select(x => x.Description).ToArray(); var labelsToFind = ParkingPhysicalCashLabels.Time.ToList(); if (matchingY.Any()) { var line = String.Join("", matchingY.Select(x => x.description)); var labelsFound = labelsToFind.Where(x => line.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0) .Select(x => new { Label = x, Index = line.IndexOf(x, StringComparison.OrdinalIgnoreCase) }).ToArray(); for (int i = 0; i < labelsFound.Length; i++) { var labelEnum = labelsFound[i].Label.TryFromEnumStringValue <ParkingPhysicalCashLabels>(); var labelValue = line.Substring(labelsFound[i].Index + labelsFound[i].Label.Length, (i < labelsFound.Length - 1) ? (labelsFound[i + 1].Index - labelsFound[i].Label.Length) : line.Length - (labelsFound[i].Index + labelsFound[i].Label.Length)); if (labelEnum != ParkingPhysicalCashLabels.Unknown) { ParkingPhysicalCashElement parking = new ParkingPhysicalCashElement(); parking.Label = labelEnum; parking.Value = labelValue; if (parkingElements.All(x => x.Label != parking.Label)) { parkingElements.Add(parking); } } } } index++; //} } physicalCashData.RecieptDetails = parkingElements; }
public static void ParkingLogic() { var possibleKeys = new String[] { "MACHINE ID", "SEQUENCE NO", "EUR" }; string dateRegex = @"([0-2][0-9]|(3)[0-1])(\.)(((0)[0-9])|((1)[0-2]))(\.)\d{2,4} "; string timeRegex = @"([01]\d|2[0-3]):([0-5]\d)"; var ocrRequest = OCRHelper.BuildRequest(null); var temp = OCRHelper.GetOCRResult(ocrRequest).Result; var textAnnotations = temp.responses.FirstOrDefault().textAnnotations; ParkingData parkingData = new ParkingData(); #region Date & Time Extraction var wholeTextArray = textAnnotations[0].description.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in wholeTextArray) { if (String.IsNullOrEmpty(parkingData.MachineID) && item.StartsWith("-")) { parkingData.MachineID = item.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); } if (String.IsNullOrEmpty(parkingData.Date)) { parkingData.Date = Regex.Match(item, dateRegex).Value; } if (String.IsNullOrEmpty(parkingData.Time)) { parkingData.Time = Regex.Match(item, timeRegex).Value; } if (String.IsNullOrEmpty(parkingData.SequenceNo) && item.ToUpper().Contains("SEQUENCE NO")) { parkingData.SequenceNo = item.ToUpper().Split(new[] { "SEQUENCE NO" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault()?.Trim('.'); } if (!String.IsNullOrEmpty(parkingData.MachineID) && !String.IsNullOrEmpty(parkingData.SequenceNo) && !String.IsNullOrEmpty(parkingData.Date) && !String.IsNullOrEmpty(parkingData.Time) ) { break; } } #endregion List <ParkingElement> parkingElements = new List <ParkingElement>(); int index = 1; while (index < textAnnotations.Count) { var textBlock = textAnnotations[index]; var y1 = textBlock.boundingPoly.vertices[0].y; var y2 = textBlock.boundingPoly.vertices[2].y; var x1 = textBlock.boundingPoly.vertices[1].x; var x2 = textBlock.boundingPoly.vertices[3].x; var matchingY = textAnnotations.Where(textB => (textB.boundingPoly.vertices[0].y == y1 && textB.boundingPoly.vertices[2].y == y2) || (Math.Abs(textB.boundingPoly.vertices[0].y - y1) <= 25 && Math.Abs(textB.boundingPoly.vertices[2].y - y2) <= 25)) .OrderBy(x => x.boundingPoly.vertices[0].x); var matchingX = textAnnotations.Where(textB => ( (textB.boundingPoly.vertices[1].x == x1 && textB.boundingPoly.vertices[3].x == x2) || (Math.Abs(textB.boundingPoly.vertices[1].x - x1) <= 100 && Math.Abs(textB.boundingPoly.vertices[3].x - x2) <= 100) ) && textB.boundingPoly.vertices[0].y > y1 && textB.boundingPoly.vertices[2].y > y2) .OrderBy(x => x.boundingPoly.vertices[1].y); if (matchingX.Any()) { var cleaned = matchingX.Where(x => x.description != "-"); var matchingWithIndex = cleaned.Select((ax, i) => new { Index = i, Label = textBlock.description.TryFromEnumStringValue <ParkingLabels>(), TextAnnotation = ax }); var finalResult = matchingWithIndex.Where(x => x.Label != ParkingLabels.Unknown); if (finalResult.Any()) { int skipIndex = 0; while (skipIndex < 4) { var item = finalResult.Skip(skipIndex).FirstOrDefault(); //foreach (var item in matchingWithIndex) //{ if (item != null) { ParkingElement parking = new ParkingElement(); parking.Label = item.Label; if (item.Label == ParkingLabels.Count) { parking.Value = item.TextAnnotation.description; } else { parking.Value = String.Join("", finalResult.Skip(skipIndex * 3).Take(3).OrderBy(x => x.TextAnnotation.boundingPoly.vertices[0].x).Select(x => x.TextAnnotation.description)); } parkingElements.Add(parking); } skipIndex++; //} } } } index++; } parkingData.RecieptDetails = parkingElements; #region Denomination //var itemWithIndex = wholeTextArray.Select((x, i) => new { Index = i, Value = x }); //var seperatorLine = itemWithIndex.Where(x => x.Value.Contains("------")).FirstOrDefault(); //var denomination1 = itemWithIndex.Skip(seperatorLine.Index + 1).FirstOrDefault(); //var denomination2 = itemWithIndex.Skip(seperatorLine.Index + 2).FirstOrDefault(); //var denomination3 = itemWithIndex.Skip(seperatorLine.Index + 3).FirstOrDefault(); //var denomination4 = itemWithIndex.Skip(seperatorLine.Index + 4).FirstOrDefault(); //var count1 = itemWithIndex.Skip(seperatorLine.Index + 5).FirstOrDefault(); //var count2 = itemWithIndex.Skip(seperatorLine.Index + 6).FirstOrDefault(); //var count3 = itemWithIndex.Skip(seperatorLine.Index + 7).FirstOrDefault(); //var count4 = itemWithIndex.Skip(seperatorLine.Index + 8).FirstOrDefault(); ////int index = 1; ////while (index < textAnnotations.Count) ////{ //// var textBlock = textAnnotations[index]; //// textBlock = textAnnotations.Where(x => x.description.ToUpper().Contains("EUR")).LastOrDefault(); //// if(textBlock.description.ToUpper().Contains("EUR")) //// { //// var x1 = textBlock.boundingPoly.vertices[1].x; //// var x2 = textBlock.boundingPoly.vertices[2].x; //// var y1 = textBlock.boundingPoly.vertices[2].y; //// var y2 = textBlock.boundingPoly.vertices[3].y; //// var matchingX = textAnnotations.Skip(1).Where(textB => //// ((textB.boundingPoly.vertices[1].x == x1 && textB.boundingPoly.vertices[2].x == x2) || //// //(textB.boundingPoly.vertices[0].y < y1 && textB.boundingPoly.vertices[1].y < y2)) //// (x1 - Math.Abs(textBlock.boundingPoly.vertices[1].x) <= 5 && Math.Abs(x2 - textBlock.boundingPoly.vertices[2].x) <= 5)) && //// (y1 < textB.boundingPoly.vertices[2].y && y2 < textB.boundingPoly.vertices[3].y) //// ); //// //.OrderBy(x => x.boundingPoly.vertices[0].y); //// } //// index++; ////} #endregion }
public async Task <IActionResult> Post(IFormFile file) { List <string> restURL = new List <string>(); #region SavingFile _logger.LogInfo("Uploading file success - Started"); var filePath = string.Empty; var strActualFileName = Path.GetFileNameWithoutExtension(file.FileName); var fileExt = Path.GetExtension(file.FileName); var appendString = DateTime.Now.ToString("yyyyddmmHHssmmm"); string strUniquename = strActualFileName + "_" + appendString + "" + fileExt; var uploads = @"C:/AzaKawSampleKYC/KYCUploads"; if (!Directory.Exists(uploads)) { _logger.LogInfo("Creating Upload Directory - Start"); Directory.CreateDirectory(uploads); _logger.LogInfo("Creating Upload Directory - Done"); } if (file.Length > 0) { filePath = Path.Combine(uploads, strUniquename); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } } strActualFileName = strActualFileName + "" + fileExt; _logger.LogInfo("Uploading file success - Done"); #endregion #region ReadingMRZWithAbbyOCR _logger.LogInfo("Reading MRZ Inforation with Abby Cloud OCR - Started"); if (System.IO.File.Exists(@filePath)) { using (var ocrClient = OCRHelper.GetOcrClientWithRetryPolicy()) { restURL = await OCRHelper.ProcessImageAsync(ocrClient, @filePath); //new List<string>() { "https://ocrsdk.blob.core.windows.net/files/8ebe4855-1071-4ed0-95b6-de2334536a85.result?sv=2012-02-12&se=2019-11-02T21%3A00%3A00Z&sr=b&si=downloadResults&sig=VFoM9QfONhmpc4GjokEIEDDtIIDd9fRDY8t%2Bkvdu%2Fuo%3D" }; //await OCRHelper.ProcessImageAsync(ocrClient, @filePath); } } else { _logger.LogWarn("Could not find the uploaded file"); throw new ArgumentException($"Some thing went wrong, Please contact administrator (view logs)"); } _logger.LogInfo("Reading MRZ Inforation with Abby Cloud OCR - Done"); #endregion #region ReadingMRZOutput _logger.LogInfo("Reading MRZ Resulted Output - Start"); DataSet ds = new DataSet(); WebRequest request = WebRequest.Create(restURL[0].ToString()); Encoding encoding = Encoding.GetEncoding("iso-8859-1"); WebRequest requestXML = WebRequest.Create(restURL[0].ToString()); request.Method = "GET"; using (var response = (System.Net.HttpWebResponse)request.GetResponse()) { // get correct charset and encoding from the server's header Encoding encodingXML; try { encodingXML = Encoding.GetEncoding(response.CharacterSet); } catch { encodingXML = Encoding.UTF8; } using (var rdr = new StreamReader(response.GetResponseStream(), encoding)) { ds.ReadXml(rdr); } } MrzInfo objMRZ = OCRHelper.DeserializeMRZInfo(ds.Tables[1]); objMRZ.CustomerId = int.Parse(User.Claims.FirstOrDefault().Value); objMRZ.FileName = strActualFileName; objMRZ.FileNameUnique = strUniquename; _logger.LogInfo("Reading MRZ Resulted Output - Done"); #endregion #region KYCVerification _logger.LogInfo("Verifying User KYC Details with Trulioo - Start"); //Example Username: JoeNapoli_API_Demo, Example Password: 05uZuPRCyPi!6 bool isKYCVerified = await IsKYCVerified(); _logger.LogInfo("Verifying User KYC Details with Trulioo - End"); #endregion #region SavingMRZInfo _logger.LogInfo("Saving MRZ Info - Start"); objMRZ.isKYCVerified = isKYCVerified; objMRZ.AdditionalInfo = JsonConvert.SerializeObject(objMRZ); _uploadRepository.InsertMrzInfo(objMRZ); var allMRzInfo = _uploadRepository.GetMrzInfos(); _logger.LogInfo("Saving MRZ Info - End"); #endregion return(Ok(allMRzInfo)); }
// OCR Request by URL & Extract Key-Value Pairs public static async Task <List <string> > MakeOCRRequestByUrl(string imageUrl, string formType, ExecutionContext executionContext) { Console.Write("C# HTTP trigger function processed: MakeOCRRequestByUrl"); string urlBase = Environment.GetEnvironmentVariable("CognitiveServicesUrlBase"); string key = Environment.GetEnvironmentVariable("CognitiveServicesKey"); var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key); // Request parameters queryString["mode"] = "Printed"; Uri uri = new Uri(urlBase + "recognizeText?" + queryString); HttpResponseMessage response; var requstbody = "{\"url\":\"" + $"{imageUrl}" + "\"}"; // Request body byte[] byteData = Encoding.UTF8.GetBytes(requstbody); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); response = await client.PostAsync(uri, content); } string operationLocation = null; // The response contains the URI to retrieve the result of the process. if (response.IsSuccessStatusCode) { operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault(); } string contentString; int i = 0; do { System.Threading.Thread.Sleep(1000); response = await client.GetAsync(operationLocation); contentString = await response.Content.ReadAsStringAsync(); ++i; }while (i < 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1); string json = response.Content.ReadAsStringAsync().Result; json = json.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' }); RecognizeText ocrOutput = JsonConvert.DeserializeObject <RecognizeText>(json); if (ocrOutput != null && ocrOutput.RecognitionResult != null && ocrOutput.RecognitionResult.Lines != null) { List <string> resultText = new List <string>(); resultText = (from Line sline in ocrOutput.RecognitionResult.Lines select(string) sline.Text).ToList <string>(); // Extract Key-Value Pairs resultText = OCRHelper.ExtractKeyValuePairs(ocrOutput.RecognitionResult.Lines, formType, executionContext); return(resultText); } else { return(null); } }
private void Btn_ocr_Click(object sender, EventArgs e) { var text = OCRHelper.Ocr(this.picSrc.GetFirstRegionRect()); this.tb_ocrResult.Text = text; }