首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将注记从Mask-RCNN数据集格式转换为COCO格式

将注记从Mask-RCNN数据集格式转换为COCO格式
EN

Stack Overflow用户
提问于 2020-04-14 22:43:27
回答 3查看 3.6K关注 0票数 5

我想训练一个在图像中检测车辆和道路的模型。为此,我将使用Mask R-CNN和YOLACT++。我用vgg图像注释器为Mask R-CNN标记了一些图像,分割点如下图所示。

如您所见,这里没有area参数或bbox参数。我可以使用minx miny maxx maxy找到我的实例的bbox,但我找不到如何找到该分割区域的区域。您可以在下图中看到Yolact注释的形成。

标记所有实例需要大量时间。我花了至少10分钟来标记图像中的所有汽车,我已经有500张图像被标记了。你有什么建议或想法可以帮助我节省我的时间,同时转换第一个注释形成为第二个(掩码r-cnn到coco(yolact))?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-09-27 20:43:21

类似这样的东西,但这取决于您在vgg中的注释方式。

代码语言:javascript
复制
def vgg_to_coco(vgg_path: str, outfile: str=None, class_keyword: str = "Class"):
    with open(vgg_path) as f:
        vgg = json.load(f)

    images_ids_dict = {v["filename"]: i for i, v in enumerate(vgg.values())}
    # TDOD fix
    images_info = [{"file_name": k, "id": v, "width": 1024, "height": 1024} for k, v in images_ids_dict.items()]

    classes = {class_keyword} | {r["region_attributes"][class_keyword] for v in vgg.values() for r in v["regions"]
                                 if class_keyword in r["region_attributes"]}
    category_ids_dict = {c: i for i, c in enumerate(classes, 1)}
    categories = [{"supercategory": class_keyword, "id": v, "name": k} for k, v in category_ids_dict.items()]
    annotations = []
    suffix_zeros = math.ceil(math.log10(len(vgg)))
    for i, v in enumerate(vgg.values()):
        for j, r in enumerate(v["regions"]):
            if class_keyword in r["region_attributes"]:
                x, y = r["shape_attributes"]["all_points_x"], r["shape_attributes"]["all_points_y"]
                annotations.append({
                    "segmentation": [list(chain.from_iterable(zip(x, y)))],
                    "area": helper.polygon_area(x, y),
                    "bbox": helper.bbox(x, y, out_format="width_height"),
                    "image_id": images_ids_dict[v["filename"]],
                    "category_id": category_ids_dict[r["region_attributes"][class_keyword]],
                    "id": int(f"{i:0>{suffix_zeros}}{j:0>{suffix_zeros}}"),
                    "iscrowd": 0
                })

    coco = {
        "images": images_info,
        "categories": categories,
        "annotations": annotations
    }
    if outfile is None:
        outfile = vgg_path.replace(".json", "_coco.json")
    with open(outfile, "w") as f:
        json.dump(coco, f)

您必须将1024更改为您的图像大小,或者如果您有可变的图像大小,则必须为此创建一个映射。

票数 2
EN

Stack Overflow用户

发布于 2020-04-16 21:05:25

您必须创建自己的脚本并将其转换,我必须将其从xml注释转换为json maskrcnn。

您可以查看示例:https://github.com/adions025/XMLtoJson_遮罩_RCNN

票数 2
EN

Stack Overflow用户

发布于 2021-02-19 10:22:46

工作解决方案:扩展自@Zac Tod的答案

图像大小可以随时随地计算出来。

代码语言:javascript
复制
import skimage 
import math
from itertools import chain
import numpy as np

def vgg_to_coco(dataset_dir, vgg_path: str, outfile: str=None, class_keyword: str = "label"):
    with open(vgg_path) as f:
        vgg = json.load(f)

    images_ids_dict = {}
    images_info = []
    for i,v in enumerate(vgg.values()):

        images_ids_dict[v["filename"]] = I
        image_path = os.path.join(dataset_dir, v['filename'])
        image = skimage.io.imread(image_path)
        height, width = image.shape[:2]  
        images_info.append({"file_name": v["filename"], "id": i, "width": width, "height": height})

    classes = {class_keyword} | {r["region_attributes"][class_keyword] for v in vgg.values() for r in v["regions"].values()
                             if class_keyword in r["region_attributes"]}
    category_ids_dict = {c: i for i, c in enumerate(classes, 1)}
    categories = [{"supercategory": class_keyword, "id": v, "name": k} for k, v in category_ids_dict.items()]
    annotations = []
    suffix_zeros = math.ceil(math.log10(len(vgg)))
    for i, v in enumerate(vgg.values()):
        for j, r in enumerate(v["regions"].values()):
            if class_keyword in r["region_attributes"]:
                x, y = r["shape_attributes"]["all_points_x"], r["shape_attributes"]["all_points_y"]
                annotations.append({
                    "segmentation": [list(chain.from_iterable(zip(x, y)))],
                    "area": PolyArea(x, y),
                    "bbox": [min(x), min(y), max(x)-min(x), max(y)-min(y)],
                    "image_id": images_ids_dict[v["filename"]],
                    "category_id": category_ids_dict[r["region_attributes"][class_keyword]],
                    "id": int(f"{i:0>{suffix_zeros}}{j:0>{suffix_zeros}}"),
                    "iscrowd": 0
                    })

    coco = {
        "images": images_info,
        "categories": categories,
        "annotations": annotations
        }
    if outfile is None:
        outfile = vgg_path.replace(".json", "_coco.json")
    with open(outfile, "w") as f:
        json.dump(coco, f)

我的数据被标注为makesense.ai和区域class_keyword="label"属性如下所示在函数调用中。

代码语言:javascript
复制
"region_attributes": {
      "label": "box"
    }

为了计算多边形面积,代码复制自这个答案

代码语言:javascript
复制
def PolyArea(x,y):
    return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61210420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档