编写python脚本对labelme标注的数据集进行左右/上下镜面翻转扩充数据集

编写python脚本对labelme标注的数据集进行左右/上下镜面翻转扩充数据集

labeme是什么?

一个用于标注图像语义分割或者说图像实例分割的标注软件

labelme标注文件格式

  • shapes - 标注的多边形数据
  • imagePath - 图片路径
  • imageData - 图片base64转义的图像数据
  • imageHeight - 图像的高度
  • imageWidth - 图像的宽度

特别说明

labelme读取的是json文件里的数据,图片数据是读imageData的base64数据而不是原图

Code

from PIL import Image
import os
import glob
import json
import base64

# 数据集路径
path = ".\\data\\"
# 生成数据的保存路径
save_path = ".\\data\\"
# 当前数据集图片格式
file_format = ".jpg"
# 替换格式jpg -> json
replace_format = ".json"
# 左右翻转文件名附加字符
LR = "lr_"
# 上下翻转文件名附加字符
TB = "tb_"
# 获取数据集目录的图片数据集
img_list = glob.glob(path + "*" + file_format)

print("左右翻转-start")
# 1.遍历图片
for i in range(len(img_list)):
    # 图片路径
    img_path = img_list[i]
    # 对应json路径
    json_path = img_list[i].replace(file_format, replace_format)
    # 判断json文件是否存在
    is_exists = os.path.exists(json_path)
    if is_exists:
        # 打开json文件
        f = open(json_path, encoding='utf-8')
        # 读取json
        setting = json.load(f)
        # 获取当前图片尺寸
        width = setting['imageWidth']
        height = setting['imageHeight']
        # 获取中轴
        mid_width = width / 2
        mid_height = height / 2

        print("中轴:x-" + str(mid_width) + ",y-" + str(mid_height))
        # 2.遍历shapes
        for i2 in range(len(setting['shapes'])):
            # 3.遍历每个shapes的点
            for i3 in range(len(setting['shapes'][i2]['points'])):
                temp_x = setting['shapes'][i2]['points'][i3][0]
                temp_y = setting['shapes'][i2]['points'][i3][1]
                if temp_x > mid_width:
                    dis = temp_x - mid_width
                    new_x = mid_width - dis
                elif temp_x < mid_width:
                    dis = mid_width - temp_x
                    new_x = mid_width + dis
                else:
                    new_x = temp_x
                new_y = temp_y
                setting['shapes'][i2]['points'][i3][0] = new_x
                setting['shapes'][i2]['points'][i3][1] = new_y
        # 从json获取文件名
        file_name = setting['imagePath']
        # 修改json文件名
        setting['imagePath'] = LR + file_name
        full_path = save_path + LR + file_name
        full_path = full_path.replace(file_format, replace_format)
        # 图片转换
        pri_image = Image.open(img_path)
        # 左右镜面翻转FLIP_LEFT_RIGHT
        pri_image.transpose(Image.FLIP_LEFT_RIGHT).save(path + LR + file_name)
        # 将转换后的图片进行base64加密
        with open(path + LR + file_name, 'rb') as f:
            setting['imageData'] = base64.b64encode(f.read()).decode()
        string = json.dumps(setting)
        # 将修改后的json写入文件
        with open(full_path, 'w', encoding='utf-8') as f:
            f.write(string)
            f.close()
        print(img_path + "-------转换完成")
        setting = None
    else:
        print(json_path + "-------文件不存在")
print("左右翻转-end")
# 原理同上
print("上下翻转-start")
for i in range(len(img_list)):
    img_path = img_list[i]
    json_path = img_list[i].replace(file_format, replace_format)
    is_exists = os.path.exists(json_path)
    if is_exists:
        f = open(json_path, encoding='utf-8')
        setting = json.load(f)
        width = setting['imageWidth']
        height = setting['imageHeight']
        mid_width = width / 2
        mid_height = height / 2

        for i2 in range(len(setting['shapes'])):
            for i3 in range(len(setting['shapes'][i2]['points'])):
                temp_x = setting['shapes'][i2]['points'][i3][0]
                temp_y = setting['shapes'][i2]['points'][i3][1]
                if temp_y > mid_height:
                    dis = temp_y - mid_height
                    new_y = mid_height - dis
                elif temp_y < mid_height:
                    dis = mid_height - temp_y
                    new_y = mid_height + dis
                else:
                    new_y = temp_y
                new_x = temp_x
                setting['shapes'][i2]['points'][i3][0] = new_x
                setting['shapes'][i2]['points'][i3][1] = new_y

        file_name = setting['imagePath']
        setting['imagePath'] = TB + file_name
        full_path = save_path + TB + file_name
        full_path = full_path.replace(file_format, replace_format)
        pri_image = Image.open(img_path)
        # 上下镜面翻转FLIP_TOP_BOTTOM
        pri_image.transpose(Image.FLIP_TOP_BOTTOM).save(path + TB + file_name)
        with open(path + TB + file_name, 'rb') as f:
            setting['imageData'] = base64.b64encode(f.read()).decode()
        string = json.dumps(setting)
        with open(full_path, 'w', encoding='utf-8') as f:
            f.write(string)
            f.close()
        print(img_path + "-------转换完成")
        setting = None
    else:
        print(json_path + "-------文件不存在")
print("上下翻转-end")

测试结果

原图

原图

左右镜面

左右镜面

上下镜面

上下镜面

可以使用Imgaug库对labelme标注后的json格式的数据集进行扩充。Imgaug是一个强大的图像增强库,可以应用各种图像增强技术来扩充数据集。 首先,你需要安装Imgaug库。你可以使用以下命令来安装Imgaug: ``` pip install imgaug ``` 接下来,你需要加载labelme标注后的json格式的数据集。你可以使用labelme库来加载和解析json文件。下面是一个示例代码: ```python import json from labelme import utils def load_labelme_json(json_path): with open(json_path, 'r') as f: data = json.load(f) return data json_data = load_labelme_json('path/to/json/file.json') ``` 然后,你可以使用Imgaug库来定义并应用各种图像增强技术。例如,你可以使用以下代码来实现随机水平翻转和随机旋转的图像增强: ```python import imgaug.augmenters as iaa # 定义图像增强器 augmenter = iaa.Sequential([ iaa.Fliplr(0.5), # 随机水平翻转概率为50% iaa.Affine(rotate=(-45, 45)) # 随机旋转角度范围为-45到45度 ]) # 对每张图像进行增强 for image_data in json_data['images']: image = utils.img_b64_to_arr(image_data['data']) augmented_image = augmenter.augment_image(image) # 在这里可以对增强后的图像进行保存或其他操作 ``` 注意,Imgaug库还支持许多其他的图像增强技术,如缩放、裁剪、亮度调整、颜色变换等。你可以根据自己的需求选择适当的增强技术,并将其添加到图像增强器中。 希望这个回答能对你有帮助!如果你还有其他问题,请继续提问。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alex-Leung

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

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

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

打赏作者

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

抵扣说明:

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

余额充值