yolo2检测到的物体位置输出

说明

检测到的物体被box所标记,输出box所在的位置,并存储到txt中。(待完善,如何根据每个图片名字存储不同的txt)

进入src/image.c,修改void draw_detections函数。

void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes)
{
    int i;
    char a[100];
    FILE *fp;
    if((fp=fopen("/home/maqy/darknet/ship.txt","w"))==NULL){
        printf("/home/maqy/darknet/ship.txt can't open\n");
        exit(1);
    }

    int shipnum=0;//船的数量
    for(i = 0; i < num; ++i){
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        if(prob > thresh){

        ++shipnum;//如果高于阈值,就数目加1

            int width = im.h * .006;

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

            //printf("%d %s: %.0f%%\n", i, names[class], prob*100);
            printf("%s: %.0f%% ", names[class], prob*100);
            int offset = 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 = boxes[i];

            int left  = (b.x-b.w/2.)*im.w;//距离图片左边界的值  right-left为box宽度
            int right = (b.x+b.w/2.)*im.w;
            int top   = (b.y-b.h/2.)*im.h;//距离图片上边界的值  bot-top为box高度
            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;

            //maqy-输出box的位置
        printf("%d %d %d %d\n", left, right, top, bot);
        sprintf(a,"%d %d %d %d\n",left,right,top,bot);
        printf("a:%s\n",a);
        fputs(a,fp);

            draw_box_width(im, left, top, right, bot, width, red, green, blue);
            if (alphabet) {
                image label = get_label(alphabet, names[class], (im.h*.03)/10);
                draw_label(im, top + width, left, label, rgb);
                free_image(label);
            }
            if (masks){
                image mask = float_to_image(14, 14, 1, masks[i]);
                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);
            }
        }
    }
    printf("shipNum:%d\n",shipnum);//输出船的数目.
    sprintf(a,"%d\n",shipnum);
    printf("a:%s",a);
    rewind(fp);//回到文件开头
    fputs(a,fp);
    fclose(fp);
}

修改完后需要对darknet重新编译。

修改完后的终端输出:

这里写图片描述

存储的txt,第一行表示检测出来的总数

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013036495/article/details/75533056
今日推荐