简体   繁体   English

在双色调背景下使用 pytesseract 从图像中提取文本

[英]Extract text from image using pytesseract in two-tones background

I'm trying to extract text from an image using pytesseract on Python. This is the image where I want to extract the text:我正在尝试在 Python 上使用 pytesseract 从图像中提取文本。这是我要提取文本的图像:

Original Image原图

This is the image after applying threshold:这是应用阈值后的图像:

Image with threshold带阈值的图像

Console Output:控制台 Output:

20 hours

20 hours

Bhours

Console Output Image控制台 Output 图片

This is the code I'm using:这是我正在使用的代码:

from pytesseract import *
import cv2

path = r"path where image is located"             #path of image
folderPath = r"path for saving output image" 

grey_image = cv2.imread(path,0)                   #import image

_,bt = cv2.threshold(grey_image, 150 ,255,cv2.THRESH_BINARY)   #variable means binary threshold

cv2.imwrite(folderPath + "\\" + "test.png", bt)   #Saving result

imageout = pytesseract.image_to_string(bt)        #Convert image to text

print(imageout)                                   #Print text in console

I've been trying different ranges of thresholding but still can't get a precise output.我一直在尝试不同的阈值范围,但仍然无法获得精确的 output。

What do you suggest for getting a precise result?您对获得精确结果有何建议?

Because you are dealing with an image containing white characters over a dark background, it is recommended to invert it prior using pytesseract.因为您正在处理深色背景上包含白色字符的图像,所以建议在使用 pytesseract 之前将其反转。

This is done using inverted_grey_image = cv2.bitwise_not(grey_image) .这是使用inverted_grey_image = cv2.bitwise_not(grey_image)完成的。

You may then adjust the threshold in threshold : _,bt = cv2.threshold(inverted_grey_image, 140,255,cv2.THRESH_BINARY)然后,您可以在 threshold 中调整阈threshold_,bt = cv2.threshold(inverted_grey_image, 140,255,cv2.THRESH_BINARY)

Below is the full code:以下是完整代码:

from pytesseract import *
import cv2

path = r"path where image is located"             #path of image
folderPath = r"path for saving output image" 

grey_image = cv2.imread(path,0)                   #import image

inverted_grey_image = cv2.bitwise_not(grey_image)

_,bt = cv2.threshold(inverted_grey_image, 140 ,255,cv2.THRESH_BINARY)   #variable means binary threshold

cv2.imwrite(folderPath + "/" + "test.png", bt)   #Saving result

imageout = pytesseract.image_to_string(bt)        #Convert image to text


print(imageout)  

It returns:它返回:

20 hours 20小时

20 hours 20小时

3 hours 3小时

EDIT: I am currently dealing with a similar OCR problem: you may want to check this recent post for inspiration.编辑:我目前正在处理类似的 OCR 问题:您可能想查看这篇最近的帖子以获取灵感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM