如何使用 Tensorflow 在视频中的目标检测中获取预测边界框的坐标甚至中心点

问题描述

我正在尝试从视频中进行实时对象检测。我的模型工作正常,但在我的最后阶段,我想打印预测边界框的坐标。现在我是在视频中做的,我想连续打印这些坐标。 这是发生可视化的代码

vis_util.visualize_Boxes_and_labels_on_image_array(
   image_np,np.squeeze(Boxes),np.squeeze(classes).astype(np.int32),np.squeeze(scores),category_index,use_normalized_coordinates=True,line_thickness=3,)

我尝试执行 print(Boxes),但它打印了许多数组。例如

[0.0000000e+00 0.0000000e+00 6.3184214e-01 3.3072531e-01]
[7.6686603e-01 4.3631636e-02 1.0000000e+00 2.1988428e-01]
[5.0896335e-01 4.1433451e-01 1.0000000e+00 1.0000000e+00]
[1.6146693e-01 5.1699680e-01 8.4259009e-01 9.7707957e-01]
[0.0000000e+00 1.1906862e-02 7.6165682e-01 5.2043945e-01]
[9.5170856e-01 8.5603885e-02 1.0000000e+00 2.2153431e-01]
[4.1733772e-02 8.4026903e-01 3.4761459e-01 9.9046725e-01]
...
...
...
...
and many more

我希望将这些预测的边界框坐标打印在控制台中。 我想要有人帮我解决这个问题。

解决方法

在您的配置文件中,有一个名为 max_boxes_to_predict 之类的参数。 TF OD API 将始终预测此数量的框。它还会输出这些框的置信度,其中大部分应该在 0 左右。您可以根据 detection_scores 选择保留这些框中的哪一个。

,

可以通过以下方式获取边界框坐标

for box in boxes[0]:
    xmin = box[1]*width
    ymin = box[0]*height
    xmax = box[3]*width
    ymax = box[2]*height

其中 widthheight 分别是图像的宽度和高度 它们可以通过 height,width,channels = image_np.shape

,

我通过添加以下代码找到了我正在寻找的解决方案。 任何遇到类似问题的人,可以参考此代码。

for i in range(len(boxes)):
   xmin = (int(box[i,0]*width))
   ymin = (int(box[i,1]*height))
   xmax = (int(box[i,2]*width))
   ymax = (int(box[i,3]*height))

我尝试过这样做并且对我有用。我通过用这些坐标绘制另一个矩形来确认它。它与确认坐标的预测边界框重叠。