YOLO学習のためのファイル操作もろもろ (2)

画像の左右反転

import glob
import os
from PIL import Image

jpgfiles = glob.glob("./images/001/*.jpg")

for f in jpgfiles:
    (dirname, filename) = os.path.split(f)
    (name,ext) = os.path.splitext(filename)

    new_img = "LR_" + filename
    new_txt = "LR_" + name + ".txt"

    img = Image.open(f)
    imgLR = img.transpose(Image.FLIP_LEFT_RIGHT)
    imgLR.save("images/001/" + new_img)

    txt_file = open("./labels/001/" + name + ".txt", "r")
    lines = txt_file.read().replace("\r\n","\n").split('\n')

    txt_outfile = open("./labels/001/" + new_txt, "w")

    for line in lines:
        if(line != ""):
            elems = line.split(" ")
            txt_outfile.write(elems[0] + " " + str(1-float(elems[1])) + " " + elems[2] + " " + elems[3] + " " + elems[4] + '\n')

画像の上下反転

import glob
import os
from PIL import Image

jpgfiles = glob.glob("./images/001/*.jpg")

for f in jpgfiles:
    (dirname, filename) = os.path.split(f)
    (name,ext) = os.path.splitext(filename)

    new_img = "TD_" + filename
    new_txt = "TD_" + name + ".txt"

    img = Image.open(f)
    imgLR = img.transpose(Image.FLIP_LEFT_RIGHT)
    imgLR.save("images/001/" + new_img)

    txt_file = open("./labels/001/" + name + ".txt", "r")
    lines = txt_file.read().replace("\r\n","\n").split('\n')

    txt_outfile = open("./labels/001/" + new_txt, "w")

    for line in lines:
        if(line != ""):
            elems = line.split(" ")
            txt_outfile.write(elems[0] + " " + elems[1] + " " + str(1-float(elems[2])) + " " + elems[3] + " " + elems[4] + '\n')

画像の拡大、縮小

import glob
import os
import shutil
from PIL import Image

jpgfiles = glob.glob("./images/001/*.jpg")

for f in jpgfiles:
    (dirname, filename) = os.path.split(f)
    (name,ext) = os.path.splitext(filename)

    new_img_12 = "resize_12_" + filename
    new_img_08 = "resize_08_" + filename
    new_txt_12 = "resize_12_" + name + ".txt"
    new_txt_08 = "resize_08_" + name + ".txt"

    img = Image.open(f)
    img_resize_12 = img.resize((int(img.width*1.2), int(img.height*1.2)))
    img_resize_08 = img.resize((int(img.width*0.8), int(img.height*0.8)))
    img_resize_12.save("images/001/" + new_img_12)
    img_resize_08.save("images/001/" + new_img_08)


    shutil.copy("./labels/001/" + name + ".txt", "./labels/001/" + new_txt_12)
    shutil.copy("./labels/001/" + name + ".txt", "./labels/001/" + new_txt_08)

画像の明るさ変更

import glob
import os
import shutil
from PIL import Image
from PIL import ImageEnhance

jpgfiles = glob.glob("./images/001/*.jpg")

for f in jpgfiles:
    (dirname, filename) = os.path.split(f)
    (name,ext) = os.path.splitext(filename)

    new_img_bright = "bright_" + filename
    new_img_dark = "dark_" + filename

    new_txt_bright = "bright_" + name + ".txt"
    new_txt_dark = "dark_" + name + ".txt"

    img = Image.open(f)
    eim = ImageEnhance.Brightness(img)
    
    eim.enhance(1.1).save("images/001/" + new_img_bright)
    eim.enhance(0.9).save("images/001/" + new_img_dark)

    shutil.copy("./labels/001/" + name + ".txt", "./labels/001/" + new_txt_bright)
    shutil.copy("./labels/001/" + name + ".txt", "./labels/001/" + new_txt_dark)