Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extract text from specific portion of image using pytesseract

I have bounding box(coordinate of rectangle) in an image and want to extract text within that coordinates. How can I use pytesseract to extract text within that coordinates?

I tried copying the image portion to other numpyarray using opencv like

cropped_image = image[y1:y2][x1:x2]

and tried pytesseract.image_to_string(). But the accuracy was very poor. But when I tried original image to pytesseract.image_to_string() it extracted every thing perfectly..

Is there any function to extract specific portion of image using pytesseract?

This image has different sections of information consider I have rectangle coordinates enclosing 'Online food delivering system' how to extract that data in pytessaract?

Please help Thanks in advance

Versions I am using: Tesseract 4.0.0 pytesseract 0.3.0 OpenCv 3.4.3

like image 912
Prem Kumar P Avatar asked Nov 20 '19 07:11

Prem Kumar P


People also ask

How do I extract text from an image using Tesseract?

Create a Python tesseract script Create a project folder and add a new main.py file inside that folder. Once the application gives access to PDF files, its content will be extracted in the form of images. These images will then be processed to extract the text.

Can you extract the text from an image?

You can capture text from a scanned image, upload your image file from your computer, or take a screenshot on your desktop. Then simply right click on the image, and select Grab Text. The text from your scanned PDF can then be copied and pasted into other programs and applications.

What can you do with Pytesseract?

Pytesseract or Python-tesseract is an OCR tool for python that also serves as a wrapper for the Tesseract-OCR Engine. It can read and recognize text in images and is commonly used in python ocr image to text use cases.

How to extract text from image using Python tesseract?

pytesseract.image_to_string is used to detects the image and extracts text and saved as a string #extracting text in the image text = pytesseract.image_to_string (img, lang='eng') a file name "recognized_text" is created to export the recognized text #creating a file with name recognized file = open ("recognized_text.txt", "w+") file.close ()

How to use image_to_string function in pytesseract?

Now you have to pass that image into pytesseract module. image_to_string returns the result of a Tesseract OCR run on the image to string. Then finally print the text. Now run the above code and check the output.

What is pytesseract for OCR?

Python provides a tool pytesseract for OCR. That is, it will recognize and “read” the text embedded in images. What Is pytesseract ? pytesseract will recognize and read the text present in images.

How to extract text from digital images using Python?

This article will give you a glimpse of extracting text from digital images. We will use python and pytesseract library to extract the text. The image should have text inside it to find the output text. The extraction of text with pytesseract needs a library to be installed in the system environment.


1 Answers

There's no built in function to extract a specific portion of an image using Pytesseract but we can use OpenCV to extract the ROI bounding box then throw this ROI into Pytesseract. We convert the image to grayscale then threshold to obtain a binary image. Assuming you have the desired ROI coordinates, we use Numpy slicing to extract the desired ROI

enter image description here

From here we throw it into Pytesseract to get our result

ONLINE FOOD DELIVERY SYSTEM

Code

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.jpg', 0)
thresh = 255 - cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

x,y,w,h = 37, 625, 309, 28  
ROI = thresh[y:y+h,x:x+w]
data = pytesseract.image_to_string(ROI, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('ROI', ROI)
cv2.waitKey()
like image 94
nathancy Avatar answered Oct 04 '22 01:10

nathancy