【问题标题】:Printing class name only once if same object is detected in Tensorflow Object Detection API如果在 TensorFlow 对象检测 API 中检测到相同的对象,则仅打印一次类名
【发布时间】:2020-01-22 02:59:13
【问题描述】:

我使用以下代码打印类名,但如果类名已经打印,我只需要一次类名。
例如,如果检测到一个人,则循环打印类名person
仅当之前未检测到类名时,我才需要打印类名。

with detection_graph.as_default():
  with tf.compat.v1.Session(graph=detection_graph) as sess:
    while True:
        ret,image_np=cap.read()
      # 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})
        a=[category_index.get(i) for i in classes[0]]
        x=a[0]
        y=x['name']
        print(y)
        # 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)
        cv2.imshow('object detection',cv2.resize(image_np,(800,600)))
        if cv2.waitKey(25) & 0xFF == ord('q'):
          cv2.destroyAllWindows()
          break

【问题讨论】:

    标签: python-3.x tensorflow tensorflow2.0


    【解决方案1】:

    您可以通过在 while 循环之前创建一个列表对象并根据条件附加类名来做到这一点。

    我已经修改了你的代码,条件是只打印一次类名。

    with detection_graph.as_default():
      with tf.compat.v1.Session(graph=detection_graph) as sess:
        present = []
        while True:
            ret,image_np=cap.read()
          # 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})
            a=[category_index.get(i) for i in classes[0]]
            x=a[0]
            y=x['name']
            if y in present:
              pass
            else:
              present.append(y)
              print(y)
            # 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)
            cv2.imshow('object detection',cv2.resize(image_np,(800,600)))
            if cv2.waitKey(25) & 0xFF == ord('q'):
              cv2.destroyAllWindows()
              break  
    

    我希望这能回答您的问题。快乐学习!

    【讨论】:

    • @Prashast Yadav - 如果您认为我已经回答了您的问题,请接受并投票,谢谢。
    猜你喜欢
    • 2021-06-30
    • 2023-01-07
    • 2021-12-05
    • 1970-01-01
    • 2018-05-17
    • 2019-03-20
    • 2018-01-17
    • 2022-10-08
    • 1970-01-01
    相关资源
    最近更新 更多