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()
我正在尝试获取 API 使用 vis_util.visualize_boxes_and_labels_on_image_array()
绘制的框的 x1, y1, x2, y2
坐标
我已经尝试查看 detection_boxes
,但我得到了一堆我不知道它们是什么意思的值。
有人可以给我一个解决方案吗?谢谢
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,我要找的class是3。匹配的那些框存储在数组调用框中。
这个问题似乎与你的相似:
How to find bounding boxes coordinates in Tensorflow Object Detection API
并且有人发布了一个简单的代码解决方案。
还有另一种方法,您可以操纵 visualize_boxes_and_labels_on_image_array() 函数来 return 坐标
类似于:
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
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
,但我得到了一堆我不知道它们是什么意思的值。
有人可以给我一个解决方案吗?谢谢
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,我要找的class是3。匹配的那些框存储在数组调用框中。
这个问题似乎与你的相似: How to find bounding boxes coordinates in Tensorflow Object Detection API
并且有人发布了一个简单的代码解决方案。
还有另一种方法,您可以操纵 visualize_boxes_and_labels_on_image_array() 函数来 return 坐标 类似于:
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