opencv 使用OCR python读取图像上的数字

new9mtju  于 2023-01-21  发布在  Python
关注(0)|答案(1)|浏览(179)

我尝试在Python和tesseract中使用OpenCV提取图像上的数字。下面是我的尝试,但一无所获。代码没有返回预期的数字

import fitz, pytesseract, os, re
import cv2

sTemp = "Number.png"
directory = '.\MyFolder'

def useMagick(img):
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    command = 'magick convert {} -resize 1024x640 -density 300 -quality 100 {}'.format(img, sTemp)
    os.system(command)

def readNumber(img):
    img = cv2.imread(img)
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    txt = pytesseract.image_to_string(gry)
    print(txt)
    try:
        return re.findall(r'\d+\s?\/\s?(\d+)', txt)[0]
    except:
        blur = cv2.GaussianBlur(gry, (3,3), 0)
        txt = pytesseract.image_to_string(blur)
        try:
            return re.findall(r'\d+\s?\/\s?(\d+)', txt)[0]
        except:
            return 'REVIEW'

sPath = os.path.join(directory, sTemp)
useMagick(sPath)
x = readNumber(sPath)
print(x)

以下是图像

的示例
这个代码没有返回任何数字。我怎样才能提高这样一个图像的质量,以便能够提取数字呢?

vi4fp9gy

vi4fp9gy1#

经过多次搜索,我终于解决了这个问题

import cv2
import numpy as np
import pytesseract
import os, re

sImagesPath = r'MyFolder/'
mylist = []

def replace_chars(text):
    list_of_numbers = re.findall(r'\d+', text)
    result_number = ''.join(list_of_numbers)
    return result_number

for root, dirs, file_names in os.walk(sImagesPath):
    for file_name in file_names:
        img = cv2.imread(sImagesPath + file_name)
        gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        thr = cv2.adaptiveThreshold(gry, 181, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 13, 10)
        txt = pytesseract.image_to_string(thr, lang='eng',config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
        mylist.append(replace_chars(txt))
        print(replace_chars(txt))

with open('Output.txt', 'w') as f:
    for i in mylist:
        s = ''.join(map(str, i))
        f.write(s + '\n')

相关问题