2012-06-14 9 views
5

Mam dokumenty skanowane jako .jpg zdjęcia w folderze i chciałbym zrobić OCR w C# seryjnie dla każdego z moich dokumentów w tym folderze. dotychczas ive zrobić to:Jak serializować OCR z MODI (Microsoft Office Document Imaging) w C#

public string CheckFilesAndDoOCR(string directoryPath) 
{ 
    directoryPath = Environment.SpecialFolder.MyPictures + "\\OCRTempPictures\\"; 
    IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator(); 
    string TheTxt = ""; 

    while (files.MoveNext()) 
    { 
     // FileInfo 
     FileInfo nfo = new FileInfo(Convert.ToString(files.Current)); 

     // Get new file name 
     string fileName = AlltoJPG(nfo); 

     // FileInfo (New File) 
     FileInfo foo = new FileInfo(fileName); 

     // Check for JPG File Format 
     if (foo.Extension == ".jpg" || foo.Extension == ".JPG") 
     // or // ImageFormat.Jpeg.ToString() 
     { 
      try 
      { 
       // OCR Operations... 
       MODI.Document md = new MODI.Document(); 
       md.Create(foo.FullName); 
       md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); // OCR(); 
       MODI.Image image = (MODI.Image)md.Images[0]; 
       TheTxt = image.Layout.Text; 
       md.Close(false); 

       // Create text file with the same Image file name 
       FileStream createFile = new FileStream(foo.DirectoryName + "\\" + foo.Name.Replace(foo.Extension,string.Empty) + ".txt", FileMode.CreateNew); 

       // Save the image text in the text file 
       StreamWriter writeFile = new StreamWriter(createFile); 
       writeFile.Write(TheTxt); 
       writeFile.Close(); 
      } 
      catch (Exception ex) 
      { 
       // Expected errors 
       string LogPath = System.Environment.SpecialFolder.MyPictures + "\\OCRTempPictures\\OCRInfo.txt"; 
       Logger(LogPath, "| Exception: Source[" + ex.Source + "] Message[" + ex.Message + "] InnerException[" + ex.InnerException + "] StackTrace[" + ex.StackTrace + "] | "); 
       // MessageBox.Show(ex.Message, "OCR Exception", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     } 
    } 
    return TheTxt; 
} 

ale MODI daje OCR running! lub Cant reach file.File is in use. błędy ..

zależności od sytuacji:

  • Jak można uniknąć tych błędów?

  • Czy istnieje sposób zatrzymania działania OCR i wyczerpania wszystkich używanych obiektów?

Jeśli ktoś może odpowiedzieć na którekolwiek z powyższych pytań, będzie to docenione.

+0

Czy sprawdzić ten wątek ? http://stackoverflow.com/questions/6699740/ocr-running-error-when-using-modi-2003-with-c-sharp To jest ogólny błąd, który oznacza, że ​​MODI ma problem z rozpoznaniem mapy bitowej –

+0

@PanagiotisKanavos yes i zrobił! ale te odpowiedzi nie rozwiązują mojego problemu .. rozpoznaje on wszystkie postacie i im używa plików jpeg, a także po pracy nad nimi przez długi czas odkryłem większość problemów, ale wciąż istnieje najbardziej szalony problem. Nie pozwala mi się ruszyć - usuń ten plik, który miałem z wynikiem ocr. idk, dlaczego to robi. mówi, że plik nadal jest używany. źle zaktualizowane pytanie. –

+1

Powodem wystąpienia tego błędu jest próba przetworzenia wielu obrazów naraz. Wprowadź kod, aby temu zapobiec. –

Odpowiedz

2

Oto w pełni działający kod! dzięki @Ramhound

Poniższy kod właśnie określa folder pełen zdjęć i jeden po drugim robi skanowanie OCR na nich.

/// <summary> 
    /// Gets all images inside a Folder 
    /// and triggers OCR on each.. 
    /// </summary> 
    /// <param name="directoryPath"> Path to Folder </param> 
    /// <returns> Text </returns>   
    public string CheckFileAndDoOCR(string directoryPath) 
    { 
     string TheTxt = ""; 
     IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator(); 

     while (files.MoveNext()) 
     { 
      // FileInfo 
      FileInfo foo = new FileInfo(Convert.ToString(files.Current)); 

      // Check for JPG File Format 
      if (foo.Extension == ".jpg" || foo.Extension == ".JPG") 
      // or // ImageFormat.Jpeg.ToString() 
      { 
       // Start OCR Procedure 
       TheTxt = DoOCR(foo.FullName); 
       // Create TXT file next to ImageFile 
       string txtFileName = foo.DirectoryName + "\\" + foo.Name.Replace(foo.Extension,"") + ".txt"; 
       FileStream createFile = new FileStream(txtFileName, FileMode.OpenOrCreate); 
       // Save the text in to TXT file 
       StreamWriter writeFile = new StreamWriter(createFile); 
       writeFile.Write(TheTxt); 
       // Close 
       writeFile.Close(); 
       createFile.Close(); 
      } 

      // Delete used pictures (Optional) 
      /*--------------------------------------------------------------------*/ 
      try 
      { foo.Delete(); } 
      catch (Exception ex) 
      { Logger(LogPath, "| Exception: Source[" + ex.Source + "] Message[" + ex.Message + 
       "] InnerException[" + ex.InnerException + "] StackTrace[" + ex.StackTrace + "] | "); } 
      /*--------------------------------------------------------------------*/ 
     } 
     return TheTxt; 
    } 
    // DoOCR 
    // 
    /// <summary> 
    /// Start an OCR scan on given ImageFile 
    /// </summary> 
    /// <param name="FullPath"> Path to ImageFile </param> 
    /// <returns> Text </returns> 
    public string DoOCR(string FullPath) 
    { 
     string txt; 

     // OCR Operations... 
     MODI.Document md = new MODI.Document(); // Create MODI.Document 
     md.Create(FullPath); // Fill MODI.Document with my file 
     // Showprogress of OCR 
     md.OnOCRProgress += new MODI._IDocumentEvents_OnOCRProgressEventHandler(this.ShowProgress); 
     // Begin OCR 
     md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); // OCR(); 
     // Image from file 
     MODI.Image image = (MODI.Image)md.Images[0]; 
     txt = image.Layout.Text; 
     // Optionally you can get only first word by using word.Text 
     /// Words from Image : 
     // MODI.Word word = image.Layout.Words[0]; 
     /// Text from first Word : 
     // txt = word.Text; 

     // Close OCR 
     word = null; 
     image = null; 
     md.Close(false); 
     md = null; 

     // Finalize 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 

     // Return Text 
     return txt; 
    } 
1

To dlatego Doc1.OCR() sprawdzić wielostronicowego pliku tiff jeśli włożysz tylko pojedynczy plik strony to będzie pokazuje, że błąd spróbuj wielostronicowy plik TIFF

+0

miło poznać dzięki. –