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 <string> ocrEngine = new OCRFiles(); IList <string> workingImageFiles = entity.ImageFiles; foreach (string workingImageFile in workingImageFiles) { tempFileCollection.AddFile(workingImageFile, false); // to be deleted when program exits } for (int i = 0; i < workingImageFiles.Count; i++) { if (worker.CancellationPending) { e.Cancel = true; break; } string result = ocrEngine.RecognizeText(((List <string>)workingImageFiles).GetRange(i, 1), entity.Language, worker, e); worker.ReportProgress(i, result); // i is not really percentage } }
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 <string> ocrEngine = new OCRFiles(); IList <string> files = new List <string>(); files.Add(imageFile.FullName); string result = ocrEngine.RecognizeText(files, 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 <string> ocrEngine = new OCRFiles(); IList <string> files = new List <string>(); files.Add(imageFile.FullName); string result = ocrEngine.RecognizeText(files, 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); } }