Python tensorflow中边界框的坐标

Python tensorflow中边界框的坐标,python,python-3.x,tensorflow,object-detection-api,Python,Python 3.x,Tensorflow,Object Detection Api,我想要tensorflow模型中预测边界框的坐标。 我正在使用来自的对象检测脚本。 在回答了stackoverflow的一些问题后,我将最后一个检测块修改为 for image_path in TEST_IMAGE_PATHS: image = Image.open(image_path) # the array based representation of the image will be used later in order to prepare the # result

我想要tensorflow模型中预测边界框的坐标。
我正在使用来自的对象检测脚本。
在回答了stackoverflow的一些问题后,我将最后一个检测块修改为

for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  output_dict = run_inference_for_single_image(image_np, detection_graph)
  # Visualization of the results of a detection.
  width, height = image.size
  print(width,height)
  ymin = output_dict['detection_boxes'][5][0]*height
  xmin = output_dict['detection_boxes'][5][1]*width
  ymax = output_dict['detection_boxes'][5][2]*height
  xmax = output_dict['detection_boxes'][5][3]*width
  #print(output_dict['detection_boxes'][0])
  print (xmin,ymin)
  print (xmax,ymax)
但是,在输出dict['detection\u box']中有100个元组。
即使对于那些它无法预测的图像,也有100个元组


我想要的是单个图像的所有边界框的坐标。

如果检查正在使用的模型的pipeline.config文件,可以看到在某些地方,框的最大数量设置为100。 例如,在中,您可以在下面看到演示笔记本中的模型

post_processing {
  batch_non_max_suppression {
    ...
    max_detections_per_class: 100
    max_total_detections: 100
  }
}

这也是输入读取器的默认值(对于train和eval),您可以更改它们,但这仅与您正在培训/评估的原因有关。如果您希望在不重新训练模型的情况下进行推理,只需使用预先训练过的模型(同样,如ssd\u mobilenet\u v1),并在使用
--config\u override
参数时自行使用该模型,以更改我在NMS中提到的值。

如果您检查正在使用的模型的pipeline.config文件,您可以看到,在某些地方,最大框数设置为100。 例如,在中,您可以在下面看到演示笔记本中的模型

post_processing {
  batch_non_max_suppression {
    ...
    max_detections_per_class: 100
    max_total_detections: 100
  }
}

这也是输入读取器的默认值(对于train和eval),您可以更改它们,但这仅与您正在培训/评估的原因有关。如果您希望在不重新训练模型的情况下进行推理,只需使用预先训练过的模型(同样,如ssd\u mobilenet\u v1),并在使用
--config\u override
参数时自行进行推理,以更改我在NMS中提到的值。

展开dims行后,您可以添加这些代码。过滤的_box变量将给出预测值大于0.5的边界框

  (boxes, scores, classes, num) = sess.run(
      [detection_boxes, detection_scores, detection_classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})
  indexes = []
  import os
  for i in range (classes.size):
    if(classes[0][i] in range(1,91) and scores[0][i]>0.5):
        indexes.append(i)
  filtered_boxes = boxes[0][indexes, ...]
  filtered_scores = scores[0][indexes, ...]
  filtered_classes = classes[0][indexes, ...]
  filtered_classes = list(set(filtered_classes))
  filtered_classes = [int(i) for i in filtered_classes]

展开_dims行后,可以添加这些代码。过滤的_box变量将给出预测值大于0.5的边界框

  (boxes, scores, classes, num) = sess.run(
      [detection_boxes, detection_scores, detection_classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})
  indexes = []
  import os
  for i in range (classes.size):
    if(classes[0][i] in range(1,91) and scores[0][i]>0.5):
        indexes.append(i)
  filtered_boxes = boxes[0][indexes, ...]
  filtered_scores = scores[0][indexes, ...]
  filtered_classes = classes[0][indexes, ...]
  filtered_classes = list(set(filtered_classes))
  filtered_classes = [int(i) for i in filtered_classes]
使用上述代码,您将获得检测到的类的所需边界框坐标,最大分数位于第一个方括号指示的第0个位置

使用上述代码,您将获得检测到的类的所需边界框坐标,最大分数位于第一个方括号指示的第0个位置