opencv Pytesseract检测 Jmeter 阅读效果不佳

h43kikqp  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(156)

我正在尝试开发一个阅读表检测系统,这是图片

我需要得到 Jmeter 阅读27599作为输出。我使用了以下代码:

import pytesseract
import cv2

image = cv2.imread('read2.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
(H, W) = gray.shape

rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 7))

gray = cv2.GaussianBlur(gray, (1, 3), 0)
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)

res = cv2.threshold(blackhat, 0, 255, cv2.THRESH_BINARY_INV  + cv2.THRESH_OTSU)[1]

pytesseract.image_to_string(res, config='--psm 12 --oem 3 digits')

我得到这个输出:

'.\n\n-\n\n3\n\n7\n\n7\n\n3\n\n-2105 566.261586\n\n161200\n\n310010\n\n--\n\n.-\n\n.\n\n5\n\x0c'

这是我的第一个光学字符识别项目。任何帮助将不胜感激。

4nkexdtk

4nkexdtk1#

在我们开始阅读实际的电表号码之前,有很多文本可以删除。另一方面,我们可以将OCR限制为只读取数字,以防止误报(因为一些7段数字就像字母表中的字母)。
由于tesseract在7段数字上工作得不够好。我将使用EasyOCR。
因此,程序应该是这样的:
1.实际计数器周围有很大的空间可以裁剪。
1.我们模糊图像并运行霍夫变换以得到圆形米。
1.我们肯定知道该数字位于该圆的上半部分,因此我们再次根据检测到的圆的中心和半径进行裁剪。
1.裁剪后的图像然后可以输入EasyOCR,正如我之前所说,仅限于英语和数字。

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import easyocr

cropped = orig_image[300:850,:][:,200:680]
cropped_height, cropped_width, _ = cropped.shape

gray = cv.cvtColor(cropped, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (17,17),0)

minDist = 100
param1 = 30 
param2 = 50
minRadius = 100
maxRadius = 300

circle_img = cropped.copy()
circles = cv.HoughCircles(blurred, cv.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
print(f"{len(circles)} circles detected", circles[0,:][0])
if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv.circle(circle_img, (i[0], i[1]), i[2], (0, 255, 0), 2)

circle = circles[0,:][0]
circle_center = (circle[0], circle[1]) # x, y
circle_radius = circle[2]

color_cropped = cropped[circle_center[1] - circle_radius : circle_center[1],:]

reader = easyocr.Reader(['en'], gpu=False)
result = reader.readtext(color_cropped, allowlist ='0123456789')
if result:
    print("detected number: ", result[0][1])

检测数量:27599

相关问题