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

posted on 2018-08-28 16:11  leihui  阅读(2001)  评论(0编辑  收藏  举报