YOLOv4检测图片添加置信度和计数
本文章向大家介绍 YOLOv4检测图片添加置信度和计数 ,主要包括 YOLOv4检测图片添加置信度和计数 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
主要修改的image.c文件,在darknet目录下直接ctrl+f搜即可,然后打开,找到draw_detections_v3函数,加入用来计数的变量。(我的改法其实有点问题,如果置信度位数过多的话左上角第二行会重复。我懒的研究,直接把置信度位数改小,让第二行盖过它。)
void draw_detections_v3(.....)
.....
qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_probs);
int image_nut=0;//记录螺母数量 (在image.c的370行左右)
int image_bolt=0;//记录螺栓数量
for (i = 0; i < selected_detections_num; ++i) {
int width = im.h * .002;
if (width < 1)
width = 1;
.....
char nut[30];//*左上角第一行字符串 (430行左右)
char bolt[30]; //*左上角第二行字符串
if (im.c == 1) {
draw_box_width_bw(im, left, top, right, bot, width, 0.8); // 1 channel Black-White
}
else {
draw_box_width(im, left, top, right, bot, width, red, green, blue); // 3 channels RGB
}
....
int j;
for (j = 0; j < classes; ++j) {
if (selected_detections[i].det.prob[j] > thresh && j != selected_detections[i].best_class) {
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
}
}
char con[20]={0};//置信度添加 450行左右
sprintf(con, "%.4f", selected_detections[i].det.prob[selected_detections[i].best_class]);
strcat(labelstr, ":");
strcat(labelstr, con);//
if(!strcmp(names[selected_detections[i].best_class], "nut"))//
{
image_nut++;// 计数开始
}
else if(!strcmp(names[selected_detections[i].best_class], "bolt"))
{
image_bolt++;
}
//
image label = get_label_v3(alphabet, labelstr, (im.h*.02));
//draw_label(im, top + width, left, label, rgb);
draw_weighted_label(im, top + width, left, label, rgb, 0.7);
....
if (selected_detections[i].det.mask) {
image mask = float_to_image(14, 14, 1, selected_detections[i].det.mask);
image resized_mask = resize_image(mask, b.w*im.w, b.h*im.h);
image tmask = threshold_image(resized_mask, .5);
embed_image(tmask, im, left, top);
free_image(mask);
free_image(resized_mask);
free_image(tmask);
}
if (i==(selected_detections_num-1))//最终结果写入 484行左右
{ sprintf(nut,"nut_num: %d",image_nut);
sprintf(bolt,"bolt_num: %d",image_bolt);
}
image label_nut=get_label_v3(alphabet, nut, (im.h*.03));//last varible is size
draw_label(im, 100, 150, label_nut, rgb); //显示函数
free_image(label_nut);
image label_bolt=get_label_v3(alphabet, bolt, (im.h*.03));//last varible is size
draw_label(im, 260, 150, label_bolt, rgb);
free_image(label_bolt); //
}
free(selected_detections);
}