YOLOv3(AlexeyAB版)批量图片检测结果保存

yolov3批量测试代码,网上有很多版本,官网版本的完全按照教程没有问题,这里提供AlexeyAB/darknet版的图片批量测试代码。原文网址 https://github.com/AlexeyAB/darknet

假设训练按照原版处理完成:

1.请更新最新的代码;

2.请在/root/darknet/src路径下找到detector.c文件,并打开,或请在根目录下直接执行:

#vim darknet/src/detector.c

3.定位到 void test_detector(char *datacfg ...)这一行,将这个函数模块用以下代码完全替代:

注意3个地方:(1)请在根路径下新建一个测试文档,本文新建了test.txt文件,里面是你需要测试图片的路径与文件名,建议直接复制在训练之前生成的那个train.txt文件;

                       (2)请在根目录下新建文件夹,用来存放测试图片的结果,本文在根目录下新建文件夹,命名为result_img。读者可自己定义。

                       (3)请在以下代码 sprintf(b, "result_img/%d", i);    处修改成对应自己的文件夹路径。

void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh,
    float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile, int letter_box)
{
    list *options = read_data_cfg(datacfg);
    char *name_list = option_find_str(options, "names", "data/names.list");
    int names_size = 0;
    char **names = get_labels_custom(name_list, &names_size); //get_labels(name_list);

    image **alphabet = load_alphabet();
    network net = parse_network_cfg_custom(cfgfile, 1, 1); // set batch=1
    if (weightfile) {
        load_weights(&net, weightfile);
    }
    fuse_conv_batchnorm(net);
    calculate_binary_weights(net);
    if (net.layers[net.n - 1].classes != names_size) {
        printf(" Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n",
            name_list, names_size, net.layers[net.n - 1].classes, cfgfile);
        if (net.layers[net.n - 1].classes > names_size) getchar();
    }
    srand(2222222);
    double time;
    char buff[256];
    char *input = buff;
    char *json_buf = NULL;
    int json_image_id = 0;
    FILE* json_file = NULL;
    if (outfile) {
        json_file = fopen(outfile, "wb");
        char *tmp = "[\n";
        fwrite(tmp, sizeof(char), strlen(tmp), json_file);
    }
    int j,i;
    float nms = .45;    // 0.4F
    if (filename) {
        strncpy(input, filename, 256);
        list *plist = get_paths(input);
	char **paths = (char **)list_to_array(plist);
	printf("Start Testing!\n");
	int m = plist->size;

	for(i=0;i thresh && dets[i].prob[j] > prob) {
                            prob = dets[i].prob[j];
                            class_id = j;
                        }
                    }
                    if (class_id >= 0) {
                        sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h);
                        fwrite(buff, sizeof(char), strlen(buff), fw);
                    }
                  }
                fclose(fw);
            }

            free_detections(dets, nboxes);
            free_image(im);
            free_image(sized);
	}
    }
    printf("All Done!\n");
    pause();
    exit(0);
    free_ptrs(names, net.layers[net.n - 1].classes);
    free_list_contents_kvp(options);
    free_list(options);

    const int nsize = 8;
    for (j = 0; j < nsize; ++j) {
	for (i = 32; i < 127; ++i) {
            free_image(alphabet[j][i]);
	}
	free(alphabet[j]);
    }

    free(alphabet);
    free_network(net);
    printf("All Done!\n");
    pause();
}

4.保存好后请重新编译,执行
#make clean

#make

5.执行以下指令,在result_img文件夹中便可看到结果:

# ./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights test.txt

6.结果如下:

YOLOv3(AlexeyAB版)批量图片检测结果保存_第1张图片

你可能感兴趣的:(YOLOv3(AlexeyAB版)批量图片检测结果保存)