如何在python中解决“数据类型= 19不支持”错误?

时间:2018-03-21 01:14:07

标签: python ocr

亲爱的。 我将使用python ocr从图像中获取文本。 我希望解决以下错误。 我无法解决这个错误。 代码和错误如下。 感谢。

    import pytesseract
    from PIL import Image, ImageEnhance, ImageFilter
    import cv2
    import numpy as np


    src_path = ""
    def get_string(img_path):

    #read image with opencv
    img = cv2.imread(img_path)
    #convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #apply dilation and erosion to remove some noise
    kernel = np.ones((1,1), np.unicode)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    cv2.imwrite("2.jpg", img)
    #apply threshold to get image with only black and white
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSAIN_C,             
    cv2.THRESH_BINARY,11,2)
    cv2.imwrite("1.jpg", img)
    #recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open("1.jpg"))
    return result
    print(get_string("a.jpg"))

错误正在跟随。

 C:\python\python.exe C:/Users/rharmed/PycharmProjects/nike/crawler2_.py
        Traceback (most recent call last):
    File "C:/Users/rharmed/PycharmProjects/nike/crawler2_.py", line 26, in         
        <module>
    print(get_string("a.jpg"))
    File "C:/Users/rharmed/PycharmProjects/nike/crawler2_.py", line 15, in         
        get_string
    img = cv2.dilate(img, kernel, iterations=1)
    TypeError: kernel data type = 19 is not supported
        [ INFO:0] Initialize OpenCL runtime...

    Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

您的内核的数据类型无效。

更改

kernel = np.ones((1,1), np.unicode)

kernel = np.ones((1,1), np.uint8)