YOLO v4 批量生成并保存图片与坐标信息
YOLO v4还有在其它框架下实现的可以下载,可以参照github上给出的链接,根据自己需求进行下载:
https://github.com/AlexeyAB/darknet#yolo-v4-in-other-frameworks
我使用的是作者的C语言版本的YOLO,因为需要对一系列图像进行目标检测并且保存生成图片和label信息,所以对code进行了一些改动。
首先是darknet/src/detector.c:
test_detector和draw_object里的
draw_detections_v3(sized, dets, nboxes, thresh, names, alphabet, l.classes, 1);
save_image(im, "predictions");
改为
const char save_path[] = "F:/YOLO/results";//自己的保存图片路径
const char ch = '/'; //注意这里图片路径使用的是/,如果是\的话要写成'\\'
char *ret;
char *save_name;
ret = strrchr(filename, ch);
save_name = strtok(ret, ".");
draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output, save_name); //这里有增加save_name这个参数,需要到src/image.h中也改一下
save_name = strtok(ret, ".");
strcat(save_path, ret);
save_image(im, save_path);
image.c中:
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output)
改为
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output, char *save_name)
draw_detections_v3中,
static int frame_id = 0;
前加上
const char box_path[] = "F:/YOLO/results/boxes";//自己的保存label信息的文件夹
strcat(save_name,".txt");
strcat(box_path,save_name);
FILE *fw = fopen(box_path,"w+");
char buff[1024];
将label信息输出并以(class_name percentage xmin ymin xmax ymax)的格式保存:
printf("%s: %.0f%%", names[best_class], selected_detections[i].det.prob[best_class] * 100);
if (ext_output) {
printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2)*im.h),
round(selected_detections[i].det.bbox.w*im.w), round(selected_detections[i].det.bbox.h*im.h));
}
else
printf("\n");
int j;
for (j = 0; j < classes; ++j) {
if (selected_detections[i].det.prob[j] > thresh && j != best_class) {
printf("%s: %.0f%%", names[j], selected_detections[i].det.prob[j] * 100);
if (ext_output) {
printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2)*im.h),
round(selected_detections[i].det.bbox.w*im.w), round(selected_detections[i].det.bbox.h*im.h));
}
else
printf("\n");
}
}
改为
printf("%s: %.0f%%", names[best_class], selected_detections[i].det.prob[best_class] * 100);
sprintf(buff, "%s %.06f ", names[best_class], selected_detections[i].det.prob[best_class]);
fprintf(fw,buff);
if (ext_output) {
printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2)*im.h),
round(selected_detections[i].det.bbox.w*im.w), round(selected_detections[i].det.bbox.h*im.h));
sprintf(buff, "\t %4.0f %4.0f %4.0f %4.0f\n",
round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2)*im.h),
round((selected_detections[i].det.bbox.x + selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y + selected_detections[i].det.bbox.h / 2)*im.h));
fprintf(fw, buff);
}
else
printf("\n");
int j;
for (j = 0; j < classes; ++j) {
if (selected_detections[i].det.prob[j] > thresh && j != best_class) {
printf("%s: %.0f%%", names[j], selected_detections[i].det.prob[j] * 100);
sprintf(buff, "%s: %.06f ", names[j], selected_detections[i].det.prob[j]);
fprintf(fw, buff);
if (ext_output) {
printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2)*im.h),
round(selected_detections[i].det.bbox.w*im.w), round(selected_detections[i].det.bbox.h*im.h));
sprintf(buff, "\t %4.0f %4.0f %4.0f %4.0f\n",
round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2)*im.h),
round((selected_detections[i].det.bbox.x + selected_detections[i].det.bbox.w / 2)*im.w),
round((selected_detections[i].det.bbox.y + selected_detections[i].det.bbox.h / 2)*im.h));
fprintf(fw, buff);
}
else
printf("\n");
}
}
如果是使用png图片的话,ctrl+F把detector.c和image.c里面的jpg都换成png就行(包括大小写)
作者在github上也提供了保存label信息的方法,暂时还没有尝试过。
https://github.com/tzutalin/labelImg
为了不改动太多源码,使用了bash脚本来进行一系列图片的生成和保存,windows下可以下载MSYS2来运行,或者安装git然后使用git bash。我这里使用的是MSYS2
#!/bin/bash
path_dir="../../f/RTTS/ImageSets/Main/test.txt" ##存有图片名字的文件,不带路径和后缀
img_dir="F:/RTTS/JPEGImages/"
bak=$IFS
IFS=$'\n'
mark=".png" ##源图为png格式,需要jpg的将其改成".jpg",或者其它的图片格式
for i in `cat $path_dir`
do
./darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights $img_dir$i$mark -dont_show -ext_output
done
IFS=$bak