从 PNG 掩码生成 JSON 注释文件

发布于 2025-01-18 19:48:26 字数 999 浏览 3 评论 0 原文

为我提供了一系列图像对,以帮助进行图像分割项目。一个图像是原始的RGB照片,另一个是一个png文件,它掩盖了原始图像的部分。请参阅下面的示例。

“

我的图像分割培训脚本要求我为每个RGB文件有一个注释的JSON文件,显示了蒙版的bale和坐标。例如:

"label": "window",
      "points": [
        [
          48.30337078651685,
          507.9887640449438
        ],
        [
          73.02247191011236,
          510.7977528089888
        ],
        [
          71.33707865168539,
          419.7865168539326
        ],
        [
          44.93258426966292,
          423.1573033707865
        ]
      ]

我有1000辆RGB和Mask PNG文件用于改进我的模型培训,但目前我不知道如何将PNG标签转换为JSON多边形。

有可以提供帮助的工具吗?

另外,我是否可以使用一种培训方法来使用这些文件?

I have been supplied with a series of image pairs to help with an image segmentation project. One image is the original RGB photo, the other is a PNG file which has masked parts of the original image. See example below.

RGB of House

Alpha of house

My image segmentation training script requires I have an annotated JSON file for each RGB file, showing the bale and coordinates of the masked. For example:

"label": "window",
      "points": [
        [
          48.30337078651685,
          507.9887640449438
        ],
        [
          73.02247191011236,
          510.7977528089888
        ],
        [
          71.33707865168539,
          419.7865168539326
        ],
        [
          44.93258426966292,
          423.1573033707865
        ]
      ]

I have 1000's of RGB and mask PNG files to use to improve my model training, but I currently don't know how to convert the PNG labels into JSON polygons.

Is there a tool out there that can help?

Alternatively, is there a method of training I can use to use these files as they are?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2025-01-25 19:48:26

也许类似的东西

import cv2

def get_segmentation_annotations(segmentation_mask, DEBUG=True):
    hw = segmentation_mask.shape[:2]
    segmentation_mask = segmentation_mask.reshape(hw)
    polygons = []

    for segtype in SegmentationClass.values():
        seg_type_name = proto_api.get_segmentation_type_name(segtype)
        if segtype == SegmentationClass.BACKGROUND:
            continue
        temp_img = np.zeros(hw)
        seg_class_mask_over_seg_img = np.where(segmentation_mask==segtype)
        if np.any(seg_class_mask_over_seg_img):
            temp_img[seg_class_mask_over_seg_img] = 1
            contours, _ = cv2.findContours(temp_img.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
            if len(contours) < 1:
                continue
            has_relevant_contour = False
            for contour in contours:
                if cv2.contourArea(contour) < 2:
                    continue
                has_relevant_contour = True
                polygons.append((contour, seg_type_name))
            if DEBUG and has_relevant_contour:
                fig = plt.figure()
                fig.suptitle(seg_type_name)
                plt.imshow(temp_img)
    return polygons

def get_segmentation_dict(segmentation_mask, img_id="0", starting_annotation_indx=0, DEBUG=False):
    annotations = []
    for indx, (contour, seg_type) in enumerate(get_segmentation_annotations(segmentation_mask, DEBUG=DEBUG)):
        segmentation = contour.ravel().tolist()
        annotations.append({
            "segmentation": segmentation,
            "area": cv2.contourArea(contour),
            "image_id": img_id,
            "category_id": seg_type,
            "id": starting_annotation_indx + indx
        })
    return annotations

Maybe something similar to this

import cv2

def get_segmentation_annotations(segmentation_mask, DEBUG=True):
    hw = segmentation_mask.shape[:2]
    segmentation_mask = segmentation_mask.reshape(hw)
    polygons = []

    for segtype in SegmentationClass.values():
        seg_type_name = proto_api.get_segmentation_type_name(segtype)
        if segtype == SegmentationClass.BACKGROUND:
            continue
        temp_img = np.zeros(hw)
        seg_class_mask_over_seg_img = np.where(segmentation_mask==segtype)
        if np.any(seg_class_mask_over_seg_img):
            temp_img[seg_class_mask_over_seg_img] = 1
            contours, _ = cv2.findContours(temp_img.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
            if len(contours) < 1:
                continue
            has_relevant_contour = False
            for contour in contours:
                if cv2.contourArea(contour) < 2:
                    continue
                has_relevant_contour = True
                polygons.append((contour, seg_type_name))
            if DEBUG and has_relevant_contour:
                fig = plt.figure()
                fig.suptitle(seg_type_name)
                plt.imshow(temp_img)
    return polygons

def get_segmentation_dict(segmentation_mask, img_id="0", starting_annotation_indx=0, DEBUG=False):
    annotations = []
    for indx, (contour, seg_type) in enumerate(get_segmentation_annotations(segmentation_mask, DEBUG=DEBUG)):
        segmentation = contour.ravel().tolist()
        annotations.append({
            "segmentation": segmentation,
            "area": cv2.contourArea(contour),
            "image_id": img_id,
            "category_id": seg_type,
            "id": starting_annotation_indx + indx
        })
    return annotations
ぃ弥猫深巷。 2025-01-25 19:48:26

不是答案,而是指针。我的一部分学习是在 htttps ://www.highvoltagecode.com/post/edge-ege-ai-semantic-semantic-semantic-on-nvidia-jetson 如果您已经在这个可能的png png plng上,则最终会使用JPG和匹配的png掩码训练该模型一条路。
祝你好运
JC

Not an answer but maybe a pointer. A part of my learning I was using the instructions at https://www.highvoltagecode.com/post/edge-ai-semantic-segmentation-on-nvidia-jetson This ends up training the model using a jpg and matching png mask if you already have the png this may off a path.
Good Luck
JC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文