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);
}
复制代码

 

posted @   菠萝超级酸  阅读(2455)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示