Skip to content

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

Open
@Zifrkoks

Description

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;
    }
}

}
`

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions