darknet 識別獲取結果


examples/darknet.c文件中若使用detect命令可以看到調用了test_detector.

...
else if (0 == strcmp(argv[1], "detect")){
        float thresh = find_float_arg(argc, argv, "-thresh", .24);
        char *filename = (argc > 4) ? argv[4]: 0;
        char *outfile = find_char_arg(argc, argv, "-out", 0);
        int fullscreen = find_arg(argc, argv, "-fullscreen");
        test_detector("cfg/coco.data", argv[2], argv[3], filename, thresh, .5, outfile, fullscreen);
	    } 
...

test_detector位於examples/detector.c :562,對其識別結果調用了draw_detections,在入口

void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
中,存儲了識別結果的是dets,識別數量為num.

dets結構為:

typedef struct detection{
    box bbox;
    int classes;
    float *prob;
    float *mask;
    float objectness;
    int sort_class;
} detection;

box 結構為:

typedef struct{
    float x, y, w, h;
} box;

那是不是直接使用如下的訪問方式就行了呢?

printf("x=%lf,y=%lf,w=%lf,h=%lf",
        dets->bbox.x,dets->bbox.y,
        dets->bbox.w,dets->bbox.h);

但是運行結果並不如意

x=0.324301,y=0.611036,w=0.313848,h=0.573097

其實獲取正確的矩形數據應該在draw_detections里尋找才是,果不其然,bingo!

void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
...
	for(i = 0; i < num; ++i){
        ...

		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;

		// add
		printf("left=%d,right=%d,top=%d,bot=%d\n",left,right,top,bot);
		// end add
		...
}

將其在test_detector中嵌入,運行效果如下:

left=128,right=369,top=186,bot=517
left=533,right=621,top=94,bot=157
left=465,right=679,top=71,bot=169
left=205,right=576,top=151,bot=449

___________________________

(left,top)-----------
|				    |
|				    |
---------------(right,bot)

>[1] box,detection的定義在include\darknet.h >[2] detector.c在examples\detector.c >[3] image.c在src\image.c


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM