后处理并读取模糊的数字-OpenCV / tesseract

问题描述

我的老板给了我9000张显示万用表输出的图像,他希望我将它们全部读取并在星期一早上将输出电压写在文本文件中!

我无法手动完成此操作,因此,我迫切需要您的帮助以使该过程自动化。

enter image description here

我已经做了一些编码。万用表的屏幕位置固定在9000张图像上,因此我只裁剪了图片并在屏幕上放大了。

到目前为止,这是我的代码

import PyTesseract as tess
tess.PyTesseract.tesseract_cmd = r'D:\Programs\Tesseract-OCR\tesseract.exe'
from PIL import Image
from matplotlib import pyplot as plt
import cv2
import numpy as np

img = cv2.imread('test1.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(7,7),0)
ret,BW = cv2.threshold(gray,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)

plt.imshow(BW)
plt.show()

print(tess.image_to_string(img,config='digits --oem 3'))

我的代码输出以下图片

enter image description here

对于这张照片,tesseract读取以下数字:138

在这里有两个问题:第一个是我不知道如何正确处理我的照片,以便使其易于阅读。

第二个问题:我不知道如何处理小数点。实际上,在某些图片上没有小数点,因此我需要找到一种使tesseract读取它的方法

您认为我需要手动“训练” tesseract,以使我达到接近100%的准确性吗?

非常感谢您的帮助,同时我将继续研究OpenCv函数

这是原始文件之一:

enter image description here

更新版本:

好的,到目前为止,我已经更新了一些代码

# Loading the picture
img = cv2.imread('test1.JPG')

# Crop - Rotate - Scale up
x = 1400
y = 1375
h = 325
w = 800
img = img[y:y+h,x:x+w]
image = cv2.rotate(img,cv2.ROTATE_180)

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray,128,cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,10))
close = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=1)

cv2.imshow('close',close)

x = 0
y = 0
h = 300
w = 750
img = close[y:y+h,x:x+w]

plt.imshow(img)
plt.show()

print(tess.image_to_string(img,config='--psm 7 -c tessedit_char_whitelist=0123456789 --oem 0'))

我得到了更好的图像供tesseract分析。但小数点:/。仍然没有运气。

enter image description here

解决方法

您大约需要3-4个小时来手动输入内容,只需花1个多小时来编写一个打开图片的包装程序,然后输入一个值即可。

自动化时,我最大的担心是,如果所有测量值都具有相同的千欧姆单位,那么您应该真正将该检查包括在open-cv代码中。

祝你好运!