Domain Name For Sale

Unlock the Potential of Your Premium Domain for Python in Deep Learning and Machine Learning!

Are you passionate about Python and its incredible applications in the world of deep learning and machine learning? Do you own a domain that...

Sunday, July 30, 2023

Deep Learning with URDU OCR: Text Recognition Implementation in Python


Optical Character Recognition (OCR) stands as one of the most remarkable breakthroughs in the field of computer vision and natural language processing. The ability to transform images containing printed or handwritten text into editable and machine-readable formats has revolutionized diverse industries, from archiving historical documents to enhancing automated data entry systems. In this comprehensive article, we embark on a detailed journey into the world of OCR and delve into a practical implementation using the power of deep learning to recognize text in images. By the end of this exploration, readers will gain an in-depth understanding of the underlying mechanisms behind OCR, the significance of pre-trained deep learning models, and the intricacies involved in converting dense model outputs to intelligible human-readable text.


import tensorflow as tf
import cv2 as cv
import utils

path = "test.jpg"
image = cv.imread(path)

sess = tf.Session()
model = tf.saved_model.loader.load(sess ,tags = ['serve'], export_dir = 'model_pb')

resized_image = tf.image.resize_image_with_pad(image, 64, 1024).eval(session = sess)
img_gray = cv.cvtColor(resized_image, cv.COLOR_RGB2GRAY).reshape(64,1024,1)

output ='Dense-Decoded/SparseToDense:0', 
         feed_dict = {
output_text = utils.dense_to_text(output[0])

Understanding the Code

1. Importing Libraries

Our journey commences with the importation of essential libraries that serve as the bedrock of our OCR implementation. TensorFlow, a pioneering deep learning framework, equips us with the tools needed to create and train neural networks for OCR tasks. OpenCV, a versatile computer vision library, aids us in image processing and loading. Additionally, the utility of NumPy ensures efficient numerical computations, while the custom `utils` module houses crucial functions that simplify the conversion of the model's dense output to human-readable text.

2. Loading the Input Image

As a pivotal step in OCR, the input image, containing the text of interest, is essential for our implementation. The code allows flexibility in specifying the image path directly or receiving it interactively from the user. OpenCV's `cv.imread()` skillfully reads the image, and the obtained image is stored in the `image` variable, ready for further processing.

3. Loading the Pre-trained Model

The true power behind our OCR implementation lies in the availability of a pre-trained deep learning model proficient in recognizing text. With a TensorFlow session established, we confidently load the pre-trained model using the `tf.saved_model.loader.load()` function. By specifying the 'serve' tag, we ensure the model is retrieved in a state ready for serving predictions.

4. Preparing the Input Image for Prediction

To maximize the OCR model's prediction accuracy, we diligently preprocess the input image. Through resizing the image to a standardized size of 64x1024 using TensorFlow's `tf.image.resize_image_with_pad()` function, we ensure compatibility with the model's expectations. Furthermore, leveraging OpenCV's `cv.cvtColor()`, we expertly convert the resized image to grayscale, a step often enhancing OCR performance. After this conversion, the image is reshaped to possess a single channel, thereby facilitating ease of data handling, and subsequently, it is stored in the `img_gray` variable, primed for prediction.

5. Making Predictions

With the input image appropriately preprocessed and the model loaded, the time has come for us to embark on the prediction phase. By leveraging the TensorFlow session `` function, we orchestrate the flow of data through the model's computational graph. Armed with the input tensor's name ('Deep-CNN/Placeholder:0') and a feed dictionary containing the preprocessed image, we eagerly await the output, which is saved in the `output` variable.

6. Converting the Prediction to Text

The fruits of our labor materialize as a dense representation of the recognized characters, housed within the `output` tensor. However, our ultimate goal lies in obtaining human-readable text from this numerical representation. Enter the custom `utils.dense_to_text()` function, a masterful creation that artfully maps the character indices to their respective characters using the character set previously loaded from the 'chars.txt' file. This conversion lays the foundation for presenting the final recognized text, serving as the culmination of our OCR journey.


In conclusion, our exploration into deep learning-based OCR has shed light on a transformative technology that transcends the boundaries of mere image recognition. Through the process of loading pre-trained models, preprocessing input images, making predictions, and converting dense model outputs to human-readable text, we have unveiled the intricacies involved in text recognition from images. The far-reaching applications of OCR, from historical document preservation to streamlined data extraction, are a testament to its lasting impact on a myriad of industries. As the realm of deep learning and computer vision continues to evolve, OCR will undoubtedly stand at the forefront, empowering us to interact seamlessly with textual information in the digital age and beyond.

No comments:

Post a Comment