配置EmguCV中的OCR


問題:Unable to create ocr model using Path '.\' and language 'eng'.

_ocr = new Tesseract(@".\", "eng", OcrEngineMode.TesseractLstmCombined);

糾結了有幾個星期,最后終於試出來了。

總結原因有N,如下:

1. 路徑寫法

path =  @"C:\myTools\Tess“ 后面需要加上 \,

2. 子文件夾

如果path= @"C:\myTools\Tess\", 那么文件應該放在一個名叫tessdata的子文件夾內,而不是直接放在path的目錄下。

3. 版本匹配

剛開始我下載了最新的traindata放在文件夾下面,不好使呀,隨后找到了正確的版本,才成功

emgu版本號:3.4.3 所需要的數據版本路徑如下

https://github.com/tesseract-ocr/tessdata/blob/4592b8d453889181e01982d22328b5846765eaad/eng.traineddata

https://github.com/tesseract-ocr/tessdata/blob/4592b8d453889181e01982d22328b5846765eaad/osd.traineddata

 

運行通過的代碼和文件夾配置如下

string path =  @"C:\myTools\Tess\"; 
 _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined);

 

參考

從Emgu官網下載的程序包里面的例程。

http://www.emgu.com/wiki/index.php/Main_Page

 

關鍵代碼區域如下

        private void InitOcr(String path, String lang, OcrEngineMode mode)
        {
            try
            {
                if (_ocr != null)
                {
                    _ocr.Dispose();
                    _ocr = null;
                }

                if (String.IsNullOrEmpty(path))
                    path = ".";

                TesseractDownloadLangFile(path, lang);
                TesseractDownloadLangFile(path, "osd"); //script orientation detection
                String pathFinal = path.Length == 0 || path.Substring(path.Length - 1, 1).Equals(Path.DirectorySeparatorChar.ToString())
                    ? path
                    : String.Format("{0}{1}", path, System.IO.Path.DirectorySeparatorChar);

                _ocr = new Tesseract(pathFinal, lang, mode);

            }
            catch (Exception e)
            {
                _ocr = null;
                System.Diagnostics.Debug.Print(e.Message, "Failed to initialize tesseract OCR engine");
            }
        }
private static void TesseractDownloadLangFile(String folder, String lang) { String subfolderName = "tessdata"; String folderName = System.IO.Path.Combine(folder, subfolderName); if (!System.IO.Directory.Exists(folderName)) { System.IO.Directory.CreateDirectory(folderName); } String dest = System.IO.Path.Combine(folderName, String.Format("{0}.traineddata", lang)); if (!System.IO.File.Exists(dest)) using (System.Net.WebClient webclient = new System.Net.WebClient()) { String source = String.Format("https://github.com/tesseract-ocr/tessdata/blob/4592b8d453889181e01982d22328b5846765eaad/{0}.traineddata?raw=true", lang); Console.WriteLine(String.Format("Downloading file from '{0}' to '{1}'", source, dest)); webclient.DownloadFile(source, dest); Console.WriteLine(String.Format("Download completed")); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM