在网上找了一些批量的代码,发现跑不起来,之后看了一下,下载的darknet包的结构不同,于是自己结合网上的代码修改了一下自己的test_detector函数。
我使用的是这个网址上下载的darknet:https://github.com/AlexeyAB/darknet
修改的是darknet/src/detector.c文件中的test_detector函数。
1、复制如下代码替换原来的test_detector函数。
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)
{
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); // set batch=1
if(weightfile){
load_weights(&net, weightfile);
}
//set_batch_network(&net, 1);
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;
int j;
float nms=.45; // 0.4F
while(1){
if(filename)
{
strncpy(input, filename, 256);
if(strlen(input) > 0)
if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0;
} 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("/root/yolo3/out",0)==-1)
{
if(mkdir("/root/yolo3/out",0777))
{
printf("creat file bag failed!!!");
}
}
for(int i=0;i<m;++i)
{
char *path=paths[i];
image im = load_image(path,0,0,net.c);//根据测试图片路径载入图片
int letterbox = 0;
image sized = resize_image(im, net.w, net.h);
layer l = net.layers[net.n-1];
float *X = sized.data;
time= what_time_is_it_now();
network_predict(net, X);
//network_predict_image(&net, im); letterbox = 1;
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, letterbox);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);
char b[2048];
sprintf(b,"/root/yolo3/out/%s",GetFilename(path));
save_image(im,b);
printf("save %s successfully!\n",GetFilename(path));
free_image(im);
if(filename)break;
// free memory
free_ptrs(names, net.layers[net.n - 1].classes);
free_list_contents_kvp(options);
free_list(options);
int i;
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);
}
}
2、在static int coco_ids[](如下图所示)后面加GetFilename()函数。
char *GetFilename(char *p)
{
static char name[20]={""};
char *q=strrchr(p,'/')+1;
strncpy(name,q,6);
return name;
}
3、保存后使用如下命令编译detector.c文件。
gcc detector.c
4、编译成功后在darknet-master目录下make
make
5、批量测试
./darknet detector test build/darknet/x64/data/obj.data cfg/yolo-obj.cfg build/darknet/x64/backup/yolo-obj_32000.weights
在Enter path 后面输入自己测试图片路径存放txt文件。
参考网址:https://blog.youkuaiyun.com/mieleizhi0522/article/details/79989754