CVAT Cityscapes 格式导入导出实战:像素级标注编码、标签色表与源码级实现解析
CVAT Cityscapes 格式导入导出实战像素级标注编码、标签色表与源码级实现解析【免费下载链接】cvatComputer Vision Annotation Tool (CVAT) is a leading platform for building high-quality visual datasets for vision AI. It offers open-source, cloud, and enterprise products, as well as labeling services, for image, video, and 3D annotation with AI-assisted labeling, quality assurance, team collaboration, analytics, and developer APIs.项目地址: https://gitcode.com/GitHub_Trending/cvat/cvatCityscapes 是面向城市街景语义分割与实例分割的像素级标注事实标准CVAT 通过 Datumaro 引擎为其提供了完整的导出为 Cityscapes ZIP / 从 Cityscapes ZIP 导入能力。本文以 CVAT 官方文档 format-cityscapes.md 为主线完整梳理其导出/导入的标注支持范围、.zip包结构、gtFine三类 PNG 的像素编码规则与原始色表定义并结合 cityscapes.py 等源码说明形状到掩码的转换链、标签色表label map的生成逻辑与Convert masks to polygons选项的默认行为帮助你把 CVAT 任务与标准 Cityscapes 数据管线可靠打通。Cityscapes 格式概述Cityscapes 格式在计算机视觉领域被广泛用作标准格式尤其适用于城市街景中的语义分割semantic segmentation与实例分割instance segmentation任务。该格式通常由高分辨率的城市街景图像及其详细的像素级标注组成每个像素都被标注为road道路pedestrian行人vehicle车辆等类别之一是训练和验证城市环境理解模型的宝贵资源也是自动驾驶、机器人、智慧城市方向研究人员和工程人员的常用选择。Cityscapes 导出支持的标注类型与限制从文档定义来看Cityscapes 导出遵循以下规则支持的标注Masks掩码、Polygons转换为掩码、Bounding Boxes转换为掩码、Ellipses转换为掩码。由于 Cityscapes 是纯像素级格式CVAT 在导出时会统一把各类形状栅格化为掩码属性Attributesis_crowdboolean必须为相应标签定义为checkbox复选框类型属性。它指定该标注标签是否能区分不同实例instance。如果is_crowd为False导出的标注会包含实例 IDinstance id值Tracks轨迹不支持轨迹会被拆导出为独立的形状exported as separate shapes。导出产物ZIP 包结构导出结果是名为taskname.zip的归档文件内部结构如下taskname.zip/ ├── label_color.txt ├── gtFine │ ├── subset_name │ │ └── city_name │ │ ├── image_0_gtFine_instanceIds.png │ │ ├── image_0_gtFine_color.png │ │ ├── image_0_gtFine_labelIds.png │ │ ├── image_1_gtFine_instanceIds.png │ │ ├── image_1_gtFine_color.png │ │ ├── image_1_gtFine_labelIds.png │ │ ├── ... └── imgsFine # if saving images was requested └── leftImg8bit ├── subset_name │ └── city_name │ ├── image_0_leftImg8bit.png │ ├── image_1_leftImg8bit.png │ ├── ...各文件的含义label_color.txt描述每个标签颜色的文件每行为r g b label_name# label_color.txt example # r g b label_name 0 0 0 background 0 255 0 tree ...*_gtFine_color.png类别标签以颜色编码——每个像素的 RGB 值对应该类别在色表中的颜色*_gtFine_labelIds.png类别标签以索引index编码——每个像素值为该类别的类别 ID*_gtFine_instanceIds.png类别与实例联合编码的超级像素 ID图。像素值同时编码类别与单个实例对 ID 做整除 1000 的整数部分得到类别 ID余数mod 1000得到实例 ID。如果某条标注描述的是多个实例crowd即is_crowd为真其像素只写入该类别的常规 ID不含实例号。源码解析导出流水线CVAT 的 Cityscapes 导出器注册在 cityscapes.py 中通过exporter(nameCityscapes, extZIP, version1.0)装饰器登记显示名为Cityscapes 1.0。其_export函数的执行链是GetCVATDataExtractor(instance_data, include_imagessave_images)从数据库构建 Datumaro 数据抽取器save_images参数即对应 UI 上保存图像选项决定最终包中是否包含imgsFine目录依次施加一串形状到掩码的变换RotatedBoxesToPolygons旋转框转多边形→polygons_to_masks→boxes_to_masks→EllipsesToMasks椭圆光栅化为 RLE 掩码实现见 transformations.py 中基于cv2.ellipse的编码→merge_instance_segments合并同一实例的分段dataset.export(temp_dir, cityscapes, save_mediasave_images, apply_colormapTrue, label_map...)调用 Datumaro 的 cityscapes 导出器其中label_map由make_colormap(instance_data)构造即任务标签名 → 标签颜色 RGB的映射make_zip_archive把中间目录打包成最终 ZIP。其中label_map的构造函数 make_colormap 有一个与文档强相关的行为如果任务标签中没有background会自动在首位插入一个黑色背景标签{name: background, color: #000000}。这正是文档强调必须有一个黑色背景的标签这一要求的底层依据——导出逻辑本身就会保证背景存在但若你的业务背景类别命名/颜色与原始 Cityscapes 不一致导入回标准管线时可能产生歧义因此建议任务中显式保留黑色background标签。Cityscapes 导入支持范围支持的标注MasksPolygons当启用Convert masks to polygons选项时掩码会被转换为多边形导入属性is_crowdboolean须定义为checkbox类型Tracks不支持。上传文件结构上传的.zip归档结构为archive.zip/ ├── label_color.txt # optional └── gtFine └── city_name ├── image_0_gtFine_instanceIds.png ├── image_1_gtFine_instanceIds.png ├── ...源码解析导入流水线与颜色表文件导入器_import的实现见 cityscapes.pyshutil.unpack_archive解压 ZIP 到临时目录检查目录下的标签颜色表文件。从源码看导入器实际检查与写入的文件名是label_colors.txt若压缩包中未提供该文件则用任务自身标签颜色经write_label_map生成一份作为gtFine像素解码的依据。因此上传包若自带颜色表建议使用导入器处理的label_colors.txt文件名避免被按任务默认颜色重新生成detect_dataset以format_namecityscapes探测目录结构随后StreamDataset.import_from(temp_dir, cityscapes, envdm_env)完成像素图到掩码的反向解码MaskToPolygonTransformation.convert_dataset(dataset, **kwargs)控制掩码转多边形。该变换的选项名为conv_mask_to_poly见 transformations.py默认值为True即默认把导入的掩码转为多边形形状与文档中Polygons (if Convert masks to polygons is enabled)一致。该参数沿调用链从 REST 层传入task.py 中import_task_annotations(src_file, task_id, format_name, conv_mask_to_poly, ...)将其透传给 importerproject.py 的import_dataset_as_project同样以conv_mask_to_poly参数支持项目级导入最后import_dm_annotations把解析出的标注写回任务/作业数据库。创建 Cityscapes 任务标签与颜色配置文档给出的任务创建流程分为三步并特别强调标签配置的约束创建带所需标签的任务——你可以自定义标签也可以直接复用原始 Cityscapes 数据集的标签与颜色。但要处理 Cityscapes 格式必须有一个黑色#000000的background标签上传图像打包结构如images.zip/ ├── image_0.jpg ├── image_1.jpg ├── ...创建任务后按上一节描述的 ZIP 结构上传 Cityscapes 标注。此外若需要导出/导入实例 ID对应标签须定义is_crowd布尔复选框属性。CVAT 格式测试资产 tasks.json 中的Cityscapes 1.0任务正是一个标准示例car标签带有is_crowd属性input_type为checkbox、mutable为false、取值[false, true]person标签无属性并始终包含黑色background标签——与文档要求一一对应。原始 Cityscapes 色表34 类如果你要与官方数据集保持标签/颜色对齐文档提供了完整的原始色表JSON可原样用于任务标签定义[ {name: unlabeled, color: #000000, attributes: []}, {name: egovehicle, color: #000000, attributes: []}, {name: rectificationborder, color: #000000, attributes: []}, {name: outofroi, color: #000000, attributes: []}, {name: static, color: #000000, attributes: []}, {name: dynamic, color: #6f4a00, attributes: []}, {name: ground, color: #510051, attributes: []}, {name: road, color: #804080, attributes: []}, {name: sidewalk, color: #f423e8, attributes: []}, {name: parking, color: #faaaa0, attributes: []}, {name: railtrack, color: #e6968c, attributes: []}, {name: building, color: #464646, attributes: []}, {name: wall, color: #66669c, attributes: []}, {name: fence, color: #be9999, attributes: []}, {name: guardrail, color: #b4a5b4, attributes: []}, {name: bridge, color: #966464, attributes: []}, {name: tunnel, color: #96785a, attributes: []}, {name: pole, color: #999999, attributes: []}, {name: polegroup, color: #999999, attributes: []}, {name: trafficlight, color: #faaa1e, attributes: []}, {name: trafficsign, color: #dcdc00, attributes: []}, {name: vegetation, color: #6b8e23, attributes: []}, {name: terrain, color: #98fb98, attributes: []}, {name: sky, color: #4682b4, attributes: []}, {name: person, color: #dc143c, attributes: []}, {name: rider, color: #ff0000, attributes: []}, {name: car, color: #00008e, attributes: []}, {name: truck, color: #000046, attributes: []}, {name: bus, color: #003c64, attributes: []}, {name: caravan, color: #00005a, attributes: []}, {name: trailer, color: #00006e, attributes: []}, {name: train, color: #005064, attributes: []}, {name: motorcycle, color: #0000e6, attributes: []}, {name: bicycle, color: #770b20, attributes: []}, {name: licenseplate, color: #00000e, attributes: []} ]需要注意色表中的隐含约束unlabeled、egovehicle、rectificationborder、outofroi、static均映射为黑色#000000——与背景必须为黑色的要求一致即多个不可区分/未标注语义都归并到背景。格式注册与测试验证Cityscapes 格式在格式注册表中以Cityscapes 1.0显示名分别注册到导出与导入两侧注册机制registry.py 中exporter/importer装饰器把格式函数包装为带NAME、VERSION、EXT、DISPLAY_NAME元信息的对象分别存入EXPORT_FORMATS与IMPORT_FORMATS字典cityscapes模块在文件尾部随所有格式模块一起导入完成自注册见 registry.py 的导入段测试佐证test_formats.py 断言导出/导入格式集合均包含Cityscapes 1.0同时其中一行注释明确记录# (Cityscapes 1.0, cityscapes), does not support, empty annotations说明 Cityscapes 格式的空标注无标注场景在通用空标注往返测试中是被排除的属于该格式的已知限制。实践建议与已知限制结合文档与源码使用 Cityscapes 格式时建议注意以下几点形状多样性统一为掩码导出前 CVAT 会把旋转框、多边形、矩形框、椭圆全部转换为掩码RotatedBoxesToPolygons、polygons_to_masks、boxes_to_masks、EllipsesToMasks原始形状信息在导出后不可恢复轨迹不可用Tracks 会被拆为独立形状导出导入侧也不支持轨迹还原实例语义依赖is_crowd只有is_crowd为False的标注才会写入 instance IDcrowd 标注在instanceIds.png中退化为纯类别 ID。标签必须正确定义checkbox类型的is_crowd属性可参考 tasks.json 的测试任务定义背景与颜色表一致性确保存在黑色background标签跨数据集交换时注意导出包中的label_color.txt与导入侧实际处理的label_colors.txt文件名约定避免因颜色表缺失而按任务默认颜色解码掩码转多边形默认开启导入时conv_mask_to_poly默认为True掩码会转换为多边形以便在 UI 中编辑如需保留掩码形态可在导入选项中关闭该转换。参考文档原文Cityscapes 格式说明导出/导入实现cityscapes.py形状转换椭圆/旋转框/掩码转多边形transformations.py标签色表构造utils.py格式注册表registry.py导入调用链task.py、project.py测试与测试资产test_formats.py、tasks.json【免费下载链接】cvatComputer Vision Annotation Tool (CVAT) is a leading platform for building high-quality visual datasets for vision AI. It offers open-source, cloud, and enterprise products, as well as labeling services, for image, video, and 3D annotation with AI-assisted labeling, quality assurance, team collaboration, analytics, and developer APIs.项目地址: https://gitcode.com/GitHub_Trending/cvat/cvat创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考