简体   繁体   English

尝试使用 pytesseract 从图像中读取文本但变得空白

[英]Trying to read text from image using pytesseract but getting blank

I've taken a few pictures, and am using openCV to crop these images so i only have the relevant text.我拍了几张照片,正在使用 openCV 裁剪这些图像,所以我只有相关的文字。 This is the picture i've taken (ie the cropped photo):这是我拍的照片(即裁剪后的照片):裁剪图像

I try to feed this image to the image_to_string function of pytesseract but when i print the output this is what i get我尝试将此图像提供给image_to_string的 image_to_string function 但是当我打印 output 这就是我得到的

text from cropped image from code is '
♀ '

Any help as to how i can get the exact reading.关于如何获得准确读数的任何帮助。 Tried using尝试使用

text2 = pytesseract.image_to_string(cropped_image) ,config='--psm 6') 

but this gives a garbage value但这给出了一个垃圾值

lCould you please try with a different psm config? l你能试试不同的psm配置吗? Please note you dont have to close the cropped image with a parenthesis as you did.请注意,您不必像以前那样用括号关闭裁剪后的图像。

text2 = pytesseract.image_to_string(cropped_image, config='--psm 3')

You could aslo try adding "en" method just for extra testing like below您也可以尝试添加“en”方法来进行额外的测试,如下所示

text2 = pytesseract.image_to_string(cropped_image, lang='eng', config='--psm 3')

I was able to get a better result with a little preprocessing.通过一些预处理,我能够获得更好的结果。

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2_imshow(gray)
th2 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,17,6)
cv2_imshow(th2)
kernel = np.ones((5,5),np.uint8)
closing = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, kernel,iterations=1)
cv2_imshow(closing)

erosion = cv2.erode(closing,np.ones((5,5),np.uint8),iterations = 1)
cv2_imshow(erosion)

custom = '--psm 6'
txt = pytesseract.image_to_string(erosion, config=custom, lang='eng')
print(txt)

I cropped your image to remove the unnecessary black borders and tried adaptive thresholding followed by some morphological operations.我裁剪了您的图像以删除不必要的黑色边框并尝试了自适应阈值,然后进行了一些形态学操作。 Here is the result这是结果在此处输入图像描述

You can play with the adaptive thresholding and morphological transformations to get accurate results.您可以使用自适应阈值和形态变换来获得准确的结果。 The results would be accurate if it is possible to remove the green color noise from the image(subtract background from image) or even apply gamma correction to make only the text visible.如果可以从图像中去除绿色噪声(从图像中减去背景),或者甚至应用伽马校正以仅使文本可见,则结果将是准确的。 Pre-processing is the main thing to get accurate results.预处理是获得准确结果的主要内容。

Tarun Chakitha is right, you'll need some pre-processing, thresholding, and morphological transformations to get reliable results. Tarun Chakitha 是对的,您需要进行一些预处理、阈值处理和形态学转换才能获得可靠的结果。 The following code produces Pac=2666. 1W以下代码生成Pac=2666. 1W Pac=2666. 1W

# Obtain binary image
img_bgr = cv2.imread("3CxLj.jpg")
img_gray = cv2.cvtColor(img_bgr[90:200, 0:495], cv2.COLOR_BGR2GRAY)
img_bin = cv2.adaptiveThreshold(
    img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 15
)
fig, axs = plt.subplots(3)
axs[0].imshow(img_gray, cmap="gray")
axs[1].imshow(img_bin, cmap="gray")

# Merge dots into characters using erosion
kernel = np.ones((5, 5), np.uint8)
img_eroded = cv2.erode(img_bin, kernel, iterations=1)
axs[2].imshow(img_eroded, cmap="gray")
fig.show()

# Obtain string using psm 8 (treat the image as a single word)
ocr_string = pytesseract.image_to_string(img_eroded, config="--psm 8")
print(ocr_string)

图像灰度、二进制和侵蚀

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

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