Please could someone tell me what am i doing wrong?
The line that errors:
using (var img = TesseractOCR.Pix.Image.LoadFromFile(imagePath))
Steps taken to create the application:
- The application is created using the .Net Framework 4.8 C#
- Installed TesseractOCR using NuGet
- Added tessdata from here https://github.com/tesseract-ocr/tessdata into the project folder
- Added the following code:
public class OCRTest
{
public static string PerformOCR(string imagePath)
{
try
{
string tessDataPath = @"C:ReposOCRTesseract-OCR2tessdata";
using (var engine = new Engine(tessDataPath, Language.English, EngineMode.Default))
{
using (var img = TesseractOCR.Pix.Image.LoadFromFile(imagePath))
{
using (var page = engine.Process(img))
{
return page.Text;
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return null;
}
}
}
private void btnOCR_Click(object sender, RoutedEventArgs e)
{
string imagePath = @"C:TestImages20230820_210122.jpg";
if (!File.Exists(imagePath))
{
Console.WriteLine($"File doesnt exist: {imagePath}");
return;
}
try
{
string recognizedText = OCRTest.PerformOCR(imagePath);
if (recognizedText != null)
{
MessageBox.Show("Recognized text:n" + recognizedText, "OCR Result");
}
else
{
MessageBox.Show("No text recognized.", "OCR Result");
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "OCR Error");
}
}