东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4647|回复: 1
打印 上一主题 下一主题

[课堂笔记] 图片数据增强库imgaug的安装与使用

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2020-10-29 15:11:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
图片数据增强库imgaug的安装与使用


pip3 install imgaug
Installing collected packages: Shapely, imgaug
Successfully installed Shapely-1.7.1 imgaug-0.4.0



  1. import os
  2. import glob
  3. import cv2
  4. import numpy as np
  5. import imageio
  6. import imgaug as ia
  7. import imgaug.augmenters as iaa

  8. input_dir = "/home/jiang/DataSets/bctc_face_special"
  9. output_dir = "/home/jiang/DataSets/bctc_face_special_imgaug"
  10. # output_dir = "/home/jiang/DataSets/bctc_guang_zhao/0_no_pass"

  11. ia.seed(10)

  12. sometimes = lambda aug: iaa.Sometimes(0.5, aug)

  13. # Pipeline:
  14. # (1) Crop images from each side by 1-16px, do not resize the results
  15. #     images back to the input size. Keep them at the cropped size.
  16. # (2) Horizontally flip 50% of the images.
  17. # (3) Blur images using a gaussian kernel with sigma between 0.0 and 3.0.
  18. seq_simple = iaa.Sequential([
  19.     # iaa.Crop(px=(1, 16), keep_size=True),
  20.     # iaa.Fliplr(0.5),
  21.     # iaa.Affine(rotate=(-25, 25)),
  22.     # iaa.GaussianBlur(sigma=(0, 3.0)),
  23.     # iaa.AdditiveGaussianNoise(scale=(30, 90)),
  24. ])

  25. seq = iaa.Sequential(
  26.     [
  27.         # apply the following augmenters to most images
  28.         iaa.Fliplr(0.5), # horizontally flip 50% of all images
  29.         # crop images by -5% to 10% of their height/width
  30.         sometimes(iaa.CropAndPad(
  31.             percent=(-0.05, 0.1),
  32.             pad_mode=ia.ALL,
  33.             pad_cval=(0, 255)
  34.         )),
  35.         sometimes(iaa.Affine(
  36.             scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
  37.             translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
  38.             rotate=(-25, 25), # rotate by -45 to +45 degrees
  39.             shear=(-16, 16), # shear by -16 to +16 degrees
  40.             order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
  41.             cval=(0, 255), # if mode is constant, use a cval between 0 and 255
  42.             mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
  43.         )),
  44.         # execute 0 to 5 of the following (less important) augmenters per image
  45.         # don't execute all of them, as that would often be way too strong
  46.         iaa.SomeOf((0, 8),
  47.             [
  48.                 iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)), # convert images into their superpixel representation
  49.                 iaa.OneOf([
  50.                     iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
  51.                     iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
  52.                     iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
  53.                 ]),
  54.                 # search either for all edges or for directed edges,
  55.                 # blend the result with the original image using a blobby mask

  56.                 iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
  57.                 iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
  58.                 iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
  59.                 iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
  60.                 # either change the brightness of the whole image (sometimes
  61.                 # per channel) or change the brightness of subareas
  62.                 iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
  63.                 iaa.Grayscale(alpha=(0.8, 1.0)),
  64.                 sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
  65.                 sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
  66.                 sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
  67.             ],
  68.             random_order=True
  69.         )
  70.     ],
  71.     random_order=True
  72. )


  73. for img_path in glob.glob(os.path.join(input_dir, "*.png")):
  74.     img_name = img_path.strip().split("/")[-1].split(".")[0]
  75.     print("图片链接:", img_path)
  76.     img_ori = imageio.imread(img_path)
  77.     # print("原始图片的shape:", img_ori.shape)
  78.     img_ori = img_ori[:, :, :3]
  79.     assert img_ori.shape[2] == 3, "必须是3通道的图片才能增强!"
  80.     # ia.imshow(img_ori)
  81.     # 一张原始图片要 增强多少张?
  82.     images = [img_ori] * 399
  83.     images_aug = seq.augment_images(images=images)

  84.     img_ori = cv2.resize(img_ori, dsize=(80, 80))
  85.     imageio.imwrite(os.path.join(output_dir, "%s_ori.png" % img_name), img_ori)

  86.     # ia.imshow(np.hstack(images_aug))
  87.     # ia.imshow(ia.draw_grid(images_aug, cols=8, rows=5))
  88.     for idx, image_aug in enumerate(images_aug):
  89.         # 保存增强后的图片
  90.         # print(type(image_aug))
  91.         image_aug = cv2.resize(image_aug, dsize=(80, 80))
  92.         imageio.imwrite(os.path.join(output_dir, "%s_aug_%d.png" % (img_name, idx)), image_aug)

  93.     assert 0 == 1, "停"
  94.     pass
复制代码







让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

100

帖子

236

积分

2W人工智能培训

Rank: 10Rank: 10Rank: 10

积分
236
沙发
发表于 2020-11-10 19:51:29 | 只看该作者
积分积分积分
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-8 05:17 , Processed in 0.195698 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表