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 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 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 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_continuous(): ia.seed(1) # ----------------------- # Normal # ----------------------- from imgaug import parameters as iap params = [ iap.Normal(0, 1), iap.Normal(5, 3), iap.Normal(iap.Choice([-3, 3]), 1), iap.Normal(iap.Uniform(-3, 3), 1) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_normal.jpg", gridarr) # ----------------------- # Laplace # ----------------------- from imgaug import parameters as iap params = [ iap.Laplace(0, 1), iap.Laplace(5, 3), iap.Laplace(iap.Choice([-3, 3]), 1), iap.Laplace(iap.Uniform(-3, 3), 1) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_laplace.jpg", gridarr) # ----------------------- # ChiSquare # ----------------------- from imgaug import parameters as iap params = [ iap.ChiSquare(1), iap.ChiSquare(3), iap.ChiSquare(iap.Choice([1, 5])), iap.RandomSign(iap.ChiSquare(3)) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_chisquare.jpg", gridarr) # ----------------------- # Weibull # ----------------------- from imgaug import parameters as iap params = [ iap.Weibull(0.5), iap.Weibull(1), iap.Weibull(1.5), iap.Weibull((0.5, 1.5)) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_weibull.jpg", gridarr) # ----------------------- # Uniform # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1), iap.Uniform(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Uniform([-1, 0], 1), iap.Uniform((-1, 0), 1) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_uniform.jpg", gridarr) # ----------------------- # Beta # ----------------------- from imgaug import parameters as iap params = [ iap.Beta(0.5, 0.5), iap.Beta(2.0, 2.0), iap.Beta(1.0, 0.5), iap.Beta(0.5, 1.0) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_beta.jpg", gridarr)
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 _enhance_parameter(level): fparam = _float_parameter(level, 0.9) return iap.Clip( iap.Add(1.0, iap.RandomSign(fparam), elementwise=True), 0.1, 1.9)
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