【问题标题】:Detect object in specific region of frame检测帧特定区域中的对象
【发布时间】:2017-09-16 09:33:32
【问题描述】:

我想使用 tensorflows 对象检测 api 从框架区域检测对象。我已将框架拆分为 region_1 和 region_2 但如何仅在框架中的 region_1 中执行检测并仅在 region1 中绘制矩形

def detect_objects(image_np, sess, detection_graph):

    region_1 = image_np[zone1[1]: zone1[3], zone1[0]:zone1[2]]
    region_2 = image_np[zone2[1]: zone2[3], zone2[0]:zone2[2]]

    image_np_expanded = np.expand_dims(image_np, axis=0)
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    scores = detection_graph.get_tensor_by_name('detection_scores:0')
    classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    (boxes, scores, classes, num_detections) = sess.run(
        [boxes, scores, classes, num_detections],
        feed_dict={image_tensor: image_np_expanded})

    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=1)
    return image_np

【问题讨论】:

  • 您能否通过分享一个指向 TensorFlow 的对象检测 API 的链接来提供一些背景信息?

标签: python opencv tensorflow


【解决方案1】:

你必须将你感兴趣的区域替换为 image_np 之前的位置,所以改变

image_np_expanded = np.expand_dims(image_np, axis=0)

到:

image_np_expanded = np.expand_dims(region_1, axis=0)

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=1)
return image_np

到:

vis_util.visualize_boxes_and_labels_on_image_array(
        region_1,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=1)
return region_1

如果您想返回仅在 region_1 一侧绘制框的原始图像,您必须将框坐标从 region_1 的尺寸转换回原始图像尺寸(考虑到 tf 使用的不同数据类型,这很难他们的 np.arrays) 或 concatenate 将这些区域重新组合在一起: 垂直堆叠(img1 over img2):

vis = np.concatenate((img1, img2), axis=0)

水平堆叠(img1在img2的左边):

vis = np.concatenate((img1, img2), axis=1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多