在项目中需要对测试集进行测试,而测试集又是由很多图片组成的,在yolo框架中有对单张 图片进行检测的机制,可键入命令:
./darknet detector test data/*.data cfg/*.cfg yolo.weights pic_path -dont_show -i 2
函数调用流程为:src/darknet.c (main)-> src/detector.c(run_detector->test_detector)
当测试多张图片,可键入以下命令:./darknet detector test data/*.data cfg/*.cfg yolo.weights -dont_show -i 2
键入命令后,会显示提示输入图片路径的文本文件
这里我将批量的图片路径放在imgpath.txt文本文件中,文件的内容如下图所示:
接下来主要讲代码修改部分,代码主要修改了darknet/src文件路径下的detector.c文件
这里我在src/detector.c文件中的test_detector函数处做了如下改变,在while函数里面加了一个else语句
while语句的上半部分,就是对单张图片进行分析,这个时候在运行darknet命令的时候,是需要将图片路径放在命令行里面的,这部分语句也是darknet源码本身就有的
while语句的下半部分,也是我新增的代码部分,该部分代码是读取一个文本文件中的图片路径,然后在一个循环中处理单张图片的测试,这里单张图片的测试与之前的方法类似,不再赘述。
while (1)
{
if (filename)
{
strncpy(input, filename, 256);
if (strlen(input) > 0)
if (input[strlen(input) - 1] == 0x0d)
{
input[strlen(input) - 1] = 0;
image im = load_image(input, 0, 0, net.c);
image sized;
if(letter_box) sized = letterbox_image(im, net.w, net.h);
else sized = resize_image(im, net.w, net.h);
layer l = net.layers[net.n - 1];
//box *boxes = calloc(l.w*l.h*l.n, sizeof(box));
//float **probs = calloc(l.w*l.h*l.n, sizeof(float*));
//for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(l.classes, sizeof(float));
float *X = sized.data;
//time= what_time_is_it_now();
double time = get_time_point();
network_predict(net, X);
//network_predict_image(&net, im); letterbox = 1;
printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000);
//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, letter_box);
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];
char *pic_file = basecfg(filename);
//sprintf(b,"/home/data2/cellphone_workspace/darknet- master/yolo_cellphone_result/%s",pic_file);//"/home/FENGsl/darknet/data"修改成自己的路径
sprintf(b,"%s",pic_file);//"/home/FENGsl/darknet/data"修改成自己的路径
//printf("b is \n",b);
save_image(im, b);
//printf("save %s successfully!\n",GetFilename());
printf("save %s successfully!\n",filename);
}
}
else
{
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if (!input) break;
strtok(input, "\n");
//printf("the input is %s\n",input);
list *plist = get_paths(input);
char **pic_paths = (char **)list_to_array(plist);
//printf("%s\n",pic_paths[0]);
printf("Start Testing!\n");
int m = plist->size;
//printf("the len of m is %d\n",m);
if(access("/home/data2/cellphone_workspace/darknet-master/yolo_cellphone_result",0)==-1)//"/home/FENGsl/darknet/data"修改成自己的路径
{
if(mkdir("/home/data2/cellphone_workspace/darknet-master/yolo_cellphone_result",0777))//"/home/FENGsl/darknet/data"修改成自己的路径
{
printf("creat file bag failed!!!\n");
}
}
for(int i = 0; i < m; ++i)
{
char *path = pic_paths[i];
input = pic_paths[i];
filename = input;
image im = load_image(input, 0, 0, net.c);
image sized;
if(letter_box) sized = letterbox_image(im, net.w, net.h);
else sized = resize_image(im, net.w, net.h);
layer l = net.layers[net.n - 1];
//box *boxes = calloc(l.w*l.h*l.n, sizeof(box));
//float **probs = calloc(l.w*l.h*l.n, sizeof(float*));
//for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(l.classes, sizeof(float));
float *X = sized.data;
//time= what_time_is_it_now();
double time = get_time_point();
network_predict(net, X);
//network_predict_image(&net, im); letterbox = 1;
printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000);
//printf("%s: Predicted in %f seconds.\n", input, (what_time_is_it_now()-time));
//int nboxes = 0;
//printf("begin to print filename to result_info.txt\n");
//result_info.txt 该记事本文件可以程序运行的时候自动生成
FILE *fp = fopen("result_info.txt","a+");
fprintf(fp,"%s\n",filename);
fclose(fp);
//printf("finish to print filename to result_info.txt\n");
int nboxes = 0;
detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box);
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];
char *pic_file = basecfg(filename);
sprintf(b,"/home/data2/cellphone_workspace/darknet-master/yolo_cellphone_result/%s",pic_file);//"/home/FENGsl/darknet/data"修改成自己的路径
//FILE *fp = fopen("result_info.txt","a+");
//fprintf(fp,"%s\n",filename);
//fclose(fp);
//printf("b is %s\n",b);
save_image(im, b);
//printf("save %s successfully!\n",GetFilename());
printf("save %s successfully!\n",filename);
//image im = load_image(input, 0, 0, net.c);
//image sized;
//if(letter_box) sized = letterbox_image(im, net.w, net.h);
//else sized = resize_image(im, net.w, net.h);
//layer l = net.layers[net.n - 1];
//box *boxes = calloc(l.w*l.h*l.n, sizeof(box));
//float **probs = calloc(l.w*l.h*l.n, sizeof(float*));
//for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(l.classes,
//sizeof(float));
//float *X = sized.data;
//time= what_time_is_it_now();
//double time = get_time_point();
// network_predict(net, X);
//network_predict_image(&net, im); letterbox = 1;
//printf("%s: Predicted in %lf milli-seconds.\n", input,((double)get_time_point() - time) / 1000);
//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, letter_box);
//if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
///draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes,ext_output);
//save_image(im, "predictions");
}
}
以上为代码修改部分,有