Python Tensorflow对象检测API裁剪图像片段

Python Tensorflow对象检测API裁剪图像片段,python,tensorflow,crop,image-segmentation,object-detection-api,Python,Tensorflow,Crop,Image Segmentation,Object Detection Api,我正在使用Tensorflow对象检测API,模型可以检测带有边界框和遮罩的对象 这是我的密码: def run_inference_for_single_image_raw(image, graph): with graph.as_default(): with tf.Session() as sess: ops = tf.get_default_graph().get_operations() all_tensor_names = {output.name

我正在使用Tensorflow对象检测API,模型可以检测带有边界框和遮罩的对象

这是我的密码:

def run_inference_for_single_image_raw(image, graph):
  with graph.as_default():
    with tf.Session() as sess:
      ops = tf.get_default_graph().get_operations()
      all_tensor_names = {output.name for op in ops for output in op.outputs}
      tensor_dict = {}
      for key in [
          'num_detections', 'detection_boxes', 'detection_scores',
          'detection_classes', 'detection_masks'
      ]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
          tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
              tensor_name)
      if 'detection_masks' in tensor_dict:
        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
            detection_masks, detection_boxes, image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(
            tf.greater(detection_masks_reframed, 0.5), tf.uint8)
        tensor_dict['detection_masks'] = tf.expand_dims(
            detection_masks_reframed, 0)
      image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

      output_dict = sess.run(tensor_dict,
                             feed_dict={image_tensor: np.expand_dims(image, 0)})

  return output_dict
因此,如果我运行以下代码:

vis_util.visualize_boxes_and_labels_on_image_array(
      image,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)
plt.figure(figsize=(12, 8))
plt.grid(False)
plt.imshow(image)
结果是(带有边界框和遮罩的图像):

因此,如何通过遮罩路径裁剪图像对象,而不是边界框,因此在本例中,我希望仅在透明背景上使用对象(cat/瓶子)输出图像。(可能使用PIL或OpenCV等)

因此,如果
输出dict.get('detection\u masks')
是numpy对象,实际上是二进制掩码,您可以通过简单的相乘或使用
np.where

mask = output_dict.get('detection_masks')
img_cropped = img * mask
这将裁剪所有检测到的对象,但如果要单独裁剪对象,可以通过检测轮廓来实现。我们可以为此使用
scikit image

from skimage import measure
label_mask = measure.label(mask)
现在,我们已经标记了二进制图像中所有连接的组件,并为每个组件指定了数字标签(通过更改像素值)。标签从“1”开始,以对象数结束

single_object_mask = (label_mask == 1) #or 2, 3...

这将使用您提供的标签过滤标签遮罩图像。您还可以使用边界框信息来裁剪特定对象。

是否可以粘贴“输出dict.get('detection\u masks')的形状?或者所有的形状,如果它有子列表?@noumanniazkhan,带有一些图像
输出dict.get('detection\u masks')。shape
,得到了这个
(3,1080,1920)
,其中3-找到的对象的数量,1920-像素中的图像宽度,和1080-像素中的图像高度。因此,它带来了所有找到的对象的numpy数组,维度为1920x1080,元素为0和1。因此,这是每个对象的二进制掩码。所以,我想知道,如何通过这些二值遮罩裁剪初始图像。让我写下答案。