我想在Darknet YOLOv3上获得边界框坐标(如xmin,xmax,ymin,ymax),我尝试了一些方法,但是当我在更改代码后编译程序时,看不到任何差异。
如何找到Darknette的边界框坐标,或者为什么所做的更改不影响程序?
答案 0 :(得分:1)
如果您想获得一次检测的坐标,您可以使用 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
文件中找到的函数,但我认为函数已更改。
答案 1 :(得分:0)
在Yolo中,坐标是相对的。意味着注释是这样写的:
<object-class> <x_center> <y_center> <width> <height>
其中x_center
和y_center
是相对于图像宽度和高度的浮点值,它可以等于(0.0到1.0]。因此:
<x> = <absolute_x> / <image_width>
<y> = <absolute_y> / <image_height>
<width> = <box_absolute_width> / <image_width>
<height> = <box_absolute_height> / <image_height>
如果您使用的是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
以此类推。