是否可以使用pytesseract从图像的特定部分提取文本

问题描述 投票:-1回答:1

我在图像中具有边框(矩形的坐标),并希望在该坐标内提取文本。如何使用pytesseract在该坐标内提取文本?

我尝试使用opencv将图像部分复制到其他numpyarray中,如

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

并尝试pytesseract.image_to_string()。但是准确性很差。但是,当我尝试将原始图像用于pytesseract.image_to_string()时,它完美地提取了所有内容。.

是否有使用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?

请帮助在此先感谢

我使用的版本:Tesseract 4.0.0pytesseract 0.3.0OpenCv 3.4.3

python opencv ocr text-extraction python-tesseract
1个回答
0
投票

[没有内置函数可以使用Pytesseract提取图像的特定部分,但是我们可以使用OpenCV提取ROI边界框,然后将此ROI放入Pytesseract。我们将图像转换为灰度,然后将其转换为阈值以获得二进制图像。假设您具有所需的ROI坐标,我们使用Numpy切片来提取所需的ROI

enter image description here

[从这里我们将其扔到Pytesseract以得到结果

ONLINE FOOD DELIVERY SYSTEM

代码

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()
© www.soinside.com 2019 - 2024. All rights reserved.