1. 程式人生 > >使用imgaug--python影象資料增強庫進行Bounding Boxes影像增強

使用imgaug--python影象資料增強庫進行Bounding Boxes影像增強

使用imgaug影象資料增強庫進行Bounding Boxes影像增強

簡介

相較於Augmentor,imgaug具有更多的功能,比如對影像增強的同時,對keypoint, bounding box進行相應的變換。例如在目標檢測的過程中,訓練集包括影像及其對應的bounding box檔案,在對影像增強的時候,同時解算出bounding box 相應變換的座標生成對應的bounding box檔案。

程式碼

imgaug安裝

imgaug使用文件

安裝依賴庫

pip install six numpy scipy matplotlib scikit-image opencv-python imageio

安裝imgaug

方式一(安裝github最新版本):

pip install git+https://github.com/aleju/imgaug

方式二(安裝pypi版本):

pip install imgaug

Bounding Boxes實現

讀取原影像bounding boxes座標

讀取xml檔案並使用ElementTree對xml檔案進行解析,找到每個object的座標值。

def read_xml_annotation(root, image_id):
    in_file = open(os.path.join(root, image_id))
    tree = ET.parse(in_file)
    root = tree.getroot()
    bndboxlist = []

    for object in root.findall('object'):  # 找到root節點下的所有object節點
        bndbox = object.find('bndbox')  # 子節點下節點rank的值

        xmin =
int(bndbox.find('xmin').text) xmax = int(bndbox.find('xmax').text) ymin = int(bndbox.find('ymin').text) ymax = int(bndbox.find('ymax').text) # print(xmin,ymin,xmax,ymax) bndboxlist.append([xmin,ymin,xmax,ymax]) # print(bndboxlist) bndbox = root.find('object').find('bndbox') return bndboxlist

生成變換後的bounding boxe座標檔案

傳入目標變換後的bounding boxe座標,將原座標替換成新座標並生成新的xml檔案。

def change_xml_list_annotation(root, image_id, new_target,saveroot,id):

    in_file = open(os.path.join(root, str(image_id) + '.xml'))  # 這裡root分別由兩個意思
    tree = ET.parse(in_file)
    xmlroot = tree.getroot()
    index = 0

    for object in xmlroot.findall('object'):  # 找到root節點下的所有country節點
        bndbox = object.find('bndbox')  # 子節點下節點rank的值

        new_xmin = new_target[index][0]
        new_ymin = new_target[index][1]
        new_xmax = new_target[index][2]
        new_ymax = new_target[index][3]

        xmin = bndbox.find('xmin')
        xmin.text = str(new_xmin)
        ymin = bndbox.find('ymin')
        ymin.text = str(new_ymin)
        xmax = bndbox.find('xmax')
        xmax.text = str(new_xmax)
        ymax = bndbox.find('ymax')
        ymax.text = str(new_ymax)

        index = index + 1

    tree.write(os.path.join(saveroot, str(image_id) + "_aug_" + str(id) + '.xml'))

生成變換序列

產生一個處理圖片的Sequential。

seq = iaa.Sequential([
        iaa.Flipud(0.5),  # vertically flip 20% of all images
        iaa.Fliplr(0.5),  # 映象
        iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect BBs
        iaa.GaussianBlur(sigma=(0, 3.0)),
        # iaa.GaussianBlur(0.5),
        iaa.Affine(
            translate_px={"x": 15, "y": 15},
            scale=(0.8, 0.95),
            rotate=(-30, 30)
        )  # translate by 40/60px on x/y axis, and scale to 50-70%, affects BBs
    ])

bounding box 變化後坐標計算

先讀取該影像對應xml檔案,獲取所有目標的bounding boxes,然後依次計算每個box變化後的座標。

bndbox = read_xml_annotation(XML_DIR, name)
for epoch in range(AUGLOOP):
    seq_det = seq.to_deterministic()  # 保持座標和影象同步改變,而不是隨機

    # 讀取圖片
    img = Image.open(os.path.join(IMG_DIR, name[:-4] + '.jpg'))
    img = np.array(img)

    # bndbox 座標增強
    for i in range(len(bndbox)):
        bbs = ia.BoundingBoxesOnImage([
        ia.BoundingBox(x1=bndbox[i][0], y1=bndbox[i][1], x2=bndbox[i][2], y2=bndbox[i][3]),
        ], shape=img.shape)

        bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
        boxes_img_aug_list.append(bbs_aug)

使用示例

資料準備

輸入資料為兩個資料夾一個是需要增強的影像資料(JPEGImages),一個是對應的xml檔案(Annotations)。注意:影像檔名需和xml檔名相對應!

Annotations

JPEGImages

設定檔案路徑

    IMG_DIR = "./dataset/JPEGImages" #輸入的影像資料夾路徑
    XML_DIR = "./dataset/Annotations" # 輸入的XML資料夾路徑


    AUG_XML_DIR = "./dataset/AUG_XML" # 儲存增強後的XML資料夾路徑
    mkdir(AUG_XML_DIR)

    AUG_IMG_DIR = "./dataset/AUG_IMG" # 儲存增強後的影像資料夾路徑
    mkdir(AUG_IMG_DIR)

設定增強次數

    AUGLOOP = 10 # 每張影像增強的數量

設定增強引數

通過修改Sequential函式引數進行設定,具體設定參考imgaug使用文件

seq = iaa.Sequential([
        iaa.Flipud(0.5),  # vertically flip 50% of all images
        iaa.Fliplr(0.5),  # 映象
        iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect BBs
        iaa.GaussianBlur(sigma=(0, 0.5)),
         # iaa.GaussianBlur(0.5),
        iaa.Affine(
            translate_px={"x": 15, "y": 15},
            scale=(0.8, 0.95),
            rotate=(-30, 30)
        )  
    ])

輸出

執行XMLaug.py ,執行結束後即可得到增強的影像和對應的xml資料夾
在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述