python - 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)
解决方案
推荐阅读
- python - 对 Python 的 SQL 查询,使用 Pandas
- django - Django 自定义用户模型错误 ((admin.E108) 'list_display[2]' 的值指的是 'first_name',它不是可调用的
- android - 在服务中使用 SMS 在 Android 中排队
- .net-core - 如何将 MediatR 与我的业务层分离
- php - Facebook 登录 JAvascript SDK、内容安全策略
- vba - 用于数据转换的 Excel 帮助
- java - 波斯字符串的对象序列化中的 String.contain
- python - Python请求响应得到错误的url
- javascript - 在 Javascript/Node 中将大数转换为字符串
- c++ - 找到顶点之间的最短路径,但在服务器中有错误