在tensorflow对象检测API之后,从训练和测试数据裁剪所有边界框

2024-06-03 11:37:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在GoogleColaboratorial上为文本检测培训了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()

enter image description here

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

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

任何帮助都将不胜感激


Tags: toimagenumpyonwithnppltwidth
1条回答
网友
1楼 · 发布于 2024-06-03 11:37:40

我认为您试图做的是,从这个列表中提取边界框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]的后期处理版本。您可以从列表中返回它们,并在此处访问它们

相关问题 更多 >