【发布时间】:2021-07-18 00:18:23
【问题描述】:
我正在尝试使用 openCV 手动绘制边界框。边界框坐标是使用 tensorflow 对象检测 API 收集的。
当我得到坐标时,它们被归一化,所以我将它们转换然后将它们添加到图像中,如下所示:
boxes = np.squeeze(detections['detection_boxes'])
scores = np.squeeze(detections['detection_scores'])
min_score_thresh = 0.7
bboxes = boxes[scores > min_score_thresh]
im_width, im_height,_ = image.shape
for box in bboxes:
ymin, xmin, ymax, xmax = box
ymin, xmin, ymax, xmax = int(xmin * im_width), int(xmax * im_width), int(ymin * im_height), int(ymax * im_height)
cv2.rectangle(image_np_with_detections, (xmin,ymin),(xmax,ymax),(0,0,255),5)
cv2.putText(image_np_with_detections,"TEST",(int(xmin),int(ymin)-5),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,0,255), 1)
然后我将这个矩形与 Tensorflow API 生成的矩形进行比较,如下所示:
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=.7,
agnostic_mode=False)
#Show the image
cv2.imshow('object detection', image_np_with_detections)
但如下图所示,手动绘制的边界框已关闭。这可能是什么原因?
感谢您的帮助!
编辑:
将im_width,im_height 交换为im_height,im_width 后,我现在得到:
【问题讨论】:
-
尝试将
im_width, im_height,_ = image.shape切换为height, width, _ = ... -
@NicolasGervais 感谢您的回复!我似乎帮助很大。但由于某种原因,它把它画得比它应该的大(新图片已附加在帖子中)。
-
在
ymin, xmin, ymax, xmax = int(xmin * im_width), int(xmax * im_width), int(ymin * im_height), int(ymax * im_height)中左右两边不匹配。例如你有ymin = int(xmin * im_width)的等价物。 -
@DanMašek 当然!我怎么看不到?!现在可以完美运行,边界框彼此一致。非常感谢你们!
标签: tensorflow opencv object-detection