DarkNet & CV2


def detect_np(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
    # image should be an cv2.imread or cv2.imdecode result
    start = time.time()
    im, image = array_to_image(image)
    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):
        a = dets[j].prob[0:meta.classes]
        if any(a):
            ai = np.array(a).nonzero()[0]
            for i in ai:
                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])
    end = time.time()
    print("finding face finished in %f ms." % (end - start))
    if isinstance(image, bytes): free_image(im)
    free_detections(dets, num)
    return res