Thursday, February 13, 2020

[Darknet][Python] Support using Numpy array as image input instead of image path in Darknet Python API

When I dealt with Darknet's detect function in Python, I found that there is only one way for accepting an image file path as the argument, which is convenient for inferencing images from files.  But, it is not very well for images from video files or cameras.
Due to this reason, I dig out some solutions already on the Internet, and modify as follows:
https://github.com/pjreddie/darknet/issues/609
Please append the following code in your darkent.py in darknet source code
darknet/python/darknet.py
def array_to_image(arr):
    arr = arr.transpose(2,0,1)
    c = arr.shape[0]
    h = arr.shape[1]
    w = arr.shape[2]
    arr = (arr/255.0).flatten()
    data = c_array(c_float, arr)
    im = IMAGE(w,h,c,data)
    return im

def detect_frame(net, meta, frame, thresh=.5, hier_thresh=.5, nms=.45):
    #im = load_image(image, 0, 0)
    im = array_to_image(frame)
    rgbgr_image(im)
    num = c_int(0)
    pnum = pointer(num)
    predict_image(net, im)
    dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
    num = pnum[0]
    if (nms): do_nms_obj(dets, num, meta.classes, nms);

    res = []
    for j in range(num):
        for i in range(meta.classes):
            if dets[j].prob[i] > 0:
                b = dets[j].bbox
                res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
    res = sorted(res, key=lambda x: -x[1])
    wh = (im.w,im.h)
    #free_image(im)
    #free_detections(dets, num)
    return res,wh
So, now you will have a new detect_frame() function for inferencing.
Beware of the third argument: "frame" is the Numpy array.

For the more information:
https://blog.csdn.net/lwplwf/article/details/84566954

1 comment:

Max said...

Wow, What an exceptional pronounce. i found this too much informatics. it’s miles what i used to be searching for for. i’d as soon as to area you that absorb keep sharing such kind of data.If realistic, thank you. dark0de