在图像上执行对象检测后如何打印检测到的类? [英] How to print the detected classes after performing object detection on an image?

查看:130
本文介绍了在图像上执行对象检测后如何打印检测到的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 object_detection_tutorial.ipynb 教程.

这是代码(我只放了需要的部分,其余代码与笔记本相同):

Here is the code ( I only put parts which are needed, the rest of the code is the same as the notebook):

my_results = []   # I added this, a list to hold the detected classes

PATH_TO_LABELS = 'D:\\TensorFlow\\models\\research\\object_detection\\data\\oid_v4_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

PATH_TO_TEST_IMAGES_DIR = pathlib.Path('C:\\Users\\Bhavin\\Desktop\\objects')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS

model = load_model()

def run_inference_for_single_image(model, image):
  image = np.asarray(image)
  # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
  input_tensor = tf.convert_to_tensor(image)
  # The model expects a batch of images, so add an axis with `tf.newaxis`.
  input_tensor = input_tensor[tf.newaxis,...]

  # Run inference
  output_dict = model(input_tensor)

  # All outputs are batches tensors.
  # Convert to numpy arrays, and take index [0] to remove the batch dimension.
  # We're only interested in the first num_detections.
  num_detections = int(output_dict.pop('num_detections'))
  output_dict = {key:value[0, :num_detections].numpy() 
                 for key,value in output_dict.items()}
  output_dict['num_detections'] = num_detections

  # detection_classes should be ints.
  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'], output_dict['detection_boxes'],
               image.shape[0], image.shape[1])      
    detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                       tf.uint8)
    output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()

  return output_dict

def show_inference(model, 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 = np.array(Image.open(image_path))
  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks_reframed', None),
      use_normalized_coordinates=True,
      line_thickness=8)

  name = "Image" + str(i) + ".jpg"
  img = Image.fromarray(image_np)
  plt.imsave(name,image_np)
  my_results.append(output_dict['detection_classes']) # I added this
  print(my_results) # I added this
  #img.show()

i = 1
for image_path in TEST_IMAGE_PATHS:
 show_inference(model, image_path)
 i += 1

我检查了一些相关的堆栈溢出问题,答案与类别索引有关.但是使用的代码和示例与我正在遵循的教程有很大不同.

I checked some related stack overflow questions and the answer had something to do with category index. But the code and examples used are very different from the tutorial I am following.

行:my_results.append(output_dict['detection_classes'])

给我输出:[array([55], dtype=int64)]

如何提取检测到的对象的类?

How do I extract the classes of the detected objects?

推荐答案

第一个导入六个

def show_inference(model, image_path)之前添加get_classes_name_and_scores方法:

get_classes_name_and_scores 方法返回 {'name': 'person', 'score': '91%'}

get_classes_name_and_scores method returns {'name': 'person', 'score': '91%'}

def get_classes_name_and_scores(
        boxes,
        classes,
        scores,
        category_index,
        max_boxes_to_draw=20,
        min_score_thresh=.9): # returns bigger than 90% precision
    display_str = {}
    if not max_boxes_to_draw:
        max_boxes_to_draw = boxes.shape[0]
    for i in range(min(max_boxes_to_draw, boxes.shape[0])):
        if scores is None or scores[i] > min_score_thresh:
            if classes[i] in six.viewkeys(category_index):
                display_str['name'] = category_index[classes[i]]['name']
                display_str['score'] = '{}%'.format(int(100 * scores[i]))

    return display_str

然后在vis_util.visualize_boxes_and_labels_on_image_array

print(get_classes_name_and_scores(
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index))

这篇关于在图像上执行对象检测后如何打印检测到的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆