![](/img/trans.png)
[英]How to perform 10 Crop Image Augmentation at training time using Tensorflow 2.0 Dataset
[英]How to crop an image using TensorFlow?
我正在嘗試裁剪檢測到 object 的圖像。 從 TensorFlow 的文檔中有一個 function。
tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
我正在嘗試弄清楚如何獲取給定的 arguments 但不確定要使用哪些信息。 這是我正在使用的代碼。
img = cv2.imread(IMAGE_PATH)
image_np = np.array(img)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
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'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=9,
min_score_thresh=.5,
agnostic_mode=False)
#print(detections['detection_boxes'])
plt.imshow(cv2.cvtColor(image_np_with_detections, cv2.COLOR_BGR2RGB))
plt.show()
結果就是這樣,我試圖裁剪除邊界框之外的所有內容 -已識別 object
如果問題是為了獲得解決方案來裁剪邊界框的內容並丟棄它之外的所有內容,則應該像這樣工作:
# ’image’ is the input image tensor
boxes = detections['detection_boxes']
cropped_boxes = []
# this currently crops all bounding boxes, not taking into account the scores
for i in range(boxes.shape[0]):
box = tuple(boxes[i].tolist())
ymin, xmin, ymax, xmax = box
cropped_boxes.append(
tf.image.crop_to_bounding_box(
image=image,
offset_height=ymin,
offset_width=xmin,
target_height=ymax-ymin,
target_width=xmax-xmin
))
有關如何解釋框的信息可以從用於繪制結果的問題中使用的方法visualize_boxes_and_labels_on_image_array
的源代碼中找到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.