当前位置: 首页 > article >正文

Labelme转Voc、Coco

Q:在github找的cv代码基本都是根据现有且流行的公共数据集格式组织的训练数据集,这导致我使用labelme标注好之后需要我们重新组织数据集

labelme2coco

#!/usr/bin/env python
 
import argparse
import collections
import datetime
import glob
import json
import os
import os.path as osp
import sys
import uuid
 
import imgviz
import numpy as np
 
import labelme
 
try:
    import pycocotools.mask
except ImportError:
    print("Please install pycocotools:\n\n    pip install pycocotools\n")
    sys.exit(1)
 
 
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("input_dir",default=r"data_annotated", help="input annotated directory")
    parser.add_argument("output_dir",default=r"data_coco", help="output dataset directory")
    parser.add_argument("--labels",default=r"labels.txt", help="labels file", required=True)
    parser.add_argument(
        "--noviz", help="no visualization", action="store_true"
    )
    args = parser.parse_args()
 
    if osp.exists(args.output_dir):
        print("Output directory already exists:", args.output_dir)
        sys.exit(1)
    os.makedirs(args.output_dir)
    os.makedirs(osp.join(args.output_dir, "JPEGImages"))
    if not args.noviz:
        os.makedirs(osp.join(args.output_dir, "Visualization"))
    print("Creating dataset:", args.output_dir)
 
    now = datetime.datetime.now()
 
    data = dict(
        info=dict(
            description=None,
            url=None,
            version=None,
            year=now.year,
            contributor=None,
            date_created=now.strftime("%Y-%m-%d %H:%M:%S.%f"),
        ),
        licenses=[dict(url=None, id=0, name=None,)],
        images=[
            # license, url, file_name, height, width, date_captured, id
        ],
        type="instances",
        annotations=[
            # segmentation, area, iscrowd, image_id, bbox, category_id, id
        ],
        categories=[
            # supercategory, id, name
        ],
    )
 
    class_name_to_id = {}
    for i, line in enumerate(open(args.labels).readlines()):
        class_id = i - 1  # starts with -1
        class_name = line.strip()
        if class_id == -1:
            assert class_name == "__ignore__"
            continue
        class_name_to_id[class_name] = class_id
        data["categories"].append(
            dict(supercategory=None, id=class_id, name=class_name,)
        )
 
    out_ann_file = osp.join(args.output_dir, "annotations.json")
    label_files = glob.glob(osp.join(args.input_dir, "*.json"))
    for image_id, filename in enumerate(label_files):
        print("Generating dataset from:", filename)
 
        label_file = labelme.LabelFile(filename=filename)
 
        base = osp.splitext(osp.basename(filename))[0]
        out_img_file = osp.join(args.output_dir, "JPEGImages", base + ".jpg")
 
        img = labelme.utils.img_data_to_arr(label_file.imageData)
        imgviz.io.imsave(out_img_file, img)
        data["images"].append(
            dict(
                license=0,
                url=None,
                file_name=osp.relpath(out_img_file, osp.dirname(out_ann_file)),
                height=img.shape[0],
                width=img.shape[1],
                date_captured=None,
                id=image_id,
            )
        )
 
        masks = {}  # for area
        segmentations = collections.defaultdict(list)  # for segmentation
        for shape in label_file.shapes:
            points = shape["points"]
            label = shape["label"]
            group_id = shape.get("group_id")
            shape_type = shape.get("shape_type", "polygon")
            mask = labelme.utils.shape_to_mask(
                img.shape[:2], points, shape_type
            )
 
            if group_id is None:
                group_id = uuid.uuid1()
 
            instance = (label, group_id)
 
            if instance in masks:
                masks[instance] = masks[instance] | mask
            else:
                masks[instance] = mask
 
            if shape_type == "rectangle":
                (x1, y1), (x2, y2) = points
                x1, x2 = sorted([x1, x2])
                y1, y2 = sorted([y1, y2])
                points = [x1, y1, x2, y1, x2, y2, x1, y2]
            if shape_type == "circle":
                (x1, y1), (x2, y2) = points
                r = np.linalg.norm([x2 - x1, y2 - y1])
                # r(1-cos(a/2))<x, a=2*pi/N => N>pi/arccos(1-x/r)
                # x: tolerance of the gap between the arc and the line segment
                n_points_circle = max(int(np.pi / np.arccos(1 - 1 / r)), 12)
                i = np.arange(n_points_circle)
                x = x1 + r * np.sin(2 * np.pi / n_points_circle * i)
                y = y1 + r * np.cos(2 * np.pi / n_points_circle * i)
                points = np.stack((x, y), axis=1).flatten().tolist()
            else:
                points = np.asarray(points).flatten().tolist()
 
            segmentations[instance].append(points)
        segmentations = dict(segmentations)
 
        for instance, mask in masks.items():
            cls_name, group_id = instance
            if cls_name not in class_name_to_id:
                continue
            cls_id = class_name_to_id[cls_name]
 
            mask = np.asfortranarray(mask.astype(np.uint8))
            mask = pycocotools.mask.encode(mask)
            area = float(pycocotools.mask.area(mask))
            bbox = pycocotools.mask.toBbox(mask).flatten().tolist()
 
            data["annotations"].append(
                dict(
                    id=len(data["annotations"]),
                    image_id=image_id,
                    category_id=cls_id,
                    segmentation=segmentations[instance],
                    area=area,
                    bbox=bbox,
                    iscrowd=0,
                )
            )
 
        if not args.noviz:
            viz = img
            if masks:
                labels, captions, masks = zip(
                    *[
                        (class_name_to_id[cnm], cnm, msk)
                        for (cnm, gid), msk in masks.items()
                        if cnm in class_name_to_id
                    ]
                )
                viz = imgviz.instances2rgb(
                    image=img,
                    labels=labels,
                    masks=masks,
                    captions=captions,
                    font_size=15,
                    line_width=2,
                )
            out_viz_file = osp.join(
                args.output_dir, "Visualization", base + ".jpg"
            )
            imgviz.io.imsave(out_viz_file, viz)
 
    with open(out_ann_file, "w") as f:
        json.dump(data, f)
 
 
if __name__ == "__main__":
    main()

labelme2voc

#!/usr/bin/env python
 
from __future__ import print_function
 
import argparse
import glob
import os
import os.path as osp
import sys
 
import imgviz
import numpy as np
 
import labelme
 
 
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("input_dir", help="input annotated directory")
    parser.add_argument("output_dir",help="output dataset directory")
    parser.add_argument("--labels", help="labels file", required=True)
    parser.add_argument(
        "--noviz", help="no visualization", action="store_true"
    )
    args = parser.parse_args()
 
    if osp.exists(args.output_dir):
        print("Output directory already exists:", args.output_dir)
        sys.exit(1)
    os.makedirs(args.output_dir)
    os.makedirs(osp.join(args.output_dir, "JPEGImages"))
    os.makedirs(osp.join(args.output_dir, "SegmentationClass"))
    os.makedirs(osp.join(args.output_dir, "SegmentationClassPNG"))
    if not args.noviz:
        os.makedirs(
            osp.join(args.output_dir, "SegmentationClassVisualization")
        )
    os.makedirs(osp.join(args.output_dir, "SegmentationObject"))
    os.makedirs(osp.join(args.output_dir, "SegmentationObjectPNG"))
    if not args.noviz:
        os.makedirs(
            osp.join(args.output_dir, "SegmentationObjectVisualization")
        )
    print("Creating dataset:", args.output_dir)
 
    class_names = []
    class_name_to_id = {}
    for i, line in enumerate(open(args.labels).readlines()):
        class_id = i - 1  # starts with -1
        class_name = line.strip()
        class_name_to_id[class_name] = class_id
        if class_id == -1:
            assert class_name == "__ignore__"
            continue
        elif class_id == 0:
            assert class_name == "_background_"
        class_names.append(class_name)
    class_names = tuple(class_names)
    print("class_names:", class_names)
    out_class_names_file = osp.join(args.output_dir, "class_names.txt")
    with open(out_class_names_file, "w") as f:
        f.writelines("\n".join(class_names))
    print("Saved class_names:", out_class_names_file)
 
    for filename in glob.glob(osp.join(args.input_dir, "*.json")):
        print("Generating dataset from:", filename)
 
        label_file = labelme.LabelFile(filename=filename)
 
        base = osp.splitext(osp.basename(filename))[0]
        out_img_file = osp.join(args.output_dir, "JPEGImages", base + ".jpg")
        out_cls_file = osp.join(
            args.output_dir, "SegmentationClass", base + ".npy"
        )
        out_clsp_file = osp.join(
            args.output_dir, "SegmentationClassPNG", base + ".png"
        )
        if not args.noviz:
            out_clsv_file = osp.join(
                args.output_dir,
                "SegmentationClassVisualization",
                base + ".jpg",
            )
        out_ins_file = osp.join(
            args.output_dir, "SegmentationObject", base + ".npy"
        )
        out_insp_file = osp.join(
            args.output_dir, "SegmentationObjectPNG", base + ".png"
        )
        if not args.noviz:
            out_insv_file = osp.join(
                args.output_dir,
                "SegmentationObjectVisualization",
                base + ".jpg",
            )
 
        img = labelme.utils.img_data_to_arr(label_file.imageData)
        imgviz.io.imsave(out_img_file, img)
 
        cls, ins = labelme.utils.shapes_to_label(
            img_shape=img.shape,
            shapes=label_file.shapes,
            label_name_to_value=class_name_to_id,
        )
        ins[cls == -1] = 0  # ignore it.
 
        # class label
        labelme.utils.lblsave(out_clsp_file, cls)
        np.save(out_cls_file, cls)
        if not args.noviz:
            clsv = imgviz.label2rgb(
                cls,
                imgviz.rgb2gray(img),
                label_names=class_names,
                font_size=15,
                loc="rb",
            )
            imgviz.io.imsave(out_clsv_file, clsv)
 
        # instance label
        labelme.utils.lblsave(out_insp_file, ins)
        np.save(out_ins_file, ins)
        if not args.noviz:
            instance_ids = np.unique(ins)
            instance_names = [str(i) for i in range(max(instance_ids) + 1)]
            insv = imgviz.label2rgb(
                ins,
                imgviz.rgb2gray(img),
                label_names=instance_names,
                font_size=15,
                loc="rb",
            )
            imgviz.io.imsave(out_insv_file, insv)
 
 
if __name__ == "__main__":
    main()

使用说明:

cd进入你的数据库(data_annotated文件夹所在位置),原始标注好文件(jpg、json)存放在data_annotated文件夹,先建立一个labels.txt文件内容为:

__ignore__
_background_
类别1
类别2
类别3
...

重要说明:

  • 第一行必须是 __ignore__
  • 第二行必须是 _background_

从第三行开始才是您的实际类别名称

  • 每个类别占一行
  • 不能有空行
  • 注意下划线的数量:
  • __ignore__ 是两个下划线
  • _background_ 是一个下划线

然后根据你的需求选择下列命令之一

python labelme2voc.py data_annotated data_dataset_voc --labels labels.txt
python labelme2coco.py data_annotated data_dataset_coco --labels labels.txt 

 

运行画面:

 如果运行报错大概率是环境问题

环境参考:

Package                 Version
----------------------- --------------------
absl-py                 2.1.0
asttokens               2.4.1
backcall                0.2.0
beautifulsoup4          4.12.3
cachetools              5.3.3
certifi                 2024.2.2
charset-normalizer      3.3.2
colorama                0.4.6
coloredlogs             15.0.1
contourpy               1.1.1
cycler                  0.12.1
Cython                  3.0.9
decorator               5.1.1
executing               2.0.1
filelock                3.13.1
flatbuffers             24.3.7
fonttools               4.50.0
gdown                   5.1.0
gitdb                   4.0.11
GitPython               3.1.42
google-auth             2.29.0
google-auth-oauthlib    1.0.0
grpcio                  1.62.1
humanfriendly           10.0
idna                    3.6
imageio                 2.34.0
imgviz                  1.7.5
importlib_metadata      7.1.0
importlib_resources     6.4.0
ipython                 8.12.3
jedi                    0.19.1
kiwisolver              1.4.5
labelImg                1.8.6
labelme                 5.4.1
lazy_loader             0.3
lxml                    5.1.0
Markdown                3.6
MarkupSafe              2.1.5
matplotlib              3.7.5
matplotlib-inline       0.1.6
mpmath                  1.3.0
natsort                 8.4.0
networkx                3.1
numpy                   1.24.4
oauthlib                3.2.2
onnxruntime             1.17.1
opencv-python           4.9.0.80
packaging               24.0
pandas                  2.0.3
parso                   0.8.3
pickleshare             0.7.5
pillow                  10.2.0
pip                     23.3.1
prompt-toolkit          3.0.43
protobuf                5.26.0
psutil                  5.9.8
pure-eval               0.2.2
py-cpuinfo              9.0.0
pyasn1                  0.5.1
pyasn1-modules          0.3.0
pycocotools-windows     2.0.0.2
Pygments                2.17.2
pyparsing               3.1.2
PyQt5                   5.15.10
PyQt5-Qt5               5.15.2
PyQt5-sip               12.13.0
pyreadline3             3.4.1
PySocks                 1.7.1
python-dateutil         2.9.0.post0
pytz                    2024.1
PyWavelets              1.4.1
PyYAML                  6.0.1
QtPy                    2.4.1
requests                2.31.0
requests-oauthlib       2.0.0
rsa                     4.9
scikit-image            0.21.0
scipy                   1.10.1
seaborn                 0.13.2
setuptools              68.2.2
six                     1.16.0
smmap                   5.0.1
soupsieve               2.5
stack-data              0.6.3
sympy                   1.12
tensorboard             2.14.0
tensorboard-data-server 0.7.2
termcolor               2.4.0
thop                    0.1.1.post2209072238
tifffile                2023.7.10
torch                   1.8.0+cu111
torchaudio              0.8.0
torchvision             0.9.0+cu111
tqdm                    4.66.2
traitlets               5.14.2
typing_extensions       4.10.0
tzdata                  2024.1
ultralytics             8.1.34
urllib3                 2.2.1
wcwidth                 0.2.13
Werkzeug                3.0.1
wheel                   0.41.2
zipp                    3.18.1


http://www.kler.cn/a/529755.html

相关文章:

  • Node.js 和 npm 安装教程
  • 全面认识了解DeepSeek+利用ollama在本地部署、使用和体验deepseek-r1大模型
  • 仿真设计|基于51单片机的温湿度、一氧化碳、甲醛检测报警系统
  • 电脑要使用cuda需要进行什么配置
  • 创建前端项目的方法
  • vue2项目(一)
  • LeetCode 2909. 元素和最小的山形三元组 II
  • 实验9 JSP访问数据库(二)
  • 94,【2】buuctf web [安洵杯 2019]easy_serialize_php
  • 三角形的最大周长(976)
  • 群晖NAS安卓Calibre 个人图书馆
  • 在C++中,成员变量必须在对象构造完成前初始化,但初始化的方式有多种...
  • K8s Kubernetes集群部署
  • 【黄啊码】DeepSeek提示词大道至简版
  • 62.病毒在封闭空间中的传播时间|Marscode AI刷题
  • 深度学习查漏补缺:2. 三个指标和注意力机制
  • springboot 启动原理
  • 图像噪声处理技术:让图像更清晰的艺术
  • deepseek v3 搭建个人知识库
  • 冲刺一区!挑战7天完成一篇趋势性分析GBD DAY1-7
  • 算法8--归并
  • Linux防火墙基础
  • 【linux网络(5)】传输层协议详解(下)
  • 使用QMUI实现用户协议对话框
  • 第 1 天:UE5 C++ 开发环境搭建,全流程指南
  • [Linux]从零开始的STM32MP157 U-Boot移植