python-3.x - 在 Python 中裁剪并仅从图像中选择检测到的区域
问题描述
我使用 TensorFlow Object Detection API 从图像中检测手。通过使用提供的示例代码(object_detection_tutorial.ipynb),我已经能够在图像上绘制边界框。有没有办法只选择检测到的区域(在边界框内)并将其作为图像获取?
例如,
样本输入图像
张量流输出
我想要的是
可以在此处找到对象检测 API 示例代码。https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
任何帮助将不胜感激!
解决方案
是的,在本教程中,该变量output_dict
可用于实现这一目标。注意传递给 function 的所有变量vis_util.visualize_boxes_and_labels_on_image_array
,它们包含框、分数等。
首先,您需要获取图像形状,因为框坐标是标准化形式。
img_height, img_width, img_channel = image_np.shape
然后将所有框坐标转换为绝对格式
absolute_coord = []
THRESHOLD = 0.7 # adjust your threshold here
N = len(output_dict['detection_boxes'])
for i in range(N):
if output_dict['score'][i] < THRESHOLD:
continue
box = output_dict['detection_boxes']
ymin, xmin, ymax, xmax = box
x_up = int(xmin*img_width)
y_up = int(ymin*img_height)
x_down = int(xmax*img_width)
y_down = int(ymax*img_height)
absolute_coord.append((x_up,y_up,x_down,y_down))
然后你可以使用 numpy slices 来获取边界框内的图像区域
bounding_box_img = []
for c in absolute_coord:
bounding_box_img.append(image_np[c[1]:c[3], c[0]:c[2],:])
然后只需将所有 numpy 数组保存bounding_box_img
为图像。保存时您可能需要更改形状,因为 img 的形状为 [img_height, img_width, img_channel]。如果您使用分数数组,您甚至可以过滤掉所有低置信度分数的检测。
PS:我可能搞砸了img_height
,img_width
但这些应该给你一个起点。
推荐阅读
- javascript - Nodemon 在作为 javascript 文件中的模块执行时启动 2 个节点实例
- javascript - JavaScript 获取/设置属性,两者都必须为继承的类定义
- node.js - 如何使 https.get 请求将请求内的数据分配给请求外的变量?
- python - Python Flask HTML 路由参数
- javascript - 如何在没有 jqueryUI 的情况下使 div 垂直可拖动
- python - 使用 tensorflow 插件图像转换时出现未定义符号错误
- haskell - 这个 Functor 组合器有名字吗?
- c - 如何将二维数组作为参数传递?
- python - 在numpy数组python中的每个元素之后添加元素
- groovy - 为什么不允许隐式调用内部类实例?