首页 文章

用于OCR的OpenCv pytesseract

提问于
浏览
1

如何使用opencv和pytesseract从图像中提取文本?

import cv2

从PIL导入导入pytesseract从matplotlib导入pyplot导入numpy为np作为plt

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)

但这是错误的 -

回溯(最近一次调用最后一次):文件“open.py”,第14行,打印pytesseract.image_to_string(edges)文件“/home/sroy8091/.local/lib/python2.7/site-packages/pytesseract/pytesseract . py“,第143行,在image_to_string中如果len(image.split())== 4:AttributeError:'NoneType'对象没有属性'split'

2 回答

  • 1

    如果你想使用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)
    
  • 1

    您不能使用tesseract方法直接使用Opencv对象 .

    尝试:

    from PIL import Image
    from pytesseract import *
    
    image_file = 'test.png'
    print(pytesseract.image_to_string(Image.open(image_file)))
    

相关问题