【问题标题】:How to get the multiple bounding box coordinates in tensorflow object-detection API如何在 tensorflow 对象检测 API 中获取多个边界框坐标
【发布时间】:2019-10-11 00:04:52
【问题描述】:

我想获取多个边界框的坐标和每个边界框的类,并将其作为 JSON 文件返回。

当我从以下代码中打印 box[] 时,它的形状为 (1,300,4)。 box[] 中有 300 个坐标。但是我的预测图像上只有 2 个。我想要在我的图像上预测的边界框的坐标。

另外,我们如何知道哪个边界框映射到图像中的哪个类别/类?

例如,假设我在图像中有一只狗和一个人,我怎么知道哪个边界框对应于狗类,哪个边界框对应于人类? box[] 为我们提供了一个形状为 (1,300,4) 的数组,没有任何指示哪个边界框对应于图像中的哪个类。

我按照这个answer 使用阈值分数从框[] 中的 300 个坐标中获取边界框坐标。

我尝试获取得分最高的边界框。但即使预测图像有多个边界框,它也只会返回一个边界框。

得分最高的边界框坐标甚至与预测图像上的边界框坐标不匹配。如何获取预测图像上的边界框坐标?

            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=8)
            im = Image.fromarray(image_np)

            true_boxes = boxes[0][scores[0]==scores.max()]    # Gives us the box with max score
            for i in range(true_boxes.shape[0]):   # rescaling the coordinates
                ymin = true_boxes[i,0]*height
                xmin = true_boxes[i,1]*width
                ymax = true_boxes[i,2]*height
                xmax = true_boxes[i,3]*width

我从上面的代码中得到的坐标 xmin,ymin,xmax,ymax(具有最高分数)与预测图像上的边界框坐标不完全匹配。它们偏离了几个像素。 此外,即使预测图像有多个边界框和多个类别(例如:狗和人),我也只能得到一个边界框。

我想返回一个 JSON 文件,其中包含与每个边界框对应的 image_name、bounding_boxes 和类。

谢谢,我是新手。请询问您是否不理解问题的任何部分。

【问题讨论】:

  • 一个常见的错误是把heightwidth弄乱了,你有没有尝试过切换两者,看看结果是否正确?
  • 高度和宽度不是问题。我检查了 box[] 中得分最高的边界框,并将其与图像上的边界框进行了比较。它们非常接近,但只有几个像素。我认为高度和宽度值不是问题。
  • 没关系,我想通了。谢谢。

标签: python-3.x tensorflow object-detection bounding-box object-detection-api


【解决方案1】:

我在这里link 关注了这个答案,我找到了我所有的边界框坐标:

min_score_thresh=0.60
true_boxes = boxes[0][scores[0] > min_score_thresh]
for i in range(true_boxes.shape[0]):
    ymin = int(true_boxes[i,0]*height)
    xmin = int(true_boxes[i,1]*width)
    ymax = int(true_boxes[i,2]*height)
    xmax = int(true_boxes[i,3]*width)

    roi = image[ymin:ymax,xmin:xmax].copy()
    cv2.imwrite("box_{}.jpg".format(str(i)), roi)

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 2019-09-30
    • 1970-01-01
    • 2020-06-23
    • 2019-09-18
    • 2019-02-24
    • 2018-08-17
    • 2022-10-24
    • 2018-04-17
    相关资源
    最近更新 更多