从OpenCV python读取图像后提供图像后出现类型错误

问题描述 投票:1回答:1

我第一次尝试使用YOLO对象检测,在此期间,在将图像验证为程序后,出现Type Error。我正在使用cv2.imread()函数从opencv中读取图像,并在经过一些图像处理后将其提供给yolo,但出现“类型错误”。

我提供出现错误的代码图像:

  def ocr_function(image): 
    start = time()
    try:
        ocr_threshold = 0.4

        ocr_weights = b'data/ocr/ocr-net.weights'
        ocr_netcfg  = b'data/ocr/ocr-net.cfg'
        ocr_dataset = b'data/ocr/ocr-net.data'

        ocr_net  = dn.load_net(ocr_netcfg, ocr_weights, 0)
        ocr_meta = dn.load_meta(ocr_dataset)

        print("performing OCR at: ", os.getcwd())

        print("\tScanning %s" %image)

        image = cv2.imread(image_path)
        image = preprocess(image)
        show(image, "provided image")        

        R,(width,height) = detect(ocr_net, ocr_meta, image, thresh=ocr_threshold, nms=None)

        if len(R):
            L = dknet_label_conversion(R,width,height)
            L = nms(L,.45)

            L.sort(key=lambda x: x.tl()[0])
            lp_str = ''.join([chr(l.cl()) for l in L])

            print ('\t\tLP: %s' % lp_str)

我收到此错误:

    Traceback (most recent call last):
  File "<ipython-input-1-c25d751af85c>", line 86, in ocr_function
    R,(width,height) = detect(ocr_net, ocr_meta, imageROT, thresh=ocr_threshold, nms=None)
  File "/home/infinity/Desktop/NEW ALPR/LPD/darknet/python/darknet.py", line 126, in detect
    im = load_image(image, 0, 0)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
python opencv object-detection yolo
1个回答
0
投票

看起来像load_image函数需要pointer to data array

来自darknet库的detect函数的代码:

load_image = lib.load_image_color
load_image.argtypes = [c_char_p, c_int, c_int]
load_image.restype = IMAGE

所以我建议您使用__arrayinterface__传递数据数组。https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.interface.html

image_data = image.__array_interface__['data'][0]
R,(width,height) = detect(ocr_net, ocr_meta, image_data, thresh=ocr_threshold, nms=None)
© www.soinside.com 2019 - 2024. All rights reserved.