PyTesseract image_to_data 函数无法识别我的图像

PyTesseract image_to_data function isn't recognizing my image

我正在使用 pytesseract return 图像中对象的坐标。

通过使用这段代码:

import pytesseract
from pytesseract import Output
import cv2
img = cv2.imread('wine.jpg')

d = pytesseract.image_to_data(img, output_type=Output.DICT)
    print(d)

for i in range(n_boxes):
    (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.imshow('img', img)
cv2.waitKey(0)

我明白了:

{'level': [1, 2, 3, 4, 5, 5, 2, 3, 4, 5, 4, 5, 2, 3, 4, 5], 'page_num': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 'block_num': [0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3], 'par_num': [0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1], 'line_num': [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1], 'word_num': [0, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1], 'left': [0, 485, 485, 485, 485, 612, 537, 537, 555, 555, 537, 537, 454, 454, 454, 454], 'top': [0, 323, 323, 323, 323, 324, 400, 400, 400, 400, 426, 426, 0, 0, 0, 0], 'width': [1200, 229, 229, 229, 115, 102, 123, 123, 89, 89, 123, 123, 296, 296, 296, 296], 'height': [900, 29, 29, 29, 28, 28, 40, 40, 15, 15, 14, 14, 892, 892, 892, 892], 'conf': ['-1', '-1', '-1', '-1', 58, 96, '-1', '-1', '-1', 95, '-1', 95, '-1', '-1', '-1', 95], 'text': ['', '', '', '', "JACOB'S", 'CREEK', '', '', '', 'SHIRAZ', '', 'CABERNET', '', '', '', '']} 

[使用的图像][]1

但是,当我使用这张图片时:

我明白了:

{'level': [1, 2, 3, 4, 5], 'page_num': [1, 1, 1, 1, 1], 'block_num': [0, 1, 1, 1, 1], 'par_num': [0, 0, 1, 1, 1], 'line_num': [0, 0, 0, 1, 1], 'word_num': [0, 0, 0, 0, 1], 'left': [0, 0, 0, 0, 0], 'top': [0, 162, 162, 162, 162], 'width': [1200, 0, 0, 0, 0], 'height': [900, 276, 276, 276, 276], 'conf': ['-1', '-1', '-1', '-1', 95], 'text': ['', '', '', '', '']}

知道为什么有些图片有效而有些无效吗?

主要是画质和对比度不同造成的。 OCR 引擎更容易检测所需图像中的文本。 您可以添加一些预处理例程,包括阈值处理、模糊处理、直方图均衡化和许多其他技术。它主要是主观的,所以我无法为您提供工作代码,更像是为您的范围找到最佳技术的反复试验

更新: 这是一个可能对您有帮助的代码

def preprocessing_typing_detection(inputImage):
    inputImage= cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
    inputImage= cv2.Laplacian(inputImage, cv2.CV_8U)
    return inputImage