簡體   English   中英

Tesseract OCR 圖像識別失敗,因為“警告:分辨率無效”錯誤

[英]Tesseract OCR image recognition failed because of `Warning: Invalid resolution` error

我試圖從圖像中檢測文本,我在選擇字符周圍繪制邊界框並將它們拼接在一起以形成另一個圖像,如下所示:

最終拼接的圖像

我使用 cv2 使用以下代碼在字符周圍繪制邊界框:

cnts = cv2.findContours(inverted, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, bounding_boxes) = sort_contours(cnts)
ROI_number = 0
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    ROI = inverted[y:y + h, x:x + w]
    # ROI = cv2.erode(ROI, kernel, iterations=1)
    ROI = cv2.filter2D(ROI, -1, sharpen_kernel)
    # ROI = cv2.GaussianBlur(ROI, (5, 5), 0)
    # ROI = cv2.filter2D(ROI, -100, sharpen_kernel)
    ROI = cv2.bitwise_not(ROI)
    ht, wd = ROI.shape
    ww = 26
    hh = 30
    result = np.full((hh, ww), 255, dtype=np.uint8)
    xx = (ww - wd) // 2
    yy = (hh - ht) // 2
    result[yy:yy + ht, xx:xx + wd] = ROI
    cv2.imwrite('ROI_{}.jpeg'.format(ROI_number), result)
    cv2.rectangle(inverted, (x, y), (x + w, y + h), (36, 255, 12), 0)
    ROI_number += 1

我使用numpy 中的 hstack使用以下代碼將圖像拼接在一起:

def stitch_images(input_path):
imagePaths = []
for image_path in glob.glob(os.path.join(input_path, '*.jpeg')):
    imagePaths.append(image_path)
sorted_paths = sorted(imagePaths)
list_im = [sorted_paths[0], sorted_paths[1], sorted_paths[2], sorted_paths[3], sorted_paths[4], sorted_paths[5]]
imgs = [Image.open(i) for i in list_im]
min_shape = sorted([(np.sum(i.size), i.size) for i in imgs])[0][1]
imgs_comb = np.hstack((np.asarray(i.resize(min_shape)) for i in imgs))
imgs_comb = Image.fromarray(imgs_comb)
imgs_comb.save('stitched.jpeg')

但是,無法使用 tesseract 讀取拼接圖像並出現以下錯誤:

Tesseract Open Source OCR Engine v4.1.1-rc2-21-gf4ef with Leptonica
Warning: Invalid resolution 0 dpi. Using 70 instead.
Estimating resolution as 334
Empty page!!
Estimating resolution as 334
Empty page!

加載圖像,轉換為灰度,並使用image_to_string對我image_to_string pytesseract 的結果:

418081

代碼

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Perfrom OCR with Pytesseract
data = pytesseract.image_to_string(gray, lang='eng', config='--psm 6')
print(data)
------------------
System information
------------------
Python:  3.7.4
NumPy:   1.14.5
OpenCV:  4.1.0
------------------

暫無
暫無

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

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