我已经在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()
现在,我想裁剪这些边界框
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
任何帮助都将不胜感激
我认为您试图做的是,从这个列表中提取边界框
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]
的后期处理版本。您可以从列表中返回它们,并在此处访问它们相关问题 更多 >
编程相关推荐