yolov3利用自己训练的weigt批量测试图片并保存到定义的文件夹下

利用yolov3自带的测试命令智能对data下的指定图片作测试,每一次只能测试一张图片,经网上找找大神资料后可以批量测试指定文件夹下的图片并保存在data/out下,带有标记的图片很直观的可以测试自己检测的结果,以VOC数据集为例

 

参考https://blog.csdn.net/mieleizhi0522/article/details/79989754后发现博主的在添加*GetFilename(char *p)函数的时候出现错误,自己的环境下无法识别“charchar”类型,

将博主的代码复制到自己的detec.c后,针对自己的detect.c实现GetFilename(char *p)的功能

1.声明char fn[30],*p,*q;

2.添加strcpy(fn,(p=strrchr(path,'/')) ? p+1 : path);

3.用fn代替原来的GetFilename(path)后可以实现以上功能(别忘记重新make)

添加代码如下:(三处代码需要修改,换成你自己的计算机的名字,或者选择一个存在图片的路径)

void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{
    int person_num=0;
    list *options = read_data_cfg(datacfg);
    char *name_list = option_find_str(options, "names", "data/names.list");
    char **names = get_labels(name_list);
    char fn[30],*p,*q;
    image **alphabet = load_alphabet();
    network *net = load_network(cfgfile, weightfile, 0);
    set_batch_network(net, 1);
    srand(2222222);
    double time;
    char buff[256];
    char *input = buff;
    float nms=.45;
    int i=0;
    while(1){
        if(filename){
            strncpy(input, filename, 256);
            image im = load_image_color(input,0,0);
            image sized = letterbox_image(im, net->w, net->h);
        //image sized = resize_image(im, net->w, net->h);
        //image sized2 = resize_max(im, net->w);
        //image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
        //resize_network(net, sized.w, sized.h);
            layer l = net->layers[net->n-1];


            float *X = sized.data;
            time=what_time_is_it_now();
            network_predict(net, X);
            printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
            int nboxes = 0;
            detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
            //printf("%d\n", nboxes);
            //if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
            if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
                draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
                free_detections(dets, nboxes);
            if(outfile)//
             {
               save_image(im, outfile);
             }
            else{
                save_image(im, "predictions");
#ifdef OPENCV
                cvNamedWindow("predictions", CV_WINDOW_NORMAL); 
                if(fullscreen){
                cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
                }
                show_image(im, "predictions");
                cvWaitKey(0);
                cvDestroyAllWindows();
#endif
            }
            free_image(im);
            free_image(sized);
            if (filename) break;
         } 
        else {
            printf("Enter Image Path: ");
            fflush(stdout);
            input = fgets(input, 256, stdin);
            if(!input) return;
            strtok(input, "\n");
   
            list *plist = get_paths(input);
            char **paths = (char **)list_to_array(plist);
             printf("Start Testing!\n");
            int m = plist->size;
            if(access("/home/yourname/darknet/data/out",0)==-1)//"/home/yourname/darknet/data"换成自己的路径
            {
              if (mkdir("/home/yourname/darknet/data/out",0777))//"/home/yourname/darknet/data"换成自己的路径
               {
                 printf("creat file bag failed!!!");
               }
            }
            for(i = 0; i < m; ++i){
             char *path = paths[i];
             image im = load_image_color(path,0,0);
             image sized = letterbox_image(im, net->w, net->h);
        //image sized = resize_image(im, net->w, net->h);
        //image sized2 = resize_max(im, net->w);
        //image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
        //resize_network(net, sized.w, sized.h);
        layer l = net->layers[net->n-1];


        float *X = sized.data;
        time=what_time_is_it_now();
        network_predict(net, X);
        printf("Try Very Hard:");
        printf("%s: Predicted in %f seconds.\n", path, what_time_is_it_now()-time);
        int nboxes = 0;
        detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
        //printf("%d\n", nboxes);
        //if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
        if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
        free_detections(dets, nboxes);
	printf("person_num=%d\n",person_num);
        if(outfile){
            save_image(im, outfile);
        }
        else{
             
             char b[2048];
	     strcpy(fn,(p=strrchr(path,'/')) ? p+1 : path);
            sprintf(b,"/home/yourname/darknet/data/out/%s",fn);//"/home/yourname/darknet/data"换成自己的路径
            
            save_image(im, b);
            printf("save %s successfully!\n",fn);
#ifdef OPENCV
            //cvNamedWindow("predictions", CV_WINDOW_NORMAL); 
            //if(fullscreen){
                //cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
            //}
            //show_image(im, "predictions");
           // cvWaitKey(0);
           // cvDestroyAllWindows();
#endif
        }

        free_image(im);
        free_image(sized);
        if (filename) break;
        }
      }
    }
}

运行./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights后可以实现批量图片的测试

 

 

 

你可能感兴趣的:(目标检测,学习笔记)