Darknet: Detect numpy image with Python

Created on 15 May 2018  路  3Comments  路  Source: AlexeyAB/darknet

The script darknet.py loads the image directly with a filepath. I'd like to use the detect function with a numpy image loaded from OpenCV. How can I do this? Thanks.

want enhancement

Most helpful comment

All 3 comments

You can convert a numpy array to the format Darknet expects using this function (borrowed from this script you can use as reference)

def array_to_image(arr):
    import numpy as np
    # need to return old values to avoid python freeing memory
    arr = arr.transpose(2,0,1)
    c = arr.shape[0]
    h = arr.shape[1]
    w = arr.shape[2]
    arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0
    data = arr.ctypes.data_as(POINTER(c_float))
    im = IMAGE(w,h,c,data)
    return im, arr

ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected IMAGE instance instead of tuple

the snippet you shared is coming from the original repo, hence it seems like the classes used aren't compatible with Alexey's repo.

Was this page helpful?
0 / 5 - 0 ratings