简体   繁体   English

Tensorflow 对象检测 API - 获取框的坐标

[英]Tensorflow Object Detection API - Get Coordinates of Boxes

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()

I'm trying to get the x1, y1, x2, y2 coordinates of the boxes that the API draws using vis_util.visualize_boxes_and_labels_on_image_array()我正在尝试使用vis_util.visualize_boxes_and_labels_on_image_array()获取 API 绘制的框的x1, y1, x2, y2坐标

I've tried looking into detection_boxes but i get a bunch of values which I have no idea what they mean.我试过查看detection_boxes但我得到了一堆我不知道它们是什么意思的值。

Could someone provide me a solution please?有人可以为我提供解决方案吗? Thanks谢谢

The numbers in detection_boxes are [ymin, xmin, ymax, xmax] and they are normalised to the size of your image since "use_normalized_coordinates=True" in your script. detection_boxes 中的数字是 [ymin, xmin, ymax, xmax] 并且由于脚本中的“use_normalized_coordinates=True”,它们被标准化为图像的大小。 Each index in the detection_boxes correspond to the same index in the detection_scores and Detection_classes. detection_boxes 中的每个索引对应于 detection_scores 和 Detection_classes 中的相同索引。 So you have to find what is the object you want at what threshold score in order to get the index for the detection_box.因此,您必须在什么阈值分数下找到您想要的对象,才能获得检测框的索引。 Example:例子:

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

The score threshold set here is 0.9 and the class i am looking for is 3. Those box that match are stored in an array call boxes.此处设置的分数阈值是 0.9,我要查找的类是 3。那些匹配的框存储在数组调用框中。

This question seems similar to yours: How to find bounding boxes coordinates in Tensorflow Object Detection API这个问题似乎与您的相似: 如何在 Tensorflow 对象检测 API 中找到边界框坐标

And someone has posted a simple code solution.有人发布了一个简单的代码解决方案。

There is a another way, where you can manipulate the visualize_boxes_and_labels_on_image_array() function to return the coordinates Something like:还有另一种方法,您可以在其中操作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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM