簡體   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