[英]OpenCv pytesseract for OCR
如何使用opencv和pytesseract從圖像中提取文本?
import cv2
從PIL導入pytesseract導入圖像從matplotlib導入numpy為np導入為plt pyplot
img = Image.open('test.jpg').convert('L')
img.show()
img.save('test','png')
img = cv2.imread('test.png',0)
edges = cv2.Canny(img,100,200)
#contour = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print pytesseract.image_to_string(Image.open(edges))
print pytesseract.image_to_string(edges)
但這給了錯誤-
追溯(最近一次通話):打印pytesseract.image_to_string(edges)中的文件“ open.py”,第14行,文件“ /home/sroy8091/.local/lib/python2.7/site-packages/pytesseract/pytesseract”。 py“,第143行,如果len(image.split())== 4,則在image_to_string中:AttributeError:'NoneType'對象沒有屬性'split'
如果您想使用opencv進行一些預處理(就像您進行了一些邊緣檢測一樣),然后又想提取文本,則可以使用此命令,
# All the imports and other stuffs goes here
img = cv2.imread('test.png',0)
edges = cv2.Canny(img,100,200)
img_new = Image.fromarray(edges)
text = pytesseract.image_to_string(img_new, lang='eng')
print (text)
您不能將tecract方法直接使用Opencv對象。
嘗試:
from PIL import Image
from pytesseract import *
image_file = 'test.png'
print(pytesseract.image_to_string(Image.open(image_file)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.