#!/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", 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"))
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()
labelme标注数据集转化为coco格式
需积分: 0 3 浏览量
更新于2023-03-07
2
收藏 2KB ZIP 举报
在计算机视觉领域,数据集的标注对于训练模型至关重要。`Labelme`是一个广泛使用的开源工具,它允许用户通过图形界面方便地对图像进行标注,支持多种任务,包括人体和动物的关键点检测、目标检测以及语义分割。而将`Labelme`标注的数据集转化为`COCO`(Common Objects in Context)格式则是为了适应许多深度学习框架和评估工具的要求,因为`COCO`格式是目前最常用的标注标准之一。
`Python Labelme`是`Labelme`工具的Python接口,它提供了与`Labelme`交互的命令行功能,如加载、保存、转换标注等。在`labelme2coco.py`这个脚本中,我们将看到如何利用`labelme`库将`Labelme`的JSON标注文件转换为`COCO`格式的JSON文件。
我们需要安装`labelme`库,通常可以通过运行`pip install labelme`来完成。`labelme`库包含了`labelme.utils`模块,其中的函数可以处理`Labelme`的标注数据。
在转换过程中,我们主要关注以下几个步骤:
1. **读取Labelme的JSON文件**:每个`Labelme`标注的图像都有对应的JSON文件,存储了图像的元数据和标注信息。我们可以使用`labelme.utils.load_labelme_json`函数来读取这些信息。
2. **解析标注数据**:JSON文件中的标注数据通常包括边界框(用于目标检测)、像素级别的类别信息(用于语义分割)和关键点(用于关键点检测)。我们需要解析这些信息,以便于构造`COCO`格式的标注。
3. **构建COCO格式的数据结构**:`COCO`格式要求有`categories`(类别)、`images`(图像)和`annotations`(标注)三个主要部分。我们需要根据`Labelme`的数据创建对应的`COCO`结构。
4. **转换边界框**:`Labelme`的边界框是以像素坐标表示的,而`COCO`格式要求的是相对坐标。因此,我们需要将边界框从绝对像素坐标转换为相对于图像宽度和高度的比例坐标。
5. **处理关键点**:对于关键点检测,`Labelme`和`COCO`格式的表示方式略有不同。`Labelme`的关键点是二维坐标,`COCO`则需要每个关键点的可见性信息(0或1,表示是否可见)。我们需要适当地添加这个信息。
6. **写入COCO格式的JSON文件**:我们将转换后的数据写入新的JSON文件,这样就可以供`COCO API`或者其他支持`COCO`格式的工具使用。
`labelme2coco.py`脚本通常会接收一个目录路径作为参数,该目录包含所有`Labelme`的JSON文件。脚本会遍历这个目录,对每个JSON文件执行上述步骤,然后生成对应的`COCO`格式JSON文件。
转换完成后,我们可以使用`COCO API`进行数据预处理、训练模型或者评估模型性能。`COCO API`提供了丰富的功能,如加载数据集、生成训练验证集、计算AP(平均精度)、绘制分割掩模等。
`labelme2coco.py`脚本是将`Labelme`标注的数据集转换为`COCO`格式的关键工具,使得标注数据能够无缝对接到各种深度学习框架,加速计算机视觉项目的开发流程。

Move_tua
- 粉丝: 2
最新资源
- 反垄断法之电子商务市场反垄断规制(BB交易市场).doc
- 平面设计实施方案实训六Photoshop色彩调整.doc
- 初探网络游戏虚拟财产保险法律问题.doc
- 2017年度大数据时代的互联网信息安全考试及答案.doc
- 基于大数据的高职英语写作教学改革探讨.docx
- 基于云计算医疗物资供应商管理平台解决方案.docx
- 初中信息技术教学如何提升学生的网络学习能力.docx
- 基于PLC控制的打地鼠游戏装置的设计与制作.docx
- 移动互联网技术在物业管理中的应用.docx
- 大数据时代下如何做好初中英语课堂的教学改革.docx
- 计算机科学及其技术的发展趋势研究.docx
- 无线网络视频监控系统实施方案概述.doc
- 互联网金融专业化销售流程.ppt
- VB宿舍文档管理系统论文范文.doc
- 项目管理学概论作业题答案.doc
- 单片机步进电动机控制系统方案设计书.doc