編寫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")

測試結果

原圖

原圖

左右鏡面

左右鏡面

上下鏡面

上下鏡面

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章