darknetYolov3图片的分类计数、置信度以及输出xmin,xmax,ymin,ymax的结果

本文分享了作者在C语言Darknet中修改代码实现目标检测计数的过程,包括如何显示检测框上的计数器及输出xmin,xmax,ymin,ymax坐标。代码修改主要在src文件夹的image.c中,通过解析检测结果,计算坐标,并为不同类别目标计数,最终将计数结果添加到检测框上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前一直没找到c语言的darknet的计数代码,就自己改了。就是一个计数器再显示在识别框上,输出xmin,xmax,ymin,ymax的坐标是因为比赛需要,然而大佬们都不用yolo了根本拼不过。。。很久以前改的可能有的地方做了修
改忘记了,如有问题在评论里问一下。
具体修改就在src文件夹的image.c里,找到draw_detections_v3这个函数修改就行。代码如下,修改部分有中文注释。

void draw_detections_v3(image im, detection* dets, int num, float thresh, char** names, image** alphabet, int classes, int ext_output, char* impath)
{
    static int frame_id = 0;
    frame_id++;
    int selected_detections_num;
    detection_with_class* selected_detections = get_actual_detections(dets, num, thresh, &selected_detections_num, names);
    // text output
    qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_lefts);
    int i;
    for (i = 0; i < selected_detections_num; ++i)
    {
        //计算xmin,xmax,ymin,ymax
        float xmin = round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2) * im.w);
        float ymin = round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2) * im.h);
        float xmax = round((selected_detections[i].det.bbox.x + selected_detections[i].det.bbox.w / 2) * im.w);
        float ymax = round((selected_detections[i].det.bbox.y + selected_detections[i].det.bbox.h / 2) * im.h);
        if (xmin < 0)
        {
            xmin = 0;
        }
        if (ymin < 0)
        {
            ymin = 0;
        }
        if (xmax < 0)
        {
            xmax = 0;
        }
        if (ymax < 0)
        {
            ymax = 0;
        }
        const int best_class = selected_detections[i].best_class;
        printf("%s,%s,%.15f%,", names[best_class], impath, selected_detections[i].det.prob[best_class]);
        //改输出函数
        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));
            printf("\txmin: %4.0f   ymin: %4.0f   xmax: %4.0f   ymax: %4.0f\n",*/
            printf("%4.0f,%4.0f,%4.0f,%4.0f\n",
                xmin,
                ymin,
                xmax,
                ymax);

        else
            printf("\n");
        int j;
        for (j = 0; j < classes; ++j) {

            if (selected_detections[i].det.prob[j] > thresh&& j != best_class) {
                printf("%s,%s,%.15f%", names[j], impath, selected_detections[i].det.prob[j]);

                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));
                    */
                    //printf("xmin: %4.0f   ymin: %4.0f   xmax: %4.0f   ymax: %4.0f\n",
                    printf(",%4.0f,%4.0f,%4.0f,%4.0f\n",
                        xmin,
                        ymin,
                        xmax,
                        ymax);

                else
                    printf("\n");
            }

        }

    }

    // image output
    qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_probs);

    //记录识别框ID
    int imageID_1 = 0;//海胆
    int imageID_2 = 0;//海参
    int imageID_3 = 0;//扇贝
    int imageID_4 = 0;//海星
    for (i = 0; i < selected_detections_num; ++i)
    {

        int width = im.h * .006;
        if (width < 1)
            width = 1;

        /*
        if(0){
        width = pow(prob, 1./2.)*10+1;
        alphabet = 0;
        }
        */

        //printf("%d %s: %.0f%%\n", i, names[selected_detections[i].best_class], prob*100);
        int offset = selected_detections[i].best_class * 123457 % classes;
        float red = get_color(2, offset, classes);
        float green = get_color(1, offset, classes);
        float blue = get_color(0, offset, classes);
        float rgb[3];

        //width = prob*20+2;

        rgb[0] = red;
        rgb[1] = green;
        rgb[2] = blue;
        box b = selected_detections[i].det.bbox;
        //printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);

        int left = (b.x - b.w / 2.) * im.w;
        int right = (b.x + b.w / 2.) * im.w;
        int top = (b.y - b.h / 2.) * im.h;
        int bot = (b.y + b.h / 2.) * im.h;

        if (left < 0) left = 0;
        if (right > im.w - 1) right = im.w - 1;
        if (top < 0) top = 0;
        if (bot > im.h - 1) bot = im.h - 1;

        //int b_x_center = (left + right) / 2;
        //int b_y_center = (top + bot) / 2;
        //int b_width = right - left;
        //int b_height = bot - top;
        //sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height);

        // you should create directory: result_img
        //static int copied_frame_id = -1;
        //static image copy_img;
        //if (copied_frame_id != frame_id) {
        //    copied_frame_id = frame_id;
        //    if (copy_img.data) free_image(copy_img);
        //    copy_img = copy_image(im);
        //}
        //image cropped_im = crop_image(copy_img, left, top, right - left, bot - top);
        //static int img_id = 0;
        //img_id++;
        //char image_name[1024];
        //int best_class_id = selected_detections[i].best_class;
        //sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, best_class_id, names[best_class_id]);
        //save_image(cropped_im, image_name);
        //free_image(cropped_im);

        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
        }
        if (alphabet)
        {

            char echinus[7] = "echinus";
            char holothurian[11] = "holothurian";
            char scallop[7] = "scallop";
            char starfish[8] = "starfish";

            char labelstr[4096] = { 0 };
            strcat(labelstr, names[selected_detections[i].best_class]);

            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 buff[10];
            //_gcvt((selected_detections[i].det.prob[selected_detections[i].best_class] * 100), 5, buff);
            sprintf(buff, "%.2f", selected_detections[i].det.prob[selected_detections[i].best_class] * 100);
            char prob[] = ":";
            strcat(labelstr, prob);
            strcat(labelstr, buff);
            strcat(labelstr, "%");
            char prob2[] = " NO:";
            strcat(labelstr, prob2);
            //计数并编号
            if (!strcmp(names[selected_detections[i].best_class], echinus))
            {
                imageID_1++;
                char buff2[5];
                strcpy(buff2, "");
                strcat(labelstr, _itoa(imageID_1, buff2, 10));
            }
            else if (!strcmp(names[selected_detections[i].best_class], holothurian))
            {
                imageID_2++;
                char buff2[5];
                strcpy(buff2, "");
                strcat(labelstr, _itoa(imageID_2, buff2, 10));
            }
            else if (!strcmp(names[selected_detections[i].best_class], scallop))
            {
                imageID_3++;
                char buff2[5];
                strcpy(buff2, "");
                strcat(labelstr, _itoa(imageID_3, buff2, 10));
            }
            else
            {
                imageID_4++;
                char buff2[5];
                strcpy(buff2, "");
                strcat(labelstr, _itoa(imageID_4, buff2, 10));
            }


            image label = get_label_v3(alphabet, labelstr, (im.h * .03));
            draw_label(im, top + width, left, label, rgb);
            free_image(label);
        }
        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);
        }
    }
    free(selected_detections);
}

附上测试结果(批量测试并保存图片,看了几篇博客,找不到链接了)
在这里插入图片描述

输出检测结果(图片、置信度、位置坐标)到txt文档(这个改的地方有点杂,回来整理整理再发出来)
在这里插入图片描述

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值