【问题标题】:TensorFlow 2 print if object is detected如果检测到对象,则 TensorFlow 2 打印
【发布时间】:2023-01-07 12:56:37
【问题描述】:

我正在尝试打印“test”,同时检测到一个 leable id 为 1 的对象。单词 test 不停地打印 :(

if 1 in detections['detection_classes']:
        print("test")

cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

while cap.isOpened(): 
    ret, frame = cap.read()
    image_np = np.array(frame)
    
    input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
    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

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

    label_id_offset = 1
    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']+label_id_offset,
                detections['detection_scores'],
                category_index,
                use_normalized_coordinates=True,
                max_boxes_to_draw=5,
                min_score_thresh=.8,
                agnostic_mode=False)

    cv2.imshow('object detection',  cv2.resize(image_np_with_detections, (800, 600)))
    
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

我有兴趣打印在屏幕上实际检测到的名称 ID。

【问题讨论】:

    标签: tensorflow object-detection


    【解决方案1】:
    detections = ['output1', 'output2'] #This will be your outputs
    
    for i in detections: #to iterate through items in list
        if i == detections[1]: #if item in iterations matches label id
            print("test") #print test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 2021-06-30
      • 1970-01-01
      • 2020-11-23
      • 2021-02-01
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多