繁体   English   中英

如何从具有最小库的电表中识别数字?

[英]how get digits recognize from electric meter with minimum libs?

I am developing an application for android with python3 and kivy to which I want to add a functionality to automatically recognize the digits of the electric meter from the camera of the device, for which I have found a variety of solutions using opencv with numpy, mahotas 、pytesseract、scipy、scikit_learn 等软件包。

试:

https://github.com/VAUTPL/NUMBERS_DETECTION_1
https://github.com/spidgorny/energy-monitor

但是,我需要能够用最少的库有效地实现这一点,因为当使用 buildozer 生成 apk 时,我必须添加所有使用的库,这会生成一个太大的文件,只是为了添加这个功能。

您建议以最少数量的库来实现此目标吗?

理念:非数字化

编辑1:我需要从数字和非数字仪表中提取数字:数字仪表

一种方法是将图像处理方法与pytesseract结合起来。 Python-tesseract 是 python 的光学字符识别 (OCR) 工具对于当前示例,您需要执行颜色分割以获取二进制掩码。 接下来,您需要使用二进制掩码去除背景,然后使用 tesseract 读取 OCR 数字。

    1. 执行颜色分割:我们将加载的图像转换为 HSV 格式,定义下限/上限范围,并使用cv2.inRange执行颜色分割以获得二进制掩码。
    1. 提取数字:获得二进制掩码后,我们将使用 cv2.bitwise_and 将其从图像的cv2.bitwise_and中去除背景并分离数字部分。 算术运算,对于在 hsv 彩色图像中定义 roi 非常有用。
    1. OCR with tesseract:我们将 page-segmentation-mode 设置为 6( 查看全部)以获得准确的 output。

  • 用于获取二进制掩码的颜色分割

    • 在此处输入图像描述

    •  lwr = np.array([43, 0, 71]) upr = np.array([103, 255, 130]) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) msk = cv2.inRange(hsv, lwr, upr)
  • 使用二进制掩码提取数字

    • 在此处输入图像描述

    •  krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3)) dlt = cv2.dilate(msk, krn, iterations=5) res = 255 - cv2.bitwise_and(dlt, msk)
  • 光学字符识别

    • 15753.
    •  txt = pytesseract.image_to_string(res, config="--psm 6 digits") print(txt)
    • 如果要删除. 或任何其他非字母字符,你可以做

    • txt = pytesseract.image_to_string(res, config="--psm 6 digits") print(''.join(t for t in txt if t.isalnum()))
    • 结果将是15753

代码:


import cv2
import numpy as np
import pytesseract

# Load the image
img = cv2.imread("input.png")

# Color-segmentation to get binary mask
lwr = np.array([43, 0, 71])
upr = np.array([103, 255, 130])
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
msk = cv2.inRange(hsv, lwr, upr)
cv2.imwrite("/Users/ahx/Desktop/msk.png", msk)

# Extract digits
krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dlt = cv2.dilate(msk, krn, iterations=5)
res = 255 - cv2.bitwise_and(dlt, msk)
cv2.imwrite("/Users/ahx/Desktop/res.png", res)

# Displaying digits and OCR
txt = pytesseract.image_to_string(res, config="--psm 6 digits")
print(''.join(t for t in txt if t.isalnum()))
cv2.imshow("res", res)
cv2.waitKey(0)

要找到掩码的上下边界,您可能会发现有用: HSV-Threshold-script *


更新:


如果将相同的技术应用于数字仪表编号,结果将是

  • 在此处输入图像描述

我尝试从网络摄像头拍照,但结果不一样。

print("PRESS 'c' FOR TAKE THE PICTURE")
camara=cv2.VideoCapture(0)
while True:
    (grabacion, img) = camara.read()
    imagen = cv2.resize(img, (200, 120))
    cv2.rectangle(imagen, (xf, rois), (xf+197, rois+50), (0, 255, 0), 2)cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 2)
    cv2.imshow("CAMARA",imagen)
    tecla=cv2.waitKey(1)
    if tecla==ord('c'):
        image=img
        break
    if tecla==ord('x'):
        break

# Color-segmentation to get binary mask
lwr = np.array([43, 0, 71])
upr = np.array([103, 255, 130])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
msk = cv2.inRange(hsv, lwr, upr)
cv2.imwrite("/home/barcelo/projects-kivy/ocr1/msk.png", msk)

# Extract digits
krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dlt = cv2.dilate(msk, krn, iterations=5)
res = 255 - cv2.bitwise_and(dlt, msk)
cv2.imwrite("/home/barcelo/projects-kivy/ocr1/res.png", res)

# Displaying digits and OCR
txt = pytesseract.image_to_string(res, config="--psm 6 digits")
print(''.join(t for t in txt if t.isalnum()))
cv2.imshow("res", res)
cv2.waitKey(0)

测试图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM