YOLO物体检测中如何获取bounding box的坐标?

新手上路,请多包涵

在此处输入图像描述

我需要使用 YOLO 对象检测获取上图中生成的边界框坐标。

原文由 Shriram 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

一个快速的解决方案是修改 image.c 文件以打印出边界框信息:

 ...
if(bot > im.h-1) bot = im.h-1;

// Print bounding box values
printf("Bounding Box: Left=%d, Top=%d, Right=%d, Bottom=%d\n", left, top, right, bot);
draw_box_width(im, left, top, right, bot, width, red, green, blue);
...

原文由 Brian O‘Donnell 发布,翻译遵循 CC BY-SA 3.0 许可协议

对于 Windows 中的 python 用户:

首先…,做几个设置工作:

  1. 在环境路径中设置 darknet 文件夹的 python 路径:

PYTHONPATH = 'YOUR DARKNET FOLDER'

  1. 通过添加将 PYTHONPATH 添加到 Path 值:

%PYTHONPATH%

  1. edit file coco.data in cfg folder , by change the names folder variable to your coco.names folder, in my case:

names = D:/core/darknetAB/data/coco.names

使用此设置,您可以从任何文件夹调用 darknet.py(来自 alexeyAB\darknet 存储库)作为您的 python 模块。

开始编写脚本:

 from darknet import performDetect as scan #calling 'performDetect' function from darknet.py

def detect(str):
    ''' this script if you want only want get the coord '''
    picpath = str
    cfg='D:/core/darknetAB/cfg/yolov3.cfg' #change this if you want use different config
    coco='D:/core/darknetAB/cfg/coco.data' #you can change this too
    data='D:/core/darknetAB/yolov3.weights' #and this, can be change by you
    test = scan(imagePath=picpath, thresh=0.25, configPath=cfg, weightPath=data, metaPath=coco, showImage=False, makeImageOnly=False, initOnly=False) #default format, i prefer only call the result not to produce image to get more performance

    #until here you will get some data in default mode from alexeyAB, as explain in module.
    #try to: help(scan), explain about the result format of process is: [(item_name, convidence_rate (x_center_image, y_center_image, width_size_box, height_size_of_box))],
    #to change it with generally used form, like PIL/opencv, do like this below (still in detect function that we create):

    newdata = []
    if len(test) >=2:
        for x in test:
            item, confidence_rate, imagedata = x
            x1, y1, w_size, h_size = imagedata
            x_start = round(x1 - (w_size/2))
            y_start = round(y1 - (h_size/2))
            x_end = round(x_start + w_size)
            y_end = round(y_start + h_size)
            data = (item, confidence_rate, (x_start, y_start, x_end, y_end), w_size, h_size)
            newdata.append(data)

    elif len(test) == 1:
        item, confidence_rate, imagedata = test[0]
        x1, y1, w_size, h_size = imagedata
        x_start = round(x1 - (w_size/2))
        y_start = round(y1 - (h_size/2))
        x_end = round(x_start + w_size)
        y_end = round(y_start + h_size)
        data = (item, confidence_rate, (x_start, y_start, x_end, y_end), w_size, h_size)
        newdata.append(data)

    else:
        newdata = False

    return newdata

如何使用它:

 table = 'D:/test/image/test1.jpg'
checking = detect(table)'

获取坐标:

如果只有 1 个结果:

x1, y1, x2, y2 = checking[2]

如果结果很多:

 for x in checking:
    item = x[0]
    x1, y1, x2, y2 = x[2]
    print(item)
    print(x1, y1, x2, y2)

原文由 Wahyu Bram 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题