yolov3-python接口调用

发布时间:2019-08-31 09:55:36编辑:auto阅读(2183)

    在YOLO官网提供的Darknet源码中,有一个使用python接口的示例程序 darknet.py
    示例如下:https://github.com/pjreddie/darknet/blob/master/python/darknet.py
    其处理一张图片的代码段如下:

    if __name__ == "__main__":
        net = load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
        meta = load_meta("cfg/coco.data")
        r = detect(net, meta, "data/dog.jpg")
        print r
    

    此时,你可能会觉得这儿很麻烦,因为官网提供的接口用python处理一张图片的时候,只能传入图片路径?要是处理视频帧的话岂不是需要先将图片保存,获得路径后再调用接口?不需要!下面我们就来解决这个问题。

    首先,看detect函数的定义:

    def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
        im = load_image(image, 0, 0)
        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])
    free_image(im)
    free_detections(dets, num)
    return res
    

    传入detect函数的第三个参数image是字符串类型,即路径,随后调用load_image函数加载图片得到im对象,(注意这个im是Darknet中自定义的结构体image类型,定义在include/darknet.h中,该文件的详细注释在群内有分享。)再进行后续处理,那么我们改写接口的时候就可以直接将第三个参数改为image类型。做如下的修改:

    1、在darkenet.py中自定义一个函数,内容如下:

    def nparray_to_image(img):
        data = img.ctypes.data_as(POINTER(c_ubyte))
        image = ndarray_image(data, img.ctypes.shape, img.ctypes.strides)
        return image
    

    2、在darknet.py中增加如下行代码,增加位置找到类似代码的位置就好

    ndarray_image = lib.ndarray_to_image
    ndarray_image.argtypes = [POINTER(c_ubyte), POINTER(c_long), POINTER(c_long)]
    ndarray_image.restype = IMAGE
    

    3、在image.c中增加如下代码段,增加位置大概558行,灵活修改就好

    #ifdef NUMPY
    image ndarray_to_image(unsigned char* src, long* shape, long* strides)
    {
        int h = shape[0];
        int w = shape[1];
        int c = shape[2];
        int step_h = strides[0];
        int step_w = strides[1];
        int step_c = strides[2];
        image im = make_image(w, h, c);
        int i, j, k;
        int index1, index2 = 0;
    
    for(i = 0; i < h; ++i){
            for(k= 0; k < c; ++k){
                for(j = 0; j < w; ++j){
    
                    index1 = k*w*h + i*w + j;
                    index2 = step_h*i + step_w*j + step_c*k;
                    //fprintf(stderr, "w=%d h=%d c=%d step_w=%d step_h=%d step_c=%d \n", w, h, c, step_w, step_h, step_c);
                    //fprintf(stderr, "im.data[%d]=%u data[%d]=%f \n", index1, src[index2], index2, src[index2]/255.);
                    im.data[index1] = src[index2]/255.;
                }
            }
        }
    
    rgbgr_image(im);
    
    return im;
    

    4、在image.h的19行后面加如下代码

    #ifdef NUMPY
    image ndarray_to_image(unsigned char* src, long* shape, long* strides);
    #endif
    

    5、在makefile的47行后面中加如下代码

    ifeq ($(NUMPY), 1) 
    COMMON+= -DNUMPY -I/usr/include/python2.7/ -I/usr/lib/python2.7/dist-packages/numpy/core/include/numpy/
    CFLAGS+= -DNUMPY
    endif
    
    • 1
    • 2
    • 3
    • 4

    前几行加 一条 NUMPY=1,增加后为:

    GPU=1
    CUDNN=1
    OPENCV=1
    OPENMP=0
    NUMPY=1
    DEBUG=0
    

    6、重新编译make clean + make
    7、修改darknet.py的后续处理,示例代码如下:

    if __name__ == "__main__":
        net = load_net(b"cfg/yolov3.cfg", b"yolov3.weights", 0)
        meta = load_meta(b"cfg/coco.data")
        vid = cv2.VideoCapture(' 1pondo.avi  ')
        while True:
            return_value,arr=vid.read()
            im=nparray_to_image(arr)
            r = detect(net, meta, im)
            此时r中返回了检测到的边框信息,老铁你要怎么处理随便你!
           ......
    

    8、更改detect函数的前面几行,更改后为:

    def detect(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45):
        num = c_int(0)
        pnum = pointer(num)
    

    说明:更改了形参image为im,直接使用im,不需要再读取,去掉了加载图片行的代码。
    9、加群一起讨论交流

    参考链接 https://github.com/pjreddie/darknet/issues/289#issuecomment-342448358
    欢迎大家一起讨论交流,共同做好目标检测!码字不易,且行且珍惜!

            </div>
    					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-448c2d19d9.css" rel="stylesheet">
                </div>
    

关键字