是否可以使用pytesseract从图像的特定部分提取文本 [英] Is it possible to extract text from specific portion of image using pytesseract
问题描述
我在图像中有边界框(矩形的坐标)并想在该坐标内提取文本.如何使用 pytesseract 提取该坐标内的文本?
我尝试使用 opencv 将图像部分复制到其他 numpyarray
cropped_image = image[y1:y2][x1:x2]
并尝试了 pytesseract.image_to_string().但准确度非常差.但是当我尝试将原始图像转换为 pytesseract.image_to_string() 时,它完美地提取了所有内容..
是否有使用 pytesseract 提取图像特定部分的功能?
从这里我们把它扔到 Pytesseract 中得到我们的结果
在线食品配送系统
代码
导入 cv2导入 pytesseractpytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"图像 = 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投资回报率 = thresh[y:y+h,x:x+w]数据 = pytesseract.image_to_string(ROI, lang='eng',config='--psm 6')打印(数据)cv2.imshow('thresh', thresh)cv2.imshow('ROI', ROI)cv2.waitKey()
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?
Please help Thanks in advance
Versions I am using: Tesseract 4.0.0 pytesseract 0.3.0 OpenCv 3.4.3
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
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()
这篇关于是否可以使用pytesseract从图像的特定部分提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!