python-3.x - 如何在 tensorflow 对象检测 API 中获取多个边界框坐标
问题描述
我想获取多个边界框坐标和每个边界框的类并将其作为 JSON 文件返回。
当我从以下代码中打印 box[] 时,它的形状为 (1,300,4)。box[] 中有 300 个坐标。但是我的预测图像上只有 2 个。我想要在我的图像上预测的边界框的坐标。
此外,我们如何知道哪个边界框映射到图像中的哪个类别/类?
例如,假设我在图像中有一只狗和一个人,我怎么知道哪个边界框对应于狗类,哪个边界框对应于人类?box[] 为我们提供了一个形状为 (1,300,4) 的数组,没有任何指示哪个边界框对应于图像中的哪个类。
我按照这个答案使用阈值分数从 box[] 中的 300 个坐标中获取边界框坐标。
我试过得到得分最高的边界框。但即使预测图像有多个边界框,它也只返回一个边界框。
得分最高的边界框坐标甚至与预测图像上的边界框坐标不匹配。如何获得预测图像上的边界框坐标?
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
im = Image.fromarray(image_np)
true_boxes = boxes[0][scores[0]==scores.max()] # Gives us the box with max score
for i in range(true_boxes.shape[0]): # rescaling the coordinates
ymin = true_boxes[i,0]*height
xmin = true_boxes[i,1]*width
ymax = true_boxes[i,2]*height
xmax = true_boxes[i,3]*width
我从上面的代码 xmin,ymin,xmax,ymax (具有最高分数)得到的坐标与预测图像上的边界框坐标不完全匹配。它们偏离了几个像素。此外,即使预测图像具有多个边界框和多个类别(例如:狗和人),我也只能得到一个边界框。
我想返回一个 JSON 文件,其中包含与每个边界框对应的 image_name、bounding_boxes 和类。
谢谢,我是新手。请询问您是否不理解问题的任何部分。
解决方案
我按照这个答案在这里链接,我找到了我所有的边界框坐标:
min_score_thresh=0.60
true_boxes = boxes[0][scores[0] > min_score_thresh]
for i in range(true_boxes.shape[0]):
ymin = int(true_boxes[i,0]*height)
xmin = int(true_boxes[i,1]*width)
ymax = int(true_boxes[i,2]*height)
xmax = int(true_boxes[i,3]*width)
roi = image[ymin:ymax,xmin:xmax].copy()
cv2.imwrite("box_{}.jpg".format(str(i)), roi)
推荐阅读
- javascript - 通过 jQuery 中的索引查找 HTML 的各个元素并为其设置样式
- android - 从通知意图启动活动时重新创建 Android ViewModel
- php - Laravel 多对多同步与附加列
- docker - Jenkins docker插件问题>>标签“代理”的所有节点都离线
- open-liberty - Open Liberty 支持网络部署?
- c++ - 构建对象之间的依赖关系图
- angular - 在角度打字稿中从控制台访问类方法
- javascript - 状态改变后 React Component 不渲染
- php - 如何在将数组转换为 jsonobject php 时从 json 对象中删除数组索引?
- python-3.x - 如何在 Django 中提交 302 表单后发送 POST 请求?