简体   繁体   中英

Tesseract in VSTO office addin

I am currently developing an office addin for C# to read image and use tesseract to extract text from image. However, i am not able to start the tesseract engine. I tried to put the tessdata,x64,x86 folders in the folder that visual studio creates during debugging. the path structure looks like the below:

" AppData\\Local\\assembly\\dl3\\randomstring\\randomstring\\randomstring\\randomstring "

I figure the tesseract engine might need to read the data file from where the dll is but somehow it didnt work.

Also, I have the required folder in bin\\debug and set to copy to output directoryfolder = Copy always.

The below is the error screen prompt by excel.

在此处输入图片说明

the below is the code i tried.

private string CurrentDirectory()
    {
        Assembly assemblyInfo = Assembly.GetExecutingAssembly();
        string assemblyLocation = assemblyInfo.Location;

        return assemblyLocation;
    }

public string GetText(Bitmap bmp)
        {
            var path = Path.Combine(folder, "tessdata"); //this is the project folder

            var RandomPath = Path.Combine(Path.GetDirectoryName(CurrentDirectory()), "tessdata"); // this is the visual studio created folder.
            
            string recognizedText = string.Empty;
            var engine = new TesseractEngine(RandomPath, "eng", EngineMode.TesseractAndLstm);
            bmp.Save("tempFile.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
            // Perform OCR
            using (Pix img = Pix.LoadFromFile("tempFile.jpeg"))
            {
                using (Page recognizedPage = engine.Process(img))
                {
                    recognizedText = recognizedPage.GetText();
                }
            }
            File.Delete("tempFile.jpeg");
            return recognizedText;
}

Any help is greatly appreciated

It seems assemblies can't be found, the error message refers to the https://github.com/charlesw/tesseract/wiki/error-1 page which describes possible causes.

You can try using the Assembly Binding Log Viewer which displays details for assembly binds. This information helps you diagnose why the .NET Framework cannot locate an assembly at run time. These failures are usually the result of an assembly deployed to the wrong location, a native image that is no longer valid, or a mismatch in version numbers or cultures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM