【问题标题】:Tensorflow Object Detection API - Get Coordinates of BoxesTensorflow 对象检测 API - 获取框的坐标
【发布时间】:2020-06-23 00:58:55
【问题描述】:
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(MODEL_PATH, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

    sess = tf.Session(graph=detection_graph)

image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

gameWindow = [0, 0, 200, 300]

while True:
    image = np.array(ImageGrab.grab(bbox=(gameWindow[0], gameWindow[1], gameWindow[2], gameWindow[3])))
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, axis=0)

    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_expanded})

    vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.60)

    frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # print("Made it ")
    cv2.imshow('Detect the dumb trees', frame)

    if cv2.waitKey(1) == 27:
        break
cv2.destroyAllWindows()

我正在尝试获取 API 使用 vis_util.visualize_boxes_and_labels_on_image_array() 绘制的框的 x1, y1, x2, y2 坐标

我试过调查detection_boxes,但我得到了一堆值,我不知道它们是什么意思。

有人可以给我一个解决方案吗?谢谢

【问题讨论】:

    标签: python tensorflow object-detection


    【解决方案1】:

    detection_boxes 中的数字是 [ymin, xmin, ymax, xmax],并且由于脚本中的“use_normalized_coordinates=True”,它们被归一化为图像的大小。 detection_boxes 中的每个索引对应于 detection_scores 和 Detection_classes 中的相同索引。因此,您必须在阈值分数处找到您想要的对象,以便获得 detection_box 的索引。 示例:

    boxes=[]
        for i in range(len(detection_boxes)):
            if detection_classes[i]=3 and detection_scores[i]>0.9:
                 boxes.append(detection_boxes[i])
    

    这里设置的分数阈值为 0.9,我要查找的类是 3。匹配的那些框存储在数组调用框中。

    【讨论】:

      【解决方案2】:

      这个问题似乎和你的类似: How to find bounding boxes coordinates in Tensorflow Object Detection API

      并且有人发布了一个简单的代码解决方案。

      还有另一种方法,您可以在其中操作 Visualize_boxes_and_labels_on_image_array() 函数以返回坐标 比如:

      coordinates_list = []
      for box, color in box_to_color_map.items():
        ymin, xmin, ymax, xmax = box
        height, width, channels = image.shape
        ymin = int(ymin*height)
        ymax = int(ymax*height)
        xmin = int(xmin*width)
        xmax = int(xmax*width)
        coordinates_list.append([xmin, ymin, xmax, ymax])
      
      return coordinates_list
      

      【讨论】:

        猜你喜欢
        • 2018-08-01
        • 1970-01-01
        • 2019-02-24
        • 2019-10-11
        • 2019-09-30
        • 2020-07-25
        • 2019-09-18
        • 2021-06-27
        • 2018-04-17
        相关资源
        最近更新 更多