【问题标题】:Type Error after providing image after reading it from OpenCV python从 OpenCV python 读取图像后提供图像后键入错误
【发布时间】:2019-11-29 11:54:45
【问题描述】:

我第一次尝试使用 YOLO 对象检测,在此期间我在证明要编程的图像后出现类型错误。我正在使用 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

【问题讨论】:

  • 发布代码而不是截图
  • 我编辑了问题并在给出代码后重新发布。
  • 当您将图像作为detect 的参数传递时,图像的类型是什么。我的意思是print(type(image)) 的输出是什么
  • 使用 cv2 读取图像后,类型为 ndarray,即
  • 预处理步骤之后?

标签: python opencv object-detection yolo


【解决方案1】:

看起来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)

【讨论】:

    猜你喜欢
    • 2013-03-29
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2021-05-29
    • 2020-08-13
    • 2017-11-23
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多