public static void PerformOCR(string imageFile, string outputFile, string langCode, string pageSegMode, bool hocr) { IList <Image> imageList; try { imageList = ImageIOHelper.GetImageList(new FileInfo(imageFile)); OCR <Image> ocrEngine = new OCRImages(); ocrEngine.PageSegMode = pageSegMode; ocrEngine.Hocr = hocr; string result = ocrEngine.RecognizeText(imageList, langCode); // skip post-corrections if hocr output if (!hocr) { // postprocess to correct common OCR errors result = Processor.PostProcess(result, langCode); // correct common errors caused by OCR result = TextUtilities.CorrectOCRErrors(result); // correct letter cases result = TextUtilities.CorrectLetterCases(result); } using (StreamWriter sw = new StreamWriter(outputFile, false, new System.Text.UTF8Encoding())) { sw.Write(result); } } finally { imageList = null; } }
private void backgroundWorkerOcr_DoWork(object sender, DoWorkEventArgs e) { // Get the BackgroundWorker that raised this event. BackgroundWorker worker = sender as BackgroundWorker; OCRImageEntity entity = (OCRImageEntity)e.Argument; OCR <Image> ocrEngine = new OCRImages(); ocrEngine.PageSegMode = selectedPSM; ocrEngine.Language = entity.Language; // Assign the result of the computation to the Result property of the DoWorkEventArgs // object. This is will be available to the RunWorkerCompleted eventhandler. //e.Result = ocrEngine.RecognizeText(entity.ClonedImages, entity.Lang, entity.Rect, worker, e); IList <Image> images = entity.ClonedImages; for (int i = 0; i < images.Count; i++) { if (worker.CancellationPending) { e.Cancel = true; break; } string result = ocrEngine.RecognizeText(((List <Image>)images).GetRange(i, 1), entity.Inputfilename, entity.Rect, worker, e); worker.ReportProgress(i, result); // i is not really percentage } }
/// <summary> /// Performs OCR for bulk/batch and console operations. /// </summary> /// <param name="imageFile">Image file</param> /// <param name="outputFile">Output file without extension</param> /// <param name="langCode">language code</param> /// <param name="pageSegMode">page segmentation mode</param> /// <param name="outputFormat">format of output file. Possible values: <code>text</code>, <code>text+</code> (with post-corrections), <code>hocr</code></param> public static void PerformOCR(string imageFile, string outputFile, string langCode, string pageSegMode, string outputFormat) { IList <Image> imageList; try { DirectoryInfo dir = Directory.GetParent(outputFile); if (dir != null && !dir.Exists) { dir.Create(); } bool postprocess = "text+" == outputFormat; OCR <Image> ocrEngine = new OCRImages(); ocrEngine.PageSegMode = pageSegMode; ocrEngine.Language = langCode; ocrEngine.OutputFormat = outputFormat.Replace("+", string.Empty); // convert PDF to TIFF if (imageFile.ToLower().EndsWith(".pdf")) { imageFile = PdfUtilities.ConvertPdf2Tiff(imageFile); } imageList = ImageIOHelper.GetImageList(new FileInfo(imageFile)); string result = ocrEngine.RecognizeText(imageList, imageFile); // post-corrections for text+ output if (postprocess) { // postprocess to correct common OCR errors result = Processor.PostProcess(result, langCode); // correct common errors caused by OCR result = TextUtilities.CorrectOCRErrors(result); // correct letter cases result = TextUtilities.CorrectLetterCases(result); } //if (outputFormat == "pdf") // not yet supported //{ // byte[] bytes = null; // get the byte array // File.WriteAllBytes(outputFile, bytes); //} //else { string filename = outputFile + "." + outputFormat.Replace("+", string.Empty).Replace("text", "txt").Replace("hocr", "html"); using (StreamWriter sw = new StreamWriter(filename, false, new System.Text.UTF8Encoding())) { sw.Write(result); } } } finally { imageList = null; } }
private void AutoOCR() { FileInfo imageFile; try { imageFile = new FileInfo(queue.Dequeue()); } catch { return; } this.statusForm.TextBox.BeginInvoke(new UpdateStatusEvent(this.WorkerUpdate), new Object[] { imageFile.FullName }); if (curLangCode == null) { this.statusForm.TextBox.BeginInvoke(new UpdateStatusEvent(this.WorkerUpdate), new Object[] { " ** " + Properties.Resources.selectLanguage + " **" }); //queue.Clear(); return; } IList <Image> imageList = ImageIOHelper.GetImageList(imageFile); if (imageList == null) { this.statusForm.TextBox.BeginInvoke(new UpdateStatusEvent(this.WorkerUpdate), new Object[] { " ** " + Properties.Resources.Cannotprocess + imageFile.Name + " **" }); return; } try { OCR <Image> ocrEngine = new OCRImages(); ocrEngine.PSM = selectedPSM; string result = ocrEngine.RecognizeText(imageList, curLangCode); // postprocess to correct common OCR errors result = Processor.PostProcess(result, curLangCode); // correct common errors caused by OCR result = TextUtilities.CorrectOCRErrors(result); // correct letter cases result = TextUtilities.CorrectLetterCases(result); using (StreamWriter sw = new StreamWriter(Path.Combine(outputFolder, imageFile.Name + ".txt"), false, new System.Text.UTF8Encoding())) { sw.Write(result); } } catch (Exception e) { // Sets the UI culture to the selected language. Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedUILanguage); this.statusForm.TextBox.BeginInvoke(new UpdateStatusEvent(this.WorkerUpdate), new Object[] { " ** " + Properties.Resources.Cannotprocess + imageFile.Name + " **" }); Console.WriteLine(e.StackTrace); } }
void PerformOCR(string[] args) { try { if (args[0] == "-?" || args[0] == "-help" || args.Length == 1 || args.Length == 3) { Console.WriteLine("Usage: vietocr imagefile outputfile [-l langcode]"); return; } FileInfo imageFile = new FileInfo(args[0]); FileInfo outputFile = new FileInfo(args[1]); if (!imageFile.Exists) { Console.WriteLine("Input file does not exist."); return; } string curLangCode; if (args.Length == 2) { curLangCode = "eng"; //default language } else { curLangCode = args[3]; } IList <Image> imageList = ImageIOHelper.GetImageList(imageFile); OCR <Image> ocrEngine = new OCRImages(); string result = ocrEngine.RecognizeText(imageList, curLangCode); // postprocess to correct common OCR errors result = Processor.PostProcess(result, curLangCode); // correct common errors caused by OCR result = TextUtilities.CorrectOCRErrors(result); // correct letter cases result = TextUtilities.CorrectLetterCases(result); using (StreamWriter sw = new StreamWriter(outputFile.FullName + ".txt", false, new System.Text.UTF8Encoding())) { sw.Write(result); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }
void PerformOCR(string[] args) { try { if (args[0] == "-?" || args[0] == "-help" || args.Length == 1 || args.Length == 3 || args.Length == 5) { Console.WriteLine("Usage: vietocr imagefile outputfile [-l lang] [-psm pagesegmode]"); return; } FileInfo imageFile = new FileInfo(args[0]); FileInfo outputFile = new FileInfo(args[1]); if (!imageFile.Exists) { Console.WriteLine("Input file does not exist."); return; } string curLangCode = "eng"; //default language string psm = "3"; // or alternatively, "PSM_AUTO"; // 3 - Fully automatic page segmentation, but no OSD (default) if (args.Length == 4) { if (args[2].Equals("-l")) { curLangCode = args[3]; } else if (args[2].Equals("-psm")) { psm = args[3]; } } else if (args.Length == 6) { curLangCode = args[3]; psm = args[5]; try { Int16.Parse(psm); } catch { Console.WriteLine("Invalid input value."); return; } } IList <Image> imageList = ImageIOHelper.GetImageList(imageFile); OCR <Image> ocrEngine = new OCRImages(); ocrEngine.PSM = psm; string result = ocrEngine.RecognizeText(imageList, curLangCode); // postprocess to correct common OCR errors result = Processor.PostProcess(result, curLangCode); // correct common errors caused by OCR result = TextUtilities.CorrectOCRErrors(result); // correct letter cases result = TextUtilities.CorrectLetterCases(result); using (StreamWriter sw = new StreamWriter(outputFile.FullName + ".txt", false, new System.Text.UTF8Encoding())) { sw.Write(result); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }