繁体   English   中英

无法使用 python、Tesseract 和 opencv 从图像中获取数字

[英]Can't get numbers from image with python, Tesseract and opencv

我必须从使用 python tesseract 和 opencv 的水表图像中获取数字。 我试图改变 --psm 但它不起作用。

这是未经修改的图像:

在此处输入图像描述

这是输出图像:

在此处输入图像描述

我需要你们的帮助,我正在启动 python 并且我已经被阻止了:'(

我的代码:

from PIL import Image
import pytesseract
import cv2
import numpy as np
import urllib
import requests
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\Hymed\AppData\Local\Tesseract-OCR\tesseract.exe'

col = Image.open("pts.jpg")
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save("cp19.png")


image = cv2.imread('cp19.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
img1 = np.array(thresh)
data = pytesseract.image_to_string(img1, config='--psm 11 digits')
print(data)

cv2.imshow('thresh', thresh)
cv2.waitKey()

你几乎完成了任务。

我在GaussianBlur之后使用除法运算。

div = cv2.divide(gray, thresh, scale=192)

结果:

在此处输入图像描述

当我从图像中读取时:

data = pytesseract.image_to_string(div, config='--psm 11 digits')
print(data)

结果:

00000161

代码:(刚刚添加div = cv2.divide(gray, thresh, scale=192) rest 是您的代码)

from PIL import Image
import pytesseract
import cv2
import numpy as np

col = Image.open("TOaEW.jpg")
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x < 128 else 255, '1')
bw.save("cp19.png")

image = cv2.imread('cp19.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3, 3), 0)

div = cv2.divide(gray, thresh, scale=192)  # added

data = pytesseract.image_to_string(div, config='--psm 11 digits')
print(data)

我尝试使用 Tesseract 从图像中读取数字。 除了第一行显示的数字外,它还在第二行返回了一个无法识别的符号。 我不明白我做错了什么。 这是代码和结果代码和 output

这是我从中提取数字的图像:用于数字提取的图像

暂无
暂无

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

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