简体   繁体   English

Tesseract OCR 图像识别失败,因为“警告:分辨率无效”错误

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

I tried to detect text from an image where I draw bounding boxes around select characters and stitch them together to form another image as below :我试图从图像中检测文本,我在选择字符周围绘制边界框并将它们拼接在一起以形成另一个图像,如下所示:

最终拼接的图像

I used cv2 to draw bounding boxes around the characters using the following code :我使用 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

I have used hstack from numpy to stitch the image together using the following code:我使用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')

The stitched image however cannot be read using tesseract and gives the following error:但是,无法使用 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!

Loading the image, converting to grayscale, and using image_to_string is working for me.加载图像,转换为灰度,并使用image_to_string对我image_to_string Result from pytesseract: pytesseract 的结果:

418081 418081

Code代码

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