1. 程式人生 > >yolo2檢測到的物體位置輸出

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,第一行表示檢測出來的總數

這裡寫圖片描述