【问题标题】:How do I get the pixel coordinates for a specific bounding box如何获取特定边界框的像素坐标
【发布时间】:2021-01-05 06:57:49
【问题描述】:

我正在尝试从 person 类中获取边界框的像素坐标(标记为: mscoco_label_map.pbtxt

item {
  name: "/m/01g317"
  id: 1
  display_name: "person"
}

目前我正在通过

将边界框和标签添加到图像上
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections, predictions_dict, shapes = detect_fn(input_tensor)
label_id_offset = 1
image_np_with_detections = image_np.copy()

viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'][0].numpy(),
          (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
          detections['detection_scores'][0].numpy(),
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=3,
          min_score_thresh=.30,
          agnostic_mode=False)

(所有代码都在while循环中)

但是当我打印出 detections['detection_boxes'] 时,我得到了很多标准化坐标,我不知道如何将这些坐标排序到特定的框,例如。人标签。

那么如何在 detections['detection_boxes'] 中获取特定边界框的像素坐标?

StackOverflow 的新手,非常感谢任何提示。

【问题讨论】:

  • 所以你想从标准化坐标转换为像素坐标?
  • 是的,特别是人物边界框的坐标。

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


【解决方案1】:

所以detection_boxes 应该是一个 N x 4 的边界框坐标数组,格式为标准化坐标中的[ymin, xmin, ymax, xmax],detection_classes 应该是一个 (`float?) 数字类标签数组。我假设他们没有对 API 进行太多更改,因为我从去年初就没有使用过对象检测 API。

您应该能够执行类似的操作来转换为像素坐标,然后只使用一组标签。

detection_boxes = detections['detection_boxes'][0].numpy()
detection_classes = detections['detection_classes'][0].numpy().astype(int) + label_id_offset
detection_scores = detections['detection_scores'][0].numpy()

# Scale to pixel co-ordinates
detection_boxes[:, (0, 2)] *= IMAGE_HEIGHT
detection_boxes[:, (1, 3)] *= IMAGE_WIDTH

# Select person boxes
cond = (detection_classes == PERSON_CLASS_ID) & (detection_scores >= SCORE_THRESH)
person_boxes = detection_boxes[cond, :]
person_boxes = np.round(person_boxes).astype(int)

【讨论】:

  • 谢谢!它可以工作,但即使最终预览窗口中显然只有一个人框,我仍然会得到 20 多个数组 - 知道吗?
  • 哦,是的,我忘了我认为它只会返回所有边界框,因此您还需要在分数上包含一个阈值。我已经更新了答案。
  • 一个愚蠢的问题:person_boxes 是什么?它是一个嵌套数组吗,因为我得到了 ex。 [[ 519 273 1489 738]] 作为我打印时的结果。
  • 它仍然是一个与detection_boxes 格式相同的二维数组,只是盒子更少。
  • 我如何访问二维数组中的四个数字之一?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 2021-04-04
  • 2020-03-03
  • 1970-01-01
  • 2021-08-29
  • 2015-11-26
  • 2021-12-13
相关资源
最近更新 更多