如何使用opencv和pytesseract从图像中提取文本?
import cv2
Run Code Online (Sandbox Code Playgroud)
从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)
Run Code Online (Sandbox Code Playgroud)
但这给了错误-
追溯(最近一次通话):文件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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4973 次 |
| 最近记录: |