在項目中需要對測試集進行測試,而測試集又是由很多圖片組成的,在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");
}
}
以上爲代碼修改部分,有