简体   繁体   English

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

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

i have to get numbers from a water-meter image usign python tesseract and opencv.我必须从使用 python tesseract 和 opencv 的水表图像中获取数字。 I have tried to change the --psm but it's doesn't work.我试图改变 --psm 但它不起作用。

Here the image without modification:这是未经修改的图像:

enter image description here在此处输入图像描述

Here the outpout image:这是输出图像:

enter image description here在此处输入图像描述

I need your help guys, i'm starting python and i'm already blocked:'(我需要你们的帮助,我正在启动 python 并且我已经被阻止了:'(

My code:我的代码:

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()

You have nearly finished the task.你几乎完成了任务。

I use the divide operation, after the GaussianBlur .我在GaussianBlur之后使用除法运算。

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

Result:结果:

在此处输入图像描述

When I read from the image:当我从图像中读取时:

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

Result:结果:

00000161

Code: (Just added div = cv2.divide(gray, thresh, scale=192) rest are your code)代码:(刚刚添加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)

I tried to read the number from an image using Tesseract.我尝试使用 Tesseract 从图像中读取数字。 Except the numbers shown in the first line, it also returned an unidentified symbol in the second line.除了第一行显示的数字外,它还在第二行返回了一个无法识别的符号。 I don't understand what I did wrong.我不明白我做错了什么。 Here is the code and the results code and output这是代码和结果代码和 output

This is the image I extracted the number from: Image used for number extraction这是我从中提取数字的图像:用于数字提取的图像

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

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