# -*- coding: utf-8 -*-

"""图片增强

一个目录下有多个类别"""import numpyasnp

import imgaugasia

import imgaug.augmentersasiaa

import matplotlib.pyplotasplt

import osfromskimage import io

import random

import time

#根目录,其中是不同类别的子目录,子目录中是图片

data_dir= r"/home/joe/Documents/abc/"total_count= 4#所有子目录下文件计数for (rootpath, dirnames, filenames) inos.walk(data_dir):for dirnametmp indirnames:

dirnametmp= data_dir+dirnametmp+"/"#每个子文件夹for (rootpath, dirnames, filenames) inos.walk(dirnametmp):

filenamescount=len(filenames)

imgcounttmp= 1#当前子目录下的文件计数for icount inrange(filenamescount):

imgname=filenames.pop() #每个图片

#qqq,有imgaug的表示造的图片

file_extension=os.path.splitext(imgname)

filetmp= file_extension[0] #文件名不带扩展名

extension= file_extension[1] #扩展名if filetmp.endswith("_imgaug"):continueimgfullnametmp= dirnametmp+imgname #每个图片全路径

print( f"{imgcounttmp}/{total_count} {imgfullnametmp}")

imgcounttmp+= 1total_count+= 1image=io.imread(imgfullnametmp)

saveimage=[]

random1= random.uniform(-5,5)

random2= random.uniform(0,1)

random3= random.randint(1,5)

augtmp= iaa.Affine(rotate=(-25+random1, 25.5+random1))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Fliplr(1.0)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Crop(px=(0, random3*2), keep_size=True)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Sometimes(random2,iaa.GaussianBlur(sigma=(random2, 0.5+random2)))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.ContrastNormalization((0.75+random2, 1.5+random2))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.AdditiveGaussianNoise(loc=random3, scale=(0.01+random2, 0.08*255+random2))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp=iaa.Affine(

scale={"x": (0.8+random2, 1.2+random2), "y": (0.8+random2, 1.2+random2)}, #缩放

translate_percent={"x": (-0.2+random2, 0.2+random2), "y": (-0.2+random2, 0.2+random2)}, #平移

rotate=(-20+random1, 60+random1), #旋转

shear=(-8+random1, 8+random1) )

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.GaussianBlur(sigma=random2)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.AverageBlur(k=random3)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.MedianBlur(k=random3*2+1)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp=iaa.MotionBlur()

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp=iaa.BilateralBlur()

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.WithColorspace(to_colorspace="HSV")

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.AddToHueAndSaturation((-20, 20), per_channel=True)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Grayscale(alpha=1.0)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.CLAHE(clip_limit=(1, random3*10))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.GammaContrast(gamma=2*random2+1)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.SigmoidContrast(gain= random2+0.5)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.LogContrast(gain=2*random2+1)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.LinearContrast(alpha=random3)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.LinearContrast(alpha=random3*2)

res=augtmp.augment_image(image)

saveimage.append(res)

aug= iaa.CLAHE(clip_limit=(1, random3*20))

res=augtmp.augment_image(image)

saveimage.append(res)

#保存图片

imgid= 0t=time.time()

tt= round(t*1000)for imgtmp insaveimage:

#原文件名带有扩展名,需要拆分开

#文件名加时间戳,imgaug的表示造的图片

newname= filetmp+"_" +str(tt)+str(imgid)+"_imgaug"+extension

newfullname= dirnametmp+newname

#ia.imshow(imgtmp)

io.imsave(newfullname,imgtmp)

imgid+= 1

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐