在网上找了一些批量的代码,发现跑不起来,之后看了一下,下载的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
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.csdn.net/mieleizhi0522/article/details/79989754