繁体   English   中英

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

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

我拍了几张照片,正在使用 openCV 裁剪这些图像,所以我只有相关的文字。 这是我拍的照片(即裁剪后的照片):裁剪图像

我尝试将此图像提供给image_to_string的 image_to_string function 但是当我打印 output 这就是我得到的

text from cropped image from code is '
♀ '

关于如何获得准确读数的任何帮助。 尝试使用

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

但这给出了一个垃圾值

l你能试试不同的psm配置吗? 请注意,您不必像以前那样用括号关闭裁剪后的图像。

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

您也可以尝试添加“en”方法来进行额外的测试,如下所示

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

通过一些预处理,我能够获得更好的结果。

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)

我裁剪了您的图像以删除不必要的黑色边框并尝试了自适应阈值,然后进行了一些形态学操作。 这是结果在此处输入图像描述

您可以使用自适应阈值和形态变换来获得准确的结果。 如果可以从图像中去除绿色噪声(从图像中减去背景),或者甚至应用伽马校正以仅使文本可见,则结果将是准确的。 预处理是获得准确结果的主要内容。

Tarun Chakitha 是对的,您需要进行一些预处理、阈值处理和形态学转换才能获得可靠的结果。 以下代码生成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