首页 > 解决方案 > OCR 不提取任何文本

问题描述

我正在尝试从看起来像 OCR 应该能够轻松提取的图像中提取文本,但在某些情况下它只会提取任何内容或垃圾。

我从其他 stackoverflow 资源中尝试了以下 OpenCV 技术,但似乎没有任何帮助。

如果有人可以帮助我如何使用 OpenCV 从附加图像中提取文本,那将非常有帮助

示例图像

我的代码

import pytesseract
import cv2
import numpy as np

# Configuration
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image_path = "C:/Users//opencv_test/images/"
ocr_path = "C:/Users//opencv_test/ocr/"
doc_file_name = "Image.jpeg"
ocr_file_name = doc_file_name[:-4] + "txt"

# To read image from disk, we use
# cv2.imread function, in below method,
img = cv2.imread(image_path + doc_file_name, 1)

# Creating GUI window to display an image on screen
# first Parameter is windows title (should be in string format)
# Second Parameter is image array
cv2.imshow("Actual Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


def remove_noise(img):
    filtered = cv2.adaptiveThreshold(img.astype(np.uint8), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199, 5)
    cv2.imshow("Noise Free Image", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return filtered

def remove_noise_2(img):
    img = cv2.resize(img, None, fx=.9, fy=.8, interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    cv2.imshow("Pre-Processed Image", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return img


noise_free_img = remove_noise_2(img)

# Run Tesseract
f = open(ocr_path + ocr_file_name, 'w')
text = str((pytesseract.image_to_string(noise_free_img, config='--psm 3')))
text = text.replace('-\n', '')
f.write(text)

标签: pythonocr

解决方案


推荐阅读