How could i extract persian numbers from an image in c#

123 views Asked by At

I want a code that takes the image of a fisherman's check and first accurately detects the location of the fisherman's ID in the image, and then extracts the fisherman's ID, which contains 16 Persian digits, and displays the extracted Persian numbers in the output.

enter image description here

I write this code but the ocr can't extract all of numbers

using System;
using System.Drawing;
using System.Text.RegularExpressions;
using Tesseract;
using Rectangle = System.Drawing.Rectangle;


// Load the image
Bitmap image = new Bitmap("C:\\Users\\4020302\\Iron\\f.PNG");

var pix2=Pix.LoadFromFile("C:\\Users\\4020302\\Iron\\f.PNG");
var adadha = "";

using (var engine = new TesseractEngine(@"./tessdata", "fas", EngineMode.Default))
{
    // Create an instance of Page
    using (var page = engine.Process(pix2))
    {
        // Get an iterator over the text elements
        using (var iter = page.GetIterator())
        {
            // Loop through the iterator
            do
            {
                // Get the text of the current element
                var text = iter.GetText(PageIteratorLevel.Word);

                // Check if the text matches the sayad pattern
                var regex = new Regex("^[\u06F0-\u06F90-9]+$");
                var match = regex.Match(text);
                if (match.Success)
                {
                    // Get the bounding box of the current element

                    Rect res;
                    var rect = iter.TryGetBoundingBox(PageIteratorLevel.Word, out res);

                    //// Crop the image using the bounding box
                    var subImage = CropImage(image, res);

                    //// Save or display the sub-image
                    subImage.Save("sayad_id.png");
                    Console.WriteLine("The sayad id in the image is: " + text);
                    Console.WriteLine("The location of the sayad id is: " + rect);
                    adadha += text;
                }
            } while (iter.Next(PageIteratorLevel.Word));
        }
    }
}
File.WriteAllText("C:\\Users\\4020302\\Iron\\f.txt", adadha);

// A method to crop an image using a rectangle
 static Bitmap CropImage(Bitmap source, Rect area)
{
    Bitmap target = new Bitmap(area.Width, area.Height);
    using (Graphics g = Graphics.FromImage(target))
    {
        g.DrawImage(source, new Rectangle(0, 0, target.Width, target.Height),
            area.X1, area.Y1, area.Width, area.Height,
            GraphicsUnit.Pixel);
    }

    return target;
} 
0

There are 0 answers