【问题标题】:Darknet Yolov3 Box Coordinates暗网 Yolov3 框坐标
【发布时间】:2019-07-29 10:42:02
【问题描述】:

我想在暗网 YOLOv3 上获取边界框坐标(如 xmin、xmax、ymin、ymax)我尝试了一些方法,但是当我在更改代码后编译程序时,我看不到任何差异。

如何找到 Darknette 的边界框坐标,或者为什么更改不会影响程序?

【问题讨论】:

  • 你使用的是哪个仓库?

标签: yolo darknet


【解决方案1】:

在 Yolo 中,坐标是相对的。这意味着注释是这样写的:

<object-class> <x_center> <y_center> <width> <height>

其中x_centery_center是相对于图像宽度和高度的浮点值,它可以等于(0.0到1.0]。所以:

&lt;x&gt; = &lt;absolute_x&gt; / &lt;image_width&gt;

&lt;y&gt; = &lt;absolute_y&gt; / &lt;image_height&gt;

&lt;width&gt; = &lt;box_absolute_width&gt; / &lt;image_width&gt;

&lt;height&gt; = &lt;box_absolute_height&gt; / &lt;image_height&gt;

如果你使用AlexeyAB code,你可以这样获取坐标:

darknet.exe detector test cfg/coco.data yolov3.cfg yolov3.weights -ext_output dog.jpg

要获取代码的坐标,您需要先计算每个坐标。例如,要获取 xmin:

xmin = (box_x-center - box_width/2) * img_width

等等。

【讨论】:

  • 我明白,但我不能像我说的那样做任何改变。你有什么想法吗?
  • 你应该把程序贴在这里。同时,如果没有看到您的更改,则可能是您没有在正确的位置进行更改。例如,您可以使用打印命令来查看您的更改是否有效。
  • @EmrahKalfa 你是如何编译你的程序的?做了一些修改后你有没有重新make暗网?
  • @HadiGhahremanNezhad 用于 xmin 的公式是否提供了测试图像的精确坐标。您能否提供一些相同的参考链接,以获取图像中所有对象坐标的确切位置 from_ext_output
【解决方案2】:

如果你想得到一个检测的坐标你可以使用AlexeyAB/darknet-ext_output标志@Hadi表示:

./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output data/person.jpg

或者您可以将其直接保存在文本文件中:

./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output data/person.jpg > output.txt

但如果您有大量图像,则需要一次检测所有图像并将其保存在 JSON 文件中:

./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output -out train.json < train.txt

train.txt 是您要检测的所有图像的目录所在的文件,train.json 是保存结果的 JSON 文件。

train.json 文件将如下所示:

[
{
 "frame_id":1, 
 "filename":"data/dataset/val/dog/image.png", 
 "objects": [ 
  {"class_id":0, "name":"dog", "relative_coordinates":{"center_x":0.452191, "center_y":0.809318, "width":0.349666, "height":0.378723}, "confidence":0.418734}, 
  {"class_id":1, "name":"cat", "relative_coordinates":{"center_x":0.454491, "center_y":0.891459, "width":0.397718, "height":0.220163}, "confidence":0.024015}
 ] 
}, 
{
 "frame_id":2, 
 "filename":"data/dataset/val/dog/image2.jpg", 
 "objects": [ 
  {"class_id":0, "name":"dog", "relative_coordinates":{"center_x":0.444495, "center_y":0.539488, "width":0.297957, "height":0.307668}, "confidence":0.991456}
 ] 
}, 
...
...
]

要访问这些值,您可以使用以下代码:

import json

def get_data(distros_dict):
    json_to_variable = []
    # For every frame.
    for distro in distros_dict:
        filename = distro['filename']
        if len(distro['objects']) != 0:
            # For every detection.
            for obj in range(len(distro['objects'])):
                # Get values.
                frame_id = distro['frame_id']
                class_id = distro['objects'][obj]["class_id"]
                x = distro['objects'][obj]["relative_coordinates"]["center_x"]
                y = distro['objects'][obj]["relative_coordinates"]["center_y"]
                width = distro['objects'][obj]["relative_coordinates"]["width"]
                height = distro['objects'][obj]["relative_coordinates"]["height"]
                confidence = distro['objects'][obj]["confidence"]
                # And save them.
                print(f"{frame_id} {class_id} {x} {y} {width} {height} {confidence}")
                json_to_variable.append([frame_id, class_id, x, y, width, height, confidence])

        # If you need to use json_to_variable here, move "json_to_variable = []" inside "for distro in distros_dict:"
    # Add your code here.
    # Or return json_to_variable to use it outside this function.

with open('train.json', 'r') as f:
    distros_dict_train = json.load(f)
with open('test.json', 'r') as f:
    distros_dict_test = json.load(f)
    
get_data(distros_dict_train)
get_data(distros_dict_test)

另一个选项是导入daknet,就像提到的here一样。这里调用的函数是你可以在darknet.py文件中找到的函数,但我认为函数已经改变了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-25
    • 2020-08-26
    • 1970-01-01
    • 2020-07-24
    • 2019-09-14
    • 2023-02-12
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多