簡體   English   中英

pytesseract未檢測到數字

[英]pytesseract not detecting numbers

我正在嘗試閱讀的內容

我試圖閱讀的圖片正在輸出“一個”。 我不知道它是怎么得到的。 我的代碼:

left = 980
right = 1000
top = 237
bottom = 265
CroppedImage = cropimage.crop((left,top,right,bottom))
if os.path.isfile("Price.png"):
        os.remove("Price.png")
CroppedImage.save('Price.png', 'PNG')


Check_Price = pytesseract.image_to_string(Image.open('Price.png'), lang='eng')
Check_Price = Check_Price[:-2]
if len(Check_Price) == 4:
    Found_Price = True
print(Check_Price)

我已經正確安裝了 pytesseract 和 PIL。 這對我擁有的另外 2 個都有效,但它無法閱讀本文。

您需要放大圖像,使用此代碼我可以獲得您需要的號碼。

import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

def read():
    i = Image.open('./JA9JF.png')
    scale = 2.1
    i_width = i.size[1] * scale
    i_height = i.size[0] * scale
    i = i.resize((int(i_width), int(i_height)), Image.BILINEAR)
    xconfig = "-c page_separator=''"
    Check_Price = pytesseract.image_to_string(i, lang='eng', config=xconfig)
    print(Check_Price)

read()

所以你的代碼會是。

left = 980
right = 1000
top = 237
bottom = 265
CroppedImage = cropimage.crop((left,top,right,bottom))
if os.path.isfile("Price.png"):
        os.remove("Price.png")
CroppedImage.save('Price.png', 'PNG')

i = Image.open('Price.png')
scale = 2.1
i_width = i.size[1] * scale
i_height = i.size[0] * scale
i = i.resize((int(i_width), int(i_height)), Image.BILINEAR)

xconfig = "-c page_separator=''"
Check_Price = pytesseract.image_to_string(i, lang='eng', config=xconfig)
Check_Price = Check_Price[:-2]
if len(Check_Price) == 4:
    Found_Price = True
print(Check_Price)

我添加了xconfig = "-c page_separator=''"因為對我來說 pytesseract 出於某種原因添加了 \\n\\x0c 。 我也不知道這是否適用於其他數字,如果不是,我將不得不使用 cv2 修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM