【问题标题】:Crop all bounding boxes from training and testing data after tensorflow object detection API在 tensorflow 对象检测 API 之后从训练和测试数据中裁剪所有边界框
【发布时间】:2021-08-15 22:12:39
【问题描述】:

我已经在 google colaboratory 上训练了用于文本检测的 tensorflow 对象检测 API。使用此代码:

#run detector on test image
#it takes a little longer on the first run and then runs at normal speed. 
import random

TEST_IMAGE_PATHS = glob.glob('/content/gdrive/MyDrive/Final_datasets/UTiV/test/*.jpg')
image_path = random.choice(TEST_IMAGE_PATHS)
image_np = load_image_into_numpy_array(image_path)

# Things to try:
# Flip horizontally
# image_np = np.fliplr(image_np).copy()

# Convert image to grayscale
# image_np = np.tile(
#     np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)

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=200,
  min_score_thresh=.5,
  agnostic_mode=False,
  )

 plt.figure(figsize=(12,16))
 plt.imshow(image_np_with_detections)
 plt.show()

现在,我想裁剪这些边界框。

width=600
height=900

ymin = int((boxes[0][0][0]*height))
xmin = int((boxes[0][0][1]*width))
ymax = int((boxes[0][0][2]*height))
xmax = int((boxes[0][0][3]*width))

Result = np.array(img_np[ymin:ymax,xmin:xmax])

但它说:

NameError: name 'boxes' is not defined

我们将不胜感激。

【问题讨论】:

  • 变量boxes 没有在上面的代码中定义。如果它应该是任何函数的输出,请确保在尝试在后续分析中使用该变量之前发生这种情况。

标签: python tensorflow object-detection-api


【解决方案1】:

我认为您正在尝试做的是,从该列表中提取边界框detections['detection_boxes'][0]

提取边界框的更好解决方案是访问visualize_boxes_and_labels_on_image_array() 函数,您可以在object_detection/utils/visualization_utils.py 中找到该函数。在通过使用draw_bounding_box_on_image() 在图像上可视化它们之前,它已经具有detections['detection_boxes'][0] 的后期处理版本。您可以从那里以列表的形式返回它们并在此处访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多