Python解析LabelMe标注数据——打造自己的语义分割数据集

计算机视觉领域,语义分割是一项重要的任务。然而,建立一个高质量的语义分割数据集是非常困难的。在这方面,LabelMe可以帮助我们轻松解决这个问题。本篇文章将会介绍如何使用Python解析LabelMe标注数据,获取图片和对应的掩膜图像,以便于为语义分割任务创建自己的数据集。

首先,我们需要安装LabelMe库,这可以通过以下命令实现:

pip install labelme

我们需要从LabelMe中获取数据并保存到本地,代码如下:

import json
import os
import numpy as np
from PIL import Image
import base64
import labelme

# 设置标注文件夹路径以及保存路径
ANNOTATION_DIR = 'annotations'
IMAGE_SAVE_PATH = 'images'
MASK_SAVE_PATH = 'masks'

if not os.path.exists(IMAGE_SAVE_PATH):
    os.makedirs(IMAGE_SAVE_PATH)
if not os.path.exists(MASK_SAVE_PATH):
    os.makedirs(MASK_SAVE_PATH)

# 读取所有LabelMe标注数据
for filename in os.listdir(ANNOTATION_DIR):
    if not filename.endswith('.json'):
        continue
    w
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个示例的Python代码,用于对指定类别的labelme标注语义分割数据进行增强: ```python import os import cv2 import numpy as np def semantic_segmentation_augmentation(directory, save_directory, target_class): # 遍历指定目录下的文件 for filename in os.listdir(directory): if filename.endswith(".json"): # 查找以.json结尾的文件 json_file = os.path.join(directory, filename) image_file = json_file.replace(".json", ".jpg") # 将.json替换为.jpg if os.path.isfile(image_file): # 检查对应的图片文件是否存在 # 读取图片文件 image = cv2.imread(image_file) height, width, _ = image.shape # 读取JSON文件并获取标注信息 with open(json_file, 'r') as f: json_data = json.load(f) # 创建空白的语义分割图像 seg_image = np.zeros((height, width), dtype=np.uint8) # 处理每个标注对象 for shape in json_data['shapes']: class_name = shape['label'] if class_name == target_class: points = shape['points'] polygon_points = np.array(points, dtype=np.int32) cv2.fillPoly(seg_image, [polygon_points], 255) # 将增强后的语义分割图像保存到指定目录 save_path = os.path.join(save_directory, filename.replace(".json", ".png")) cv2.imwrite(save_path, seg_image) print(f"语义分割图像保存成功:{save_path}") else: print(f"找不到对应的图片文件:{image_file}") # 指定包含labelme标注文件的目录和保存增强后数据的目录 directory = "path/to/labelme/files" save_directory = "path/to/save/augmented/data" # 指定目标类别名称 target_class = "class_name" semantic_segmentation_augmentation(directory, save_directory, target_class) ``` 你需要将代码中的`"path/to/labelme/files"`替换为包含标注文件的实际目录路径,将`"path/to/save/augmented/data"`替换为你想要保存增强后数据的目录路径,将`"class_name"`替换为你想要增强的目标类别名称。运行代码后,它会遍历目录中的所有.json文件,读取标注信息并创建相应的语义分割图像,然后将增强后的语义分割图像保存到指定目录。 希望这可以帮到你!如果有任何问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员杨弋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值