Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to initialize tesseract engine. but i have a tessdata #648

Open
Zifrkoks opened this issue Aug 28, 2023 · 1 comment
Open

Failed to initialize tesseract engine. but i have a tessdata #648

Zifrkoks opened this issue Aug 28, 2023 · 1 comment

Comments

@Zifrkoks
Copy link

i create coreldraw docker addon, I need to make an addon that translates the image into text and checks for errors. and i try tesseract for translate picture into text. but visual studio throws Tesseract.TesseractException: "Failed to initialise tesseract engine.. See https://github.com/charlesw/tesseract/wiki/Error-1 for details.". throws an error despite the fact that I have tessdata. I have no idea what is wrong. crash on ReadText(object sender, RoutedEventArgs e)

`using Corel.Interop.VGCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tesseract;
using corel = Corel.Interop.VGCore;

namespace AiScanner
{
public partial class DockerUI : UserControl
{
private corel.Application corelApp;
private Styles.StylesController stylesController;
string fileUrl;
MemoryStream lastPicture;
public DockerUI(object app)
{
InitializeComponent();
try
{
this.corelApp = app as corel.Application;
stylesController = new Styles.StylesController(this.Resources, this.corelApp);
fileUrl = corelApp.UserWorkspacePath + "AiScanner.bmp";
}
catch
{
global::System.Windows.MessageBox.Show("VGCore Erro");
}

    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        stylesController.LoadThemeFromPreference();
    }

    private void ImageSelected(object sender, RoutedEventArgs e)
    {
        viewPicture.Source = null;
        File.Delete(fileUrl);
        
        Shapes shapes = corelApp.ActiveDocument.SelectionRange.Shapes;
        if (shapes.Count != 1)
        {
            status.Text = "select one picture";
            return;
        }
        corel.Shape shape = shapes[1];
        Bitmap bmp = null;
        if (shape.Type != cdrShapeType.cdrBitmapShape)
        {
            bmp = shape.ConvertToBitmapEx().Bitmap;
        }
        else
            bmp = shape.Bitmap;
        
        var load = bmp.SaveAs(fileUrl,cdrFilter.cdrBMP);
        load.Finish();

        FileStream fs = new FileStream(fileUrl, FileMode.Open,FileAccess.Read);
        lastPicture = new MemoryStream();
        fs.CopyTo(lastPicture);
        fs.Close();

        var source = new System.Windows.Media.Imaging.BitmapImage();
        source.BeginInit();
        source.StreamSource = lastPicture;
        source.EndInit();
        viewPicture.Source = source;

        callapse.Visibility = Visibility.Visible;
        mainButton.Content = "close";
        mainButton.Click -= ImageSelected;
        mainButton.Click += ImageClosed;
    }


    private void ImageClosed(object sender, RoutedEventArgs e)
    {
        viewPicture.Source = null;
        File.Delete(fileUrl);
        lastPicture.Close();
        callapse.Visibility = Visibility.Collapsed;
        ScannedText.Visibility = Visibility.Collapsed;
        mainButton.Click -= ImageClosed;
        mainButton.Click += ImageSelected;
        mainButton.Content = "open";


    }

    private void ReadText(object sender, RoutedEventArgs e)
    {

        if (corelApp.ActiveDocument == null)
            return;

        corelApp.BeginDraw();
        if (langSelect.SelectedIndex == 0)
        {
            status.Text = "no language selected";
            return;
        }
        TesseractEngine engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.TesseractAndLstm); 
        switch (langSelect.SelectedIndex)
        {
            case 1:
                ScannedText.Language = XmlLanguage.GetLanguage("ru-RU");
                engine = new TesseractEngine(@"./tessdata","rus", EngineMode.TesseractAndLstm);
                break;
            case 2:
                ScannedText.Language = XmlLanguage.GetLanguage("en-US");
                engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.TesseractAndLstm);
                break;
        }
        using (var img = Pix.LoadFromMemory(lastPicture.ToArray()))
        {
            using (var res = engine.Process(img))
            {
                ScannedText.Visibility = Visibility.Visible;
                ScannedText.Text = res.GetText();
            }
        }
        corelApp.EndDraw();
    }



    private static System.Windows.Media.Imaging.BitmapImage LoadImage(byte[] imageData)
    {
        if (imageData == null || imageData.Length == 0) return null;
        var image = new System.Windows.Media.Imaging.BitmapImage();
        using (var mem = new MemoryStream(imageData))
        {
            mem.Position = 0;
            image.BeginInit();
            image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = null;
            image.StreamSource = mem;
            image.EndInit();
        }
        image.Freeze();
        return image;
    }
}

}
`

@msfmar
Copy link

msfmar commented Nov 3, 2023

I have a similar issue. At first the example project (https://github.com/charlesw/tesseract-samples) ran fine, but after adding german language training data to the ./tessdata folder and using that instead of the english one (by replacing the "eng" in line 18 with "deu") , I get the following error (ConsoleDemo) :

Error opening data file ./tessdata/deu.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.
Failed loading language 'deu'
Tesseract couldn't load any languages!
Unexpected Error: Failed to initialise tesseract engine.. See https://github.com/charlesw/tesseract/wiki/Error-1 for details.
Details:
Tesseract.TesseractException: Failed to initialise tesseract engine.. See https://github.com/charlesw/tesseract/wiki/Error-1 for details.
at Tesseract.TesseractEngine.Initialise(String datapath, String language, EngineMode engineMode, IEnumerable`1 configFiles, IDictionary`2 initialValues, Boolean setOnlyNonDebugVariables)
at Tesseract.TesseractEngine..ctor(String datapath, String language, EngineMode engineMode, IEnumerable`1 configFiles, IDictionary`2 initialOptions, Boolean setOnlyNonDebugVariables)
at Tesseract.TesseractEngine..ctor(String datapath, String language, EngineMode engineMode)
at Tesseract.ConsoleDemo.Program.Main(String[] args) in [ReposPath]\tesseract-samples\src\Tesseract.ConsoleDemo\Program.cs:line 19

I downloaded the Language form the linked repository, where it also says it was made for Tesseract 4 so there should not be any version issues.

Edit: found the issue, I did not set the german language file to copy on build (in VS right klick --> properties). Leaving this comment in cas any one else runs into the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants