def make(path, name): person = Person.objects.filter(name=name) if len(person) <= 0: person = Person(name=name) person.save() else: person = person.last() img_raw = Image(path=path, person_id=person.id) img_raw.save() try: img = cv2.imread(path) images = np.array([img for _ in range(12)], dtype=np.uint8) seq = iaa.Sequential([ iaa.Affine( rotate=(0.0, 30), translate_px=iap.RandomSign( iap.Poisson(3)) # set seed for randomSign ) ]) images_aug = seq.augment_images(images) store_path = os.path.join(Config.storePath, person.name.replace(' ', '')) for index, img in enumerate(images_aug): img_path = "{}/{}.jpg".format( store_path, person.name.replace(' ', '') + str(datetime.now().microsecond)) cv2.imwrite(img_path, img) img = Image(path=img_path, person_id=person.id) img.save() except IOError: print("Path not exists!")
def chapter_parameters_discrete(): ia.seed(1) # ----------------------- # Binomial # ----------------------- from imgaug import parameters as iap params = [iap.Binomial(0.5), iap.Binomial(0.9)] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "continuous_binomial.jpg", gridarr) # ----------------------- # DiscreteUniform # ----------------------- from imgaug import parameters as iap params = [ iap.DiscreteUniform(0, 10), iap.DiscreteUniform(-10, 10), iap.DiscreteUniform([-10, -9, -8, -7], 10), iap.DiscreteUniform((-10, -7), 10) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_discreteuniform.jpg", gridarr) # ----------------------- # Poisson # ----------------------- from imgaug import parameters as iap params = [ iap.Poisson(1), iap.Poisson(2.5), iap.Poisson((1, 2.5)), iap.RandomSign(iap.Poisson(2.5)) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_poisson.jpg", gridarr)
def perturb_glyphs(_font, number_of_points): poisson_distribution = iap.RandomSign(iap.Poisson(5)) for char in GlyphPerturber.characters: if char in font.getGlyphSet(): glyph = _font.getGlyphSet().get(char) random_indexes = random.sample(population=range(len(glyph._glyph.coordinates)), k=min(number_of_points, len(glyph._glyph.coordinates))) for index in random_indexes: glyph._glyph.coordinates[index] = (glyph._glyph.coordinates[index][0] + poisson_distribution.draw_sample(), glyph._glyph.coordinates[index][1] + poisson_distribution.draw_sample()) else: raise LookupError(f"Given font has no glyph for the char '{char}'.") return _font
def chapter_parameters_introduction(): ia.seed(1) from imgaug import augmenters as iaa from imgaug import parameters as iap seq = iaa.Sequential([ iaa.GaussianBlur(sigma=iap.Uniform(0.0, 1.0)), iaa.ContrastNormalization( iap.Choice([1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2])), iaa.Affine(rotate=iap.Normal(0.0, 30), translate_px=iap.RandomSign(iap.Poisson(3))), iaa.AddElementwise(iap.Discretize( (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64)), iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.1)) + 1.0) ]) images = np.array([ia.quokka_square(size=(128, 128)) for i in range(16)]) images_aug = [seq.augment_image(images[i]) for i in range(len(images))] save("parameters", "introduction.jpg", grid(images_aug, cols=4, rows=4))
def stochastic(): return iaa.Sequential([ iaa.GaussianBlur( sigma=iap.Uniform(0.0, 1.0) ), iaa.ContrastNormalization( iap.Choice( [1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2] ) ), iaa.Affine( rotate=iap.Normal(0.0, 30), translate_px=iap.RandomSign(iap.Poisson(3)) ), iaa.AddElementwise( iap.Discretize( (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64 ) ), iaa.Multiply( iap.Positive(iap.Normal(0.0, 0.1)) + 1.0 ) ])
def chapter_parameters_special(): ia.seed(1) # ----------------------- # Choice # ----------------------- from imgaug import parameters as iap params = [ iap.Choice([0, 1, 2]), iap.Choice([0, 1, 2], p=[0.15, 0.5, 0.35]), iap.Choice([iap.Normal(-3, 1), iap.Normal(3, 1)]), iap.Choice([iap.Normal(-3, 1), iap.Poisson(3)]) ] gridarr = draw_distributions_grid(params) save("parameters", "special_choice.jpg", gridarr) # ----------------------- # Clip # ----------------------- from imgaug import parameters as iap params = [ iap.Clip(iap.Normal(0, 1), -2, 2), iap.Clip(iap.Normal(0, 1), -2, None) ] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "special_clip.jpg", gridarr) # ----------------------- # Discretize # ----------------------- from imgaug import parameters as iap params = [ iap.Discretize(iap.Normal(0, 1)), iap.Discretize(iap.ChiSquare(3)) ] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "special_discretize.jpg", gridarr) # ----------------------- # Absolute # ----------------------- from imgaug import parameters as iap params = [iap.Absolute(iap.Normal(0, 1)), iap.Absolute(iap.Laplace(0, 1))] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "special_absolute.jpg", gridarr) # ----------------------- # RandomSign # ----------------------- from imgaug import parameters as iap params = [ iap.ChiSquare(3), iap.RandomSign(iap.ChiSquare(3)), iap.RandomSign(iap.ChiSquare(3), p_positive=0.75), iap.RandomSign(iap.ChiSquare(3), p_positive=0.9) ] gridarr = draw_distributions_grid(params) save("parameters", "special_randomsign.jpg", gridarr) # ----------------------- # ForceSign # ----------------------- from imgaug import parameters as iap params = [ iap.ForceSign(iap.Normal(0, 1), positive=True), iap.ChiSquare(3) - 3.0, iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="invert"), iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="reroll") ] gridarr = draw_distributions_grid(params) save("parameters", "special_forcesign.jpg", gridarr)
def chapter_parameters_arithmetic(): ia.seed(1) # ----------------------- # Add # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1) + 1, # identical to: Add(Uniform(0, 1), 1) iap.Add(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])), iap.Normal(0, 1) + iap.Uniform(-5.5, -5) + iap.Uniform(5, 5.5), iap.Normal(0, 1) + iap.Uniform(-7, 5) + iap.Poisson(3), iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True) ] gridarr = draw_distributions_grid( params, rows=2, sample_sizes=[ # (iterations, samples per iteration) (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000), (1, 100000), (1, 100000) ]) save("parameters", "arithmetic_add.jpg", gridarr) # ----------------------- # Multiply # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1) * 2, # identical to: Multiply(Uniform(0, 1), 2) iap.Multiply(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])), (iap.Normal(0, 1) * iap.Uniform(-5.5, -5)) * iap.Uniform(5, 5.5), (iap.Normal(0, 1) * iap.Uniform(-7, 5)) * iap.Poisson(3), iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True) ] gridarr = draw_distributions_grid( params, rows=2, sample_sizes=[ # (iterations, samples per iteration) (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000), (1, 100000), (1, 100000) ]) save("parameters", "arithmetic_multiply.jpg", gridarr) # ----------------------- # Divide # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1) / 2, # identical to: Divide(Uniform(0, 1), 2) iap.Divide(iap.Uniform(0, 1), iap.Choice([0, 2], p=[0.7, 0.3])), (iap.Normal(0, 1) / iap.Uniform(-5.5, -5)) / iap.Uniform(5, 5.5), (iap.Normal(0, 1) * iap.Uniform(-7, 5)) / iap.Poisson(3), iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True) ] gridarr = draw_distributions_grid( params, rows=2, sample_sizes=[ # (iterations, samples per iteration) (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000), (1, 100000), (1, 100000) ]) save("parameters", "arithmetic_divide.jpg", gridarr) # ----------------------- # Power # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1)**2, # identical to: Power(Uniform(0, 1), 2) iap.Clip(iap.Uniform(-1, 1)**iap.Normal(0, 1), -4, 4) ] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "arithmetic_power.jpg", gridarr)
import numpy as np import os import pandas as pd import glob import imgaug as ia import imgaug.augmenters as iaa import imgaug.parameters as iap from imgaug.augmentables.kps import Keypoint, KeypointsOnImage from imgaug.augmenters import Sequential import sys seq = iaa.Sequential( [ iaa.Affine( # rotate=iap.Normal(0.0, 20), translate_px=iap.RandomSign(iap.Poisson(3))), #iaa.Crop(percent=(0, 0.2)), iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.4)) + 0.8), iaa.ContrastNormalization(iap.Uniform(0.5, 1.5)) ], random_order=True) class DataAugmentator(object): """ Generates new images in form of arrays for the given parameters. :param seq: a sequention of different augmenters applied to single augmentatiion call :param landmarks_num: number of image's landmarks :param batch_size: number of images that would be generated during augmentation for single image :param img_dir: directory to images to be augmented :param annotation_dir: directory to images' landmarks