keras 使用MaskRcnn执行model.detect()后,从图像中获取预测遮罩的x,y坐标

3duebb1j  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(148)

如何在检测后使用Mask Rcnn从预测的掩膜中提取多边形的x,y坐标。
results = model.detect([image], verbose=1)
我可以用下面的代码得到ROI、masks、class_id、类名和置信度分数,但是我需要得到每个预测的masks多边形的x,y坐标值。
ax = get_ax(1) r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], ax=ax)

t9aqgxwy

t9aqgxwy1#

我在www.example.com文件中的display_instances()函数中发现了编写的代码visualize.py。我对此代码进行了修改,并创建了单独的函数,如下所示:

from skimage.measure import find_contours
import numpy as np

def save_co_ordinates(image, boxes, masks, class_ids, class_names):
    image = image.split("/")[-1]
    image_data = []

    for i in range(boxes.shape[0]):

        class_id = class_ids[i]
        label = class_names[class_id]

        mask = masks[:, :, i]
        padded_mask = np.zeros(
            (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
        padded_mask[1:-1, 1:-1] = mask
        contours = find_contours(padded_mask, 0.5)
        for verts in contours:
            verts = np.fliplr(verts) - 1
            list_co_ordinates = np.moveaxis(verts, 1, 0).tolist()

            region = {"shape_attributes": {"all_points_x": list_co_ordinates[0],
                                           "all_points_y": list_co_ordinates[1]},
                      "region_attributes": {"name": {label: True}}}
            image_data.append(region)
    data = {"filename": image, "regions": image_data}
    return data

save_co_ordinates("C:/path/apple.jpg", r1['rois'], r1['masks'], r1['class_ids'], get_detections())

相关问题