![](/img/trans.png)
[英]How can I get only the numbers from the image using tesseract in python?
[英]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.