因为使用Darknet进行目标检测任务时,框架只会输出图片形式的检测结果(即将bounding-box显式的画在原图上),但并不会输出单独的坐标信息,这给我们的进一步分析带来很多不便。所以我们就要对框架源码进行一定的修改,使其能保存图片中目标的bounding-box坐标到txt文件中。
Step 1
首先我们要找到源码中画框的函数,因为画框是需要坐标信息的,我们可以在这个函数中找到bounding-box的坐标信息。
在源码中,进行画框任务的函数为 ./darknet/src/image.c 里面的 draw_detections() 函数,考虑到框架中多个文件会调用此函数,所以我们不直接对函数进行修改,而是在 image.c 文件中新增一个函数,命名为 draw_detections_in_txt(),这个函数在 draw_detections() 的基础上新增了一个参数 filename ,这个参数用来将 txt 保存成和被检测图片一致的文件名。整个函数如下:
//////////////////////////////JiaXuejian Modified//////////////////////////////////////////////////////////
/////////////////////////////Added an argument:filename////////////////////////////////////////////////////
void draw_detections_in_txt(image im, char *filename, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
int i,j;
char *output = filename;
//output = strcat(output, ".txt");
//output = strcat("./", output);
//printf("PATH:%s", output);
int namecount = 0;
for (namecoun