Подтвердить что ты не робот

API обнаружения объектов Tensorflow: обнаружение класса с печатью как вывод на терминал

У меня простой вопрос, но я не могу понять, как это сделать. Я использую API обнаружения объектов TF для обнаружения изображений, он работает нормально, и с учетом изображения он нарисует ограничивающую рамку с меткой и доверительной оценкой того, какой класс он считает обнаруженным. Мой вопрос заключается в том, как я могу распечатать обнаруженный класс (в виде строки) и счет на терминале, т.е. не только на изображении, но и как вывод на терминал тоже.

Ниже приведен код, отвечающий за обнаружение изображения

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    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)
      image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
      # Each box represents a part of the image where a particular object was detected.
      boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
      # Each score represent how level of confidence for each of the objects.
      # Score is shown on the result image, together with the class label.
      scores = detection_graph.get_tensor_by_name('detection_scores:0')
      classes = detection_graph.get_tensor_by_name('detection_classes:0')
      num_detections = detection_graph.get_tensor_by_name('num_detections:0')
      # Actual detection.
      (boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      # Visualization of the results of a detection.
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=8, min_score_thresh=.2)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)
      plt.show()

Спасибо заранее, первое сообщение о переполнении стека, поэтому, пожалуйста, будьте осторожны со мной

4b9b3361

Ответ 1

Ну, это очень просто. classes зашифрованы в category_index, который является dict, поэтому вы можете сделать что-то вроде этого:

with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
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)
  image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  # Each box represents a part of the image where a particular object was detected.
  boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  # Each score represent how level of confidence for each of the objects.
  # Score is shown on the result image, together with the class label.
  scores = detection_graph.get_tensor_by_name('detection_scores:0')
  classes = detection_graph.get_tensor_by_name('detection_classes:0')
  num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  # Actual detection.
  (boxes, scores, classes, num_detections) = sess.run(
      [boxes, scores, classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})

  # Here output the category as string and score to terminal
  print([category_index.get(i) for i in classes[0]])
  print(scores)

Ответ 2

Просто перейдите в каталог utils в папке object_detection и откройте script visualization_utils.py. Вы найдете функцию, а именно visualize_boxes_and_labels_on_image_array, добавьте команду печати в конце функции, чтобы напечатать переменную имя_классы (print (class_name)). Теперь запустите свой код и увидите волшебство.

Ответ 3

Dat и Omar.. У меня есть основной вопрос. Когда мы печатаем массив, он содержит массив из 100 лучших баллов и классов. Из этого только 2-3 фактически отображаются на выходном изображении (с ограниченным коробки и точность). Как я могу подмножать только те значения, которые фактически отображаются на выходном изображении? Возможно ли, или нам нужно установить фиксированный порог точности? (и риск потери некоторых объектов, которые отображаются на выходном изображении).

Ответ 4

Ниже приведен код для решения вашей проблемы. TF Version 1.12.0 Я использовал веб-камеру для тестирования.

От.. \models\research\object_detection\utils\visualization_utils.py перейдите к def visualize_boxes_and_labels_on_image_array и исправьте цикл for.

Распечатайте display_str после того, как display_str будет определен (строка 21, я полагаю), если вы напечатаете в конце цикла for, вы получите ошибку class_name, на которую ссылаются перед присваиванием. Когда когда-либо объект не был обнаружен через канал камеры, я получал эту ошибку, если добавлял оператор печати внизу, как предложил Равиш.

  for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    if scores is None or scores[i] > min_score_thresh:
      box = tuple(boxes[i].tolist())
      if instance_masks is not None:
        box_to_instance_masks_map[box] = instance_masks[i]
      if instance_boundaries is not None:
        box_to_instance_boundaries_map[box] = instance_boundaries[i]
      if keypoints is not None:
        box_to_keypoints_map[box].extend(keypoints[i])
      if scores is None:
        box_to_color_map[box] = groundtruth_box_visualization_color
      else:
        display_str = ''
        if not skip_labels:
          if not agnostic_mode:
            if classes[i] in category_index.keys():
              class_name = category_index[classes[i]]['name']
            else:
              class_name = 'N/A'
            display_str = str(class_name)
            print(display_str)
        if not skip_scores:
          if not display_str:
            display_str = '{}%'.format(int(100*scores[i]))
          else:
            display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
        box_to_display_str_map[box].append(display_str)
        if agnostic_mode:
          box_to_color_map[box] = 'DarkOrange'
        else:
          box_to_color_map[box] = STANDARD_COLORS[
              classes[i] % len(STANDARD_COLORS)]
    #(print(class_name)) -- doesn't work : error, class name referenced before assignment

Ответ 5

Сначала я тоже растерялся. Получил более 100 коробок, была ли нарисована только одна на моем изображении. Согласен со всеми ответами. Получил мое простое решение для копирования и вставки для вашего infrecne.py:

    #assume you've got this in your inference.py
    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'),
        use_normalized_coordinates=True,
        line_thickness=8)

    # This is the way I'm getting my coordinates
    boxes = output_dict['detection_boxes']
    max_boxes_to_draw = boxes.shape[0]
    scores = output_dict['detection_scores']
    min_score_thresh=.5
    for i in range(min(max_boxes_to_draw, boxes.shape[0])):
        if scores is None or scores[i] > min_score_thresh:
            # boxes[i] is the box which will be drawn
            print ("This box is gonna get used", boxes[i])

Ответ 6

@Dat Tran хорошо, я использовал эту печать ([category_index.get(i) для я в классах [0]]) печать (оценки), но она не печатает обнаруженные классы, а печатает все классы, которые я дал