Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (85 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How to Implement Optical Character Recognition in Python

Last updated on Jul 15,2021 16.4K Views


Optical Character Recognition is vital and a key aspect and python programming language. The application of such concepts in real-world scenarios is numerous. In this article, we will discuss how to implement Optical Character Recognition in Python

 

Applications of Optical Character Recognition

Ticket counters use this extensively for scanning and detecting of key information on the ticket to track routes and commuters details. Conversion of paper text into digital formats where cameras capture high-resolution photographs and then OCR is used to bring them into a word or a PDF format.

charachters

The introduction of OCR with python is credited to the addition of versatile libraries like “Tesseract” and “Orcad”. These libraries have helped many coders and developers to simplify their code design and allow them to spend more time on other aspects of their projects. Since the benefits are enormous, let us peek into what it is and how it is done.

 

Building an Optical Character Recognition in Python

We first need to make a class using “pytesseract”. This class will enable us to import images and scan them. In the process it will output files with the extension “ocr.py”. Let us see the below code. The function block “process_image” is used to sharpen the text we get.

The following route handler and view function are added to the application (app.py).

Router Handler Code

//ROUTE HANDLER
@app.route('/v{}/ocr'.format(_VERSION), methods=["POST"])
def ocr():
    try:
        url = request.json['image_url']
        if 'jpg' in url:
            output = process_image(url)
            return jsonify({"output": output})
        else:
            return jsonify({"error": "only .jpg files, please"})
    except:
        return jsonify(
            {"error": "Did you mean to send: {'image_url': 'some_jpeg_url'}"}
        )

 

OCR Engine Code

// OCR ENGINE
import pytesseract
import requests
from PIL import Image
from PIL import ImageFilter
from StringIO import StringIO

def process_image(url):
    image = _get_image(url)
    image.filter(ImageFilter.SHARPEN)
    return pytesseract.image_to_string(image)

def _get_image(url):
    return Image.open(StringIO(requests.get(url).content))
//

 

Please ensure to update the imports and add API version number.

import os
import logging
from logging import Formatter, FileHandler
from flask import Flask, request, jsonify

from ocr import process_image
_VERSION = 1  # API version

We are adding in the JSON response of the OCR Engine’s function that is “process_image()”. JSON is used for gathering information going into and out of the API. We pass the response in an object file using “Image” library from PIL to install it.

Please note that this code only performs best with .jpg images only. If we use complex libraries that can feature multiple image formats then all images can be processed effectively. Also note, in case you are interested in trying out this code by yourself, then please install PIL which is procured out of “Pillow” library first

• Start out by running the app, which is “app.py”:

//
$ cd ../home/flask_server/
$ python app.py
//

 

• Then, in another terminal run:

//$ curl -X POST http://localhost:5000/v1/ocr -d '{"image_url": "some_url"}' -H "Content-Type: application/json"

For Example:

//
$ curl -X POST http://localhost:5000/v1/ocr -d '{" C:UsersakashDownloadsPic1 ": "https://edureka.com/images/blog_images/ocr/ocr.jpg"}' -H "Content-Type: application/json"
{
  "output": "ABCDEnFGH I JnKLMNOnPQRST"
}
//

Optical Character Recognition in Python

 

 

Advantages and Disadvantages of OCR Engine

Out of the many applications of using OCR in python, the popular one is handwriting recognition. People apply this is to recreate written text which can then be populated into numerous copies rather than just photocopying the original script. This is to bring about uniformity and legibility.

OCR is also useful in converting PDF’s to texts and store them as variables. This can later be then subjected to any amount of pre-processing for additional tasks. Although the concept of OCR seems to be a beneficial topic in the world of Python, it sure does share its part of disadvantages.

The OCR cannot always guarantee 100% accuracy. A lot of hours of training need to be applied with the help of Artificial Intelligence concepts which can enable the OCR engine to learn and recognize poor images. Handwriting images can be recognized but they depend on several factors like the style of the writing, the color of the page, the contrast of the image and the resolution of the image.

With this, we come to an end of this Optical Character Recognition in Python article. I hope you go an understanding of how exactly OCR works.

To get in-depth knowledge on Python along with its various applications, you can enroll now for the best Python course training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of “Optical Character Recognition in Python” and we will get back to you.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Implement Optical Character Recognition in Python

edureka.co