1. 程式人生 > >yolov3 影象批量處理程式

yolov3 影象批量處理程式

yolov3批量測試程式碼,網上查找了一篇,稍有些問題,這裡貼出修改後程式碼。原理就是在原有的程式上加了批量的內容。
參考部落格:https://blog.csdn.net/xczexcel/article/details/80585776

void batch_process(char *datacfg, char *cfgfile, char *weightfile, char *read_file, float thresh, float hier_thresh, char *save_file)

{

    list *options = read_data_cfg(datacfg);

    char *name_list
= option_find_str(options, "names", "data/names.list"); char **names = get_labels(name_list); image **alphabet = load_alphabet(); network net = parse_network_cfg_custom(cfgfile, 1); if (weightfile) { load_weights(&net, weightfile); } set_batch_network(&net, 1
); srand(2222222); double time; float nms = .45; int max_len = 256; char buf[256]; FILE *writer; FILE *fp; int len; if ((writer = fopen(save_file, "w")) == NULL) { printf("%s", "conld not open output_file\n"); exit(1); } if ((fp = fopen(read_file, "r"
)) == NULL) { printf("%s", "could not open image_list file\n"); exit(1); } while (fgets(buf, max_len, fp) != NULL) { len = strlen(buf); buf[len - 1] = '\0'; //fprintf(writer, "%s", buf); char *image_name = buf; image im = load_image_color(image_name, 0, 0); int letterbox = 0; image sized = resize_image(im, net.w, net.h); layer l = net.layers[net.n - 1]; float *X = sized.data; network_predict(net, X); printf("Process:%s\n", image_name); int nboxes = 0; //detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes); detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox); if (nms) do_nms_sort_v3(dets, nboxes, l.classes, nms); int i, j; fprintf(writer,"\n"); //printf("%d\n",nboxes); //printf("classes:%d\n",l.classes); if (nboxes != 0) { for (i = 0; i<nboxes; i++) { float xmin = (dets[i].bbox.x - dets[i].bbox.w / 2.)*im.w; float xmax = (dets[i].bbox.x + dets[i].bbox.w / 2.)*im.w; float ymin = (dets[i].bbox.y - dets[i].bbox.h / 2.)*im.h; float ymax = (dets[i].bbox.y + dets[i].bbox.h / 2.)*im.h; for (j = 0; j<l.classes; j++) { if (dets[i].prob[j]) { fprintf(writer, "%f, %f, %f, %f, %d,", xmin, ymin, xmax, ymax, j); } } } } free_detections(dets, nboxes); free_image(im); free_image(sized); } }

剩餘部分相同,修改void run_detector(int argc, char **argv)函式,就是講batch指令加入

if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);  
    else if(0==strcmp(argv[2],"batch")){  
        if(argv<=7){  
            printf("%s\n","image_list and output_file is required!");  
            exit(0);  
        }  
        char *image_list= argv[6];  
        char *save_file=  argv[7];  
        batch_process(datacfg, cfg, weights, image_list, thresh, hier_thresh, save_file);  
    }   
    else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear);  
    else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);  
    else if(0==strcmp(argv[2], "valid2")) validate_detector_flip(datacfg, cfg, weights, outfile);  
    else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);  
    else if(0==strcmp(argv[2], "demo")) {  
        list *options = read_data_cfg(datacfg);  
        int classes = option_find_int(options, "classes", 20);  
        char *name_list = option_find_str(options, "names", "data/names.list");  
        char **names = get_labels(name_list);  
        demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix, avg, hier_thresh, width, height, fps, fullscreen);  
    }

最後使用
detector.exe detector batch cfg/coco.data cfg/yolov3.cfg yolov3.weights input_image_list.txt output_results.txt

注意:如果要修改格式,請修改fprintf內的格式,printf修改的是控制檯。