主要参考(几乎是全部复制)了这位前辈的关于YOLOV3批量检测图片的程序
修改后如下
修改后要重新编译,生成新的darknet可执行文件
while (1) {
if (filename) {
strncpy(input, filename, 256);
if (strlen(input) > 0)
//去掉最后的回车符号 0x0d
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*)xcalloc(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) {
if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms);
else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_nms);
}
draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);
save_image(im, "predictions");
if (!dont_show) {
show_image(im, "predictions");
}
}
else {
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if (!input) break;
strtok(input, "\n");
//开始更改
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/longlong/Result_yolov4/result_1",0)==-1)//"/home/longlong/Result_yolov4"修改成自己的路径
{
if(mkdir("/home/longlong/Result_yolov4/result_1",0777))//"/home/longlong/Result_yolov4"修改成自己的路径
{
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];
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));
FILE *fp = fopen("result_info.txt","a+");
fprintf(fp,"%s\n",filename);
fclose(fp);
int nboxes = 0;
detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box);
if (nms) {
if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms);
else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_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/longlong/Result_yolov4/result_1/%s",pic_file);//"/home/longlong/Result_yolov4"修改成自己的路径
save_image(im, b);
if (!dont_show) {
show_image(im, b);
}
}
//结束更改,其余部分根据个人理解自己修改也可以
}
2.测试指令
如果想直接检测单图
./darknet detector test cfg/voc.data cfg/yolo-obj.cfg yolov4.weights data/dog.jpg
如果检测目标文件夹下的图片
(1)把待检测图片地址写入text.txt中
可以参考这个脚本
# -*- coding:utf-8 -*-
import sys # sys.path.append('E:\\Anaconda\\libs')
import os #os:操作系统相关的信息模块
import random #导入随机函数
#存放原始图片地址
data_base_dir = "E:\\dyg\\yolov4\\test_1213Data"
file_list = [] #建立列表,用于保存图片信息
#读取图片文件,并将图片地址、图片名和标签写到txt文件中
write_file_name = 'E:\\dyg\\yolov4\\test_1213Data.txt'
write_file = open(write_file_name, "w") #以只写方式打开write_file_name文件
for file in os.listdir(data_base_dir): #file为current_dir当前目录下图片名
if file.endswith(".bmp"): #如果file以jpg结尾
write_name = file #图片路径 + 图片名 + 标签
file_list.append('/home/longlong/1213Train/test_1213Data/'+write_name) #将write_name添加到file_list列表最后
sorted(file_list) #将列表中所有元素随机排列
number_of_lines = len(file_list) #列表中元素个数
#将图片信息写入txt文件中,逐行写入
for current_line in range(number_of_lines):
write_file.write(file_list[current_line] + '\n')
#关闭文件
write_file.close()
(2)检测指令
先输入./darknet detector test cfg/voc.data cfg/yolo-obj.cfg yolov4.weights
再输入 path of [test.txt]