public string getTextFromImageFile(string filePath, Language selectedLanguage, string selectedMode) { string language = AspriseOCR.LANGUAGE_ENG; switch (selectedLanguage) { case Language.SPANISH: language = AspriseOCR.LANGUAGE_SPA; break; case Language.ENGLISH: language = AspriseOCR.LANGUAGE_ENG; break; case Language.GERMAN: language = AspriseOCR.LANGUAGE_DEU; break; case Language.FRENCH: language = AspriseOCR.LANGUAGE_FRA; break; default: language = AspriseOCR.LANGUAGE_ENG; break; } AspriseOCR.SetUp(); AspriseOCR ocr = new AspriseOCR(); ocr.StartEngine(language, AspriseOCR.SPEED_FASTEST); string s = ocr.Recognize(filePath, -1, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT); ocr.StopEngine(); return s; }
public async Task<List<string>> getWines(IFormFile file) { var fileName=ContentDispositionHeaderValue.Parse(file.ContentDisposition) .FileName .Trim('"'); var filePath=_hosting.WebRootPath+"\\Documents\\"+DateTime.Now.ToString("yyyyddMHHmmss")+fileName; file.SaveAs(filePath); List<string> platos=new List<string>(); AspriseOCR.SetUp(); client=new AspriseOCR(); client.StartEngine("eng",AspriseOCR.SPEED_SLOW); string text=client.Recognize(filePath, -1,-1,-1,-1,-1,AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT); string[] list=text.Split(new char[] {' '}); List<string> selected=await plato.LookFor(list); return selected; }
private void asprise_init() { bool browsed = false; int count = 0; string dllFilePath = AspriseOCR.getOcrDllPath(); if (dllFilePath == null) { string parentFolder = AspriseOCR.detectOcrDllInParentFolders(); if (parentFolder != null) { AspriseOCR.addToSystemPath(parentFolder); log("Folder containing ocr dll detected: " + parentFolder); } } String lang = GetLastLang(); if (lang == null || lang.Length == 0) { comboLang.SelectedIndex = 0; currentLang = "eng"; } else { for (int i = 0; i < comboLang.Items.Count; i++) { if (comboLang.Items[i].ToString().Equals(lang)) { comboLang.SelectedIndex = i; break; } } currentLang = lang; } // Let user browse the ocr dll if it is not found in PATH. while (true) { dllFilePath = AspriseOCR.getOcrDllPath(); if (dllFilePath == null) { log("OCR dll (" + AspriseOCR.getOcrDllName() + ") is not found in PATH."); OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.RestoreDirectory = true; fileDialog.Title = "Please select " + AspriseOCR.getOcrDllName(); fileDialog.FileName = AspriseOCR.getOcrDllName(); if (fileDialog.ShowDialog(this) == DialogResult.OK) { log("User browsed dll: " + fileDialog.FileName); AspriseOCR.addToSystemPath(Path.GetDirectoryName(fileDialog.FileName)); browsed = true; } count ++; if (count == 2) { showMessageBox("OCR DLL not found. ", "Error"); break; } } else { log("OCR dll found: " + dllFilePath); if (browsed) { log("Please consider copy " + AspriseOCR.getOcrDllName() + " to directory: " + Directory.GetCurrentDirectory()); } log(AspriseOCR.GetLibraryVersion()); try { log("Starting OCR engine ..."); AspriseOCR.SetUp(); ocr = new AspriseOCR(); ocr.StartEngine(currentLang, AspriseOCR.SPEED_FASTEST); log("OCR engine started successfully."); } catch (Exception e) { log("ERROR: Failed to start OCR engine: " + e); log(e.StackTrace); } break; } } // user preference textImage.Text = GetLastImagePath(); }
void doOcr() { if (textImage.Text.Trim().Length == 0) { showMessageBox("Please select an input image first.", "Error"); return; } if (!File.Exists(textImage.Text.Trim())) { showMessageBox("Image file does not exist: " + textImage.Text, "Error"); return; } if (! langOnUI.Equals(currentLang)) { ocr.StopEngine(); currentLang = null; ocr = new AspriseOCR(); ocr.StartEngine(langOnUI, AspriseOCR.SPEED_FASTEST); currentLang = langOnUI; } if (ocr == null || !ocr.IsEngineRunning) { showMessageBox("OCR engine is not running", "Error"); return; } if (!checkRecognizeText.Checked && !checkRecognizeBarcodes.Checked) { showMessageBox("Please check at least one of 'Text', 'Barcodes'", "Warn"); return; } string recognizeType = AspriseOCR.RECOGNIZE_TYPE_ALL; if (!checkRecognizeText.Checked) { recognizeType = AspriseOCR.RECOGNIZE_TYPE_BARCODE; } if (!checkRecognizeBarcodes.Checked) { recognizeType = AspriseOCR.RECOGNIZE_TYPE_TEXT; } string outputFormat = AspriseOCR.OUTPUT_FORMAT_PLAINTEXT; if (radioOutputXml.Checked) { outputFormat = AspriseOCR.OUTPUT_FORMAT_XML; } if (radioOutputPdf.Checked) { outputFormat = AspriseOCR.OUTPUT_FORMAT_PDF; } Dictionary<string, string> dict = new Dictionary<string, string>(); string pdfOutputFile = null; if (outputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF)) { pdfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".pdf"); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_FILE, pdfOutputFile); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_TEXT_VISIBLE, "true"); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_IMAGE_FORCE_BW, "true"); } log("OCR in progress, please stand by ...", true); DateTime timeStart = DateTime.Now; // Performs the actual recognition string s = ocr.Recognize(textImage.Text.Trim(), -1, -1, -1, -1, -1, recognizeType, outputFormat, dict); DateTime timeEnd = DateTime.Now; // open pdf file if (outputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF)) { if (s != null && s.Trim().Length > 0) { log(s + "\nPDF file: " + pdfOutputFile, true); } else { log("PDF file created: " + pdfOutputFile, true); try { System.Diagnostics.Process.Start(@pdfOutputFile); } catch (Exception e) { // ignore } } log("For illustration purpose, text has been rendered in color instead of transparent."); } else { log(s, true); } // user preference SetLastImagePath(textImage.Text); SetLastLang(currentLang); }
private void asprise_init() { formSettings = readDynamicSettings(formSettingFileName); bool browsed = false; int count = 0; // Let user browse the ocr dll if it is not found in PATH. while (true) { string dllFilePath = AspriseOCR.loadDll(); if (dllFilePath == null) { log("OCR dll (" + AspriseOCR.getOcrDllName() + ") is not found in PATH."); OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.RestoreDirectory = true; fileDialog.Title = "Please select " + AspriseOCR.getOcrDllName(); fileDialog.FileName = AspriseOCR.getOcrDllName(); if (fileDialog.ShowDialog(this) == DialogResult.OK) { log("User browsed dll: " + fileDialog.FileName); AspriseOCR.addToSystemPath(Path.GetDirectoryName(fileDialog.FileName)); browsed = true; } count++; if (count == 2) { showMessageBox("OCR DLL not found. ", "Error"); break; } } else { log("OCR dll found: " + dllFilePath); if (browsed) { log("Please consider copy " + AspriseOCR.getOcrDllName() + " to directory: " + Directory.GetCurrentDirectory()); } try { log(AspriseOCR.GetLibraryVersion()); Text = AspriseOCR.GetLibraryVersion(); log("Starting OCR engine ..."); AspriseOCR.SetUp(); setupLangDropdown(); ocr = new AspriseOCR(); ocr.StartEngine(currentLang, AspriseOCR.SPEED_FASTEST, textPropsStart.Text.Trim()); currentEngineStartProps = textPropsStart.Text.Trim(); log("OCR engine started successfully."); } catch (Exception e) { log("ERROR: Failed to start OCR engine: " + e); log(e.StackTrace); } break; } } // user preference textImage.Text = formSettings.Keys.Contains(PROP_NAME_LAST_IMG) ? formSettings[PROP_NAME_LAST_IMG] : ""; selectComboValue(comboTextLayout, formSettings.ContainsKey(PROP_NAME_TEXT_LAYOUT) ? formSettings[PROP_NAME_TEXT_LAYOUT] : "auto"); if (formSettings.ContainsKey(PROP_NAME_DATA_CAPTURE)) { checkDetectTables.Checked = "true".Equals(formSettings[PROP_NAME_DATA_CAPTURE].ToLower()); } if (formSettings.ContainsKey(PROP_NAME_AUTO_ROTATE)) { checkAutoRotatePages.Checked = "true".Equals(formSettings[PROP_NAME_AUTO_ROTATE].ToLower()); } if (formSettings.ContainsKey(PROP_NAME_WORD_LEVEL)) { checkWordLevel.Checked = "true".Equals(formSettings[PROP_NAME_WORD_LEVEL].ToLower()); } selectComboValue(comboRecognizeType, "Text + Barcodes"); // force to always init with 'all' if (formSettings.ContainsKey(PROP_NAME_HIGHLIGHT_PDF)) { checkPdfHighlightText.Checked = "true".Equals(formSettings[PROP_NAME_HIGHLIGHT_PDF].ToLower()); } string outputFormat = formSettings.ContainsKey(PROP_NAME_OUTPUT_FORMAT) ? formSettings[PROP_NAME_OUTPUT_FORMAT] : "xml"; radioOutputPdf.Checked = "pdf".Equals(outputFormat); radioOutputText.Checked = "text".Equals(outputFormat); radioOutputXml.Checked = "xml".Equals(outputFormat); radioOutputRtf.Checked = "rtf".Equals(outputFormat); textPropsStart.Text = formSettings.Keys.Contains(PROP_NAME_PROPS_START) ? formSettings[PROP_NAME_PROPS_START] : ""; textPropsRecognize.Text = formSettings.Keys.Contains(PROP_NAME_PROPS_RECOGNIZE) ? formSettings[PROP_NAME_PROPS_RECOGNIZE] : ""; }
void doOcr() { if (requestImgFile.Length == 0) { showMessageBox("Please select an input image first.", "Error"); return; } if (!File.Exists(requestImgFile)) { showMessageBox("Image file does not exist: " + requestImgFile, "Error"); return; } if (!requestLang.Equals(currentLang) || !requestPropsStart.Equals(currentEngineStartProps)) { ocr.StopEngine(); currentLang = null; ocr = new AspriseOCR(); ocr.StartEngine(requestLang, AspriseOCR.SPEED_FASTEST, requestPropsStart); currentLang = requestLang; currentEngineStartProps = requestPropsStart; } if (ocr == null || !ocr.IsEngineRunning) { showMessageBox("OCR engine is not running", "Error"); return; } Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add(AspriseOCR.PROP_OUTPUT_SEPARATE_WORDS, requestWordLevel? "true" : "false"); dict.Add(AspriseOCR.PROP_PAGE_TYPE, requestLayout); dict.Add(AspriseOCR.PROP_TABLE_SKIP_DETECTION, requestDataCapture ? "false" : "true"); dict.Add(AspriseOCR.PROP_IMG_PREPROCESS_TYPE, requestAutoRotate ? AspriseOCR.PROP_IMG_PREPROCESS_TYPE_DEFAULT_WITH_ORIENTATION_DETECTION : AspriseOCR.PROP_IMG_PREPROCESS_TYPE_DEFAULT); string pdfOutputFile = null; if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF)) { pdfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".pdf"); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_FILE, pdfOutputFile); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_TEXT_VISIBLE, checkPdfHighlightText.Checked ? "true" : "false"); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_IMAGE_FORCE_BW, "true"); } string rtfOutputFile = null; if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_RTF)) { rtfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".rtf"); dict.Add(AspriseOCR.PROP_RTF_OUTPUT_FILE, rtfOutputFile); } string allRecogProps = AspriseOCR.dictToString(dict) + (requestPropsRecognize == null || requestPropsRecognize.Trim().Length == 0 ? "" : AspriseOCR.CONFIG_PROP_SEPARATOR + requestPropsRecognize); String status = "Recognizing " + requestRecognizeType + " to output as " + requestOutputFormat + " on image: " + requestImgFile + " ...\n" + "OCR engine start props: " + requestPropsStart + "\n" + "OCR recognition props: " + allRecogProps + "\n" + "Please stand by ..."; log(status, true); DateTime timeStart = DateTime.Now; // Performs the actual recognition string s = ocr.Recognize(textImage.Text.Trim(), -1, -1, -1, -1, -1, requestRecognizeType, requestOutputFormat, AspriseOCR.dictToString(dict) + AspriseOCR.CONFIG_PROP_SEPARATOR + requestPropsRecognize); DateTime timeEnd = DateTime.Now; // open pdf file if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF)) { if (s != null && s.Trim().Length > 0) { log(s + "\nPDF file: " + pdfOutputFile, true); } else { log("PDF file created: " + pdfOutputFile, true); try { System.Diagnostics.Process.Start(@pdfOutputFile); } catch (Exception e) { Console.WriteLine(e); log(e.ToString()); } } log("For illustration purpose, text has been rendered in color instead of transparent."); } else if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_RTF)) { if (s != null && s.Trim().Length > 0) { log(s + "\nRTF file: " + rtfOutputFile, true); } else { log("RTF file created: " + rtfOutputFile, true); try { System.Diagnostics.Process.Start(@rtfOutputFile); } catch (Exception e) { Console.WriteLine(e); log(e.ToString()); } } } else if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_XML)) { log("", true); try { string xmlOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".xml"); File.WriteAllText(xmlOutputFile, s, Encoding.UTF8); AspriseOCR.saveAocrXslTo(Directory.GetCurrentDirectory(), false); if (File.Exists(xmlOutputFile)) { log("You may view the XML file using IE or Firefox: " + xmlOutputFile, true); System.Diagnostics.Process.Start(@xmlOutputFile); } } catch (Exception e) { Console.WriteLine(e); log(e.ToString()); } log(s, false); } else { log(s == null ? "(null)" : s, true); } // user preference saveSettings(); }
private void asprise_init() { formSettings = readDynamicSettings(formSettingFileName); bool browsed = false; int count = 0; // Let user browse the ocr dll if it is not found in PATH. while (true) { string dllFilePath = AspriseOCR.loadDll(); if (dllFilePath == null) { log("OCR dll (" + AspriseOCR.getOcrDllName() + ") is not found in PATH."); OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.RestoreDirectory = true; fileDialog.Title = "Please select " + AspriseOCR.getOcrDllName(); fileDialog.FileName = AspriseOCR.getOcrDllName(); if (fileDialog.ShowDialog(this) == DialogResult.OK) { log("User browsed dll: " + fileDialog.FileName); AspriseOCR.addToSystemPath(Path.GetDirectoryName(fileDialog.FileName)); browsed = true; } count ++; if (count == 2) { showMessageBox("OCR DLL not found. ", "Error"); break; } } else { log("OCR dll found: " + dllFilePath); if (browsed) { log("Please consider copy " + AspriseOCR.getOcrDllName() + " to directory: " + Directory.GetCurrentDirectory()); } try { log(AspriseOCR.GetLibraryVersion()); Text = AspriseOCR.GetLibraryVersion(); log("Starting OCR engine ..."); AspriseOCR.SetUp(); setupLangDropdown(); ocr = new AspriseOCR(); ocr.StartEngine(currentLang, AspriseOCR.SPEED_FASTEST, textPropsStart.Text.Trim()); currentEngineStartProps = textPropsStart.Text.Trim(); log("OCR engine started successfully."); } catch (Exception e) { log("ERROR: Failed to start OCR engine: " + e); log(e.StackTrace); } break; } } // user preference textImage.Text = formSettings.Keys.Contains(PROP_NAME_LAST_IMG) ? formSettings[PROP_NAME_LAST_IMG] : ""; selectComboValue(comboTextLayout, formSettings.ContainsKey(PROP_NAME_TEXT_LAYOUT) ? formSettings[PROP_NAME_TEXT_LAYOUT] : "auto"); if (formSettings.ContainsKey(PROP_NAME_DATA_CAPTURE)) { checkDetectTables.Checked = "true".Equals(formSettings[PROP_NAME_DATA_CAPTURE].ToLower()); } if (formSettings.ContainsKey(PROP_NAME_AUTO_ROTATE)) { checkAutoRotatePages.Checked = "true".Equals(formSettings[PROP_NAME_AUTO_ROTATE].ToLower()); } if (formSettings.ContainsKey(PROP_NAME_WORD_LEVEL)) { checkWordLevel.Checked = "true".Equals(formSettings[PROP_NAME_WORD_LEVEL].ToLower()); } selectComboValue(comboRecognizeType, "Text + Barcodes"); // force to always init with 'all' if (formSettings.ContainsKey(PROP_NAME_HIGHLIGHT_PDF)) { checkPdfHighlightText.Checked = "true".Equals(formSettings[PROP_NAME_HIGHLIGHT_PDF].ToLower()); } string outputFormat = formSettings.ContainsKey(PROP_NAME_OUTPUT_FORMAT) ? formSettings[PROP_NAME_OUTPUT_FORMAT] : "xml"; radioOutputPdf.Checked = "pdf".Equals(outputFormat); radioOutputText.Checked = "text".Equals(outputFormat); radioOutputXml.Checked = "xml".Equals(outputFormat); radioOutputRtf.Checked = "rtf".Equals(outputFormat); textPropsStart.Text = formSettings.Keys.Contains(PROP_NAME_PROPS_START) ? formSettings[PROP_NAME_PROPS_START] : ""; textPropsRecognize.Text = formSettings.Keys.Contains(PROP_NAME_PROPS_RECOGNIZE) ? formSettings[PROP_NAME_PROPS_RECOGNIZE] : ""; }
void doOcr() { if (requestImgFile.Length == 0) { showMessageBox("Please select an input image first.", "Error"); return; } if (!File.Exists(requestImgFile)) { showMessageBox("Image file does not exist: " + requestImgFile, "Error"); return; } if (!requestLang.Equals(currentLang) || !requestPropsStart.Equals(currentEngineStartProps)) { ocr.StopEngine(); currentLang = null; ocr = new AspriseOCR(); ocr.StartEngine(requestLang, AspriseOCR.SPEED_FASTEST, requestPropsStart); currentLang = requestLang; currentEngineStartProps = requestPropsStart; } if (ocr == null || !ocr.IsEngineRunning) { showMessageBox("OCR engine is not running", "Error"); return; } Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add(AspriseOCR.PROP_OUTPUT_SEPARATE_WORDS, requestWordLevel? "true" : "false"); dict.Add(AspriseOCR.PROP_PAGE_TYPE, requestLayout); dict.Add(AspriseOCR.PROP_TABLE_SKIP_DETECTION, requestDataCapture ? "false" : "true"); dict.Add(AspriseOCR.PROP_IMG_PREPROCESS_TYPE,requestAutoRotate ? AspriseOCR.PROP_IMG_PREPROCESS_TYPE_DEFAULT_WITH_ORIENTATION_DETECTION : AspriseOCR.PROP_IMG_PREPROCESS_TYPE_DEFAULT); string pdfOutputFile = null; if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF)) { pdfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".pdf"); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_FILE, pdfOutputFile); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_TEXT_VISIBLE, checkPdfHighlightText.Checked ? "true" : "false"); dict.Add(AspriseOCR.PROP_PDF_OUTPUT_IMAGE_FORCE_BW, "true"); } string rtfOutputFile = null; if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_RTF)) { rtfOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".rtf"); dict.Add(AspriseOCR.PROP_RTF_OUTPUT_FILE, rtfOutputFile); } string allRecogProps = AspriseOCR.dictToString(dict) + (requestPropsRecognize == null || requestPropsRecognize.Trim().Length == 0 ? "" : AspriseOCR.CONFIG_PROP_SEPARATOR + requestPropsRecognize); String status = "Recognizing " + requestRecognizeType + " to output as " + requestOutputFormat + " on image: " + requestImgFile + " ...\n" + "OCR engine start props: " + requestPropsStart + "\n" + "OCR recognition props: " + allRecogProps + "\n" + "Please stand by ..."; log(status, true); DateTime timeStart = DateTime.Now; // Performs the actual recognition string s = ocr.Recognize(textImage.Text.Trim(), -1, -1, -1, -1, -1, requestRecognizeType, requestOutputFormat, AspriseOCR.dictToString(dict) + AspriseOCR.CONFIG_PROP_SEPARATOR + requestPropsRecognize); DateTime timeEnd = DateTime.Now; // open pdf file if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_PDF)) { if (s != null && s.Trim().Length > 0) { log(s + "\nPDF file: " + pdfOutputFile, true); } else { log("PDF file created: " + pdfOutputFile, true); try { System.Diagnostics.Process.Start(@pdfOutputFile); } catch (Exception e) { Console.WriteLine(e); log(e.ToString()); } } log("For illustration purpose, text has been rendered in color instead of transparent."); } else if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_RTF)) { if (s != null && s.Trim().Length > 0) { log(s + "\nRTF file: " + rtfOutputFile, true); } else { log("RTF file created: " + rtfOutputFile, true); try { System.Diagnostics.Process.Start(@rtfOutputFile); } catch (Exception e) { Console.WriteLine(e); log(e.ToString()); } } } else if (requestOutputFormat.Equals(AspriseOCR.OUTPUT_FORMAT_XML)) { log("", true); try { string xmlOutputFile = Path.Combine(Directory.GetCurrentDirectory(), DateTime.Now.ToString("O").Replace(':', '-') + ".xml"); File.WriteAllText(xmlOutputFile, s, Encoding.UTF8); AspriseOCR.saveAocrXslTo(Directory.GetCurrentDirectory(), false); if (File.Exists(xmlOutputFile)) { log("You may view the XML file using IE or Firefox: " + xmlOutputFile, true); System.Diagnostics.Process.Start(@xmlOutputFile); } } catch (Exception e) { Console.WriteLine(e); log(e.ToString()); } log(s, false); } else { log(s == null ? "(null)" : s, true); } // user preference saveSettings(); }