简体   繁体   中英

Crop all bounding boxes from training and testing data after tensorflow object detection API

I have trained tensorflow object detection API for text detection on google colaboratory. Using this code:

#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()

在此处输入图像描述

Now, I want to crop these bounding boxes.

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])

But it says:

NameError: name 'boxes' is not defined

Any help would bee highly appreciated.

I think what you are trying to do is, to extract bounding boxes from this list detections['detection_boxes'][0] .

A better solution for extracting the bounding boxes would be to access visualize_boxes_and_labels_on_image_array() function which you can find in object_detection/utils/visualization_utils.py . It already has post process version of detections['detection_boxes'][0] before visualizing them on the image through using draw_bounding_box_on_image() . You can return them from there in a list and access it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM