首页 > 解决方案 > 用tensorflow 2预测前得到模型提出的区域

问题描述

我尝试使用 tensorflow 2 进行对象检测,但我不知道如何执行以下操作:

在教程 TF2 对象检测中,获取一张图像中某些元素的推断,如以下代码所示:

  image_np = load_image_into_numpy_array(image_path)
  input_tensor = tf.convert_to_tensor(image_np)
  input_tensor = input_tensor[tf.newaxis, ...]
  detections = detect_fn(input_tensor)

但我需要在类推理之前获取元素或区域。我需要访问所有建议进行更改或插入新区域的区域,然后获取每个区域的类。

我的代码如下:

def make_inference(image_path,counter,image_save):
  print('Running inference for {}... '.format(image_path), end='')
  image_np = load_image_into_numpy_array(image_path)
  input_tensor = tf.convert_to_tensor(image_np)
  input_tensor = input_tensor[tf.newaxis, ...]
  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
  detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
  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'],
        detections['detection_scores'],
        category_index,
        use_normalized_coordinates=True,
        max_boxes_to_draw=200,
        min_score_thresh=.5,
        agnostic_mode=False)
  plt.axis('off')
  plt.imshow(image_np_with_detections)
  nombre = str(counter)+'.jpg'
  plt.savefig('/content/RESULTADOS/'+nombre,  dpi=dpi ,bbox_inches='tight')
  counter = counter+1
  plt.clf()

提前致谢。

标签: tensorflowtensorflow2.0

解决方案


推荐阅读