<s> from . base import ImageAugmentor <EOL> from ... utils . rect import Rect <EOL> from six . moves import range <EOL> import numpy as np <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class RandomCrop ( ImageAugmentor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , crop_shape ) : <EOL> """<STR_LIT>""" <EOL> self . _init ( locals ( ) ) <EOL> def _augment ( self , img ) : <EOL> orig_shape = img . arr . shape <EOL> h0 = self . rng . randint ( <NUM_LIT:0> , orig_shape [ <NUM_LIT:0> ] - self . crop_shape [ <NUM_LIT:0> ] ) <EOL> w0 = self . rng . randint ( <NUM_LIT:0> , orig_shape [ <NUM_LIT:1> ] - self . crop_shape [ <NUM_LIT:1> ] ) <EOL> img . arr = img . arr [ h0 : h0 + self . crop_shape [ <NUM_LIT:0> ] , w0 : w0 + self . crop_shape [ <NUM_LIT:1> ] ] <EOL> if img . coords : <EOL> raise NotImplementedError ( ) <EOL> class CenterCrop ( ImageAugmentor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , crop_shape ) : <EOL> """<STR_LIT>""" <EOL> self . _init ( locals ( ) ) <EOL> def _augment ( self , img ) : <EOL> orig_shape = img . arr . shape <EOL> h0 = int ( ( orig_shape [ <NUM_LIT:0> ] - self . crop_shape [ <NUM_LIT:0> ] ) * <NUM_LIT:0.5> ) <EOL> w0 = int ( ( orig_shape [ <NUM_LIT:1> ] - self . crop_shape [ <NUM_LIT:1> ] ) * <NUM_LIT:0.5> ) <EOL> img . arr = img . arr [ h0 : h0 + self . crop_shape [ <NUM_LIT:0> ] , w0 : w0 + self . crop_shape [ <NUM_LIT:1> ] ] <EOL> if img . coords : <EOL> raise NotImplementedError ( ) <EOL> class FixedCrop ( ImageAugmentor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rect ) : <EOL> """<STR_LIT>""" <EOL> self . _init ( locals ( ) ) <EOL> def _augment ( self , img ) : <EOL> orig_shape = img . arr . shape <EOL> img . arr = img . arr [ self . rect . y0 : self . rect . y1 + <NUM_LIT:1> , <EOL> self . rect . x0 : self . rect . x0 + <NUM_LIT:1> ] <EOL> if img . coords : <EOL> raise NotImplementedError ( ) <EOL> def perturb_BB ( image_shape , bb , max_pertub_pixel , <EOL> rng = None , max_aspect_ratio_diff = <NUM_LIT> , <EOL> max_try = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> orig_ratio = bb . h * <NUM_LIT:1.0> / bb . w <EOL> if rng is None : <EOL> rng = np . random . RandomState ( ) <EOL> for _ in range ( max_try ) : <EOL> p = rng . randint ( - max_pertub_pixel , max_pertub_pixel , [ <NUM_LIT:4> ] ) <EOL> newbb = bb . copy ( ) <EOL> newbb . x += p [ <NUM_LIT:0> ] <EOL> newbb . y += p [ <NUM_LIT:1> ] <EOL> newx1 = bb . x1 + p [ <NUM_LIT:2> ] <EOL> newy1 = bb . y1 + p [ <NUM_LIT:3> ] <EOL> newbb . w = newx1 - newbb . x <EOL> newbb . h = newy1 - newbb . y <EOL> if not newbb . validate ( image_shape ) : <EOL> continue <EOL> new_ratio = newbb . h * <NUM_LIT:1.0> / newbb . w <EOL> diff = abs ( new_ratio - orig_ratio ) <EOL> if diff / orig_ratio > max_aspect_ratio_diff : <EOL> continue <EOL> return newbb <EOL> return bb <EOL> class RandomCropRandomShape ( ImageAugmentor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , perturb_ratio , max_aspect_ratio_diff = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . _init ( locals ( ) ) <EOL> def _augment ( self , img ) : <EOL> shape = img . arr . shape [ : <NUM_LIT:2> ] <EOL> box = Rect ( <NUM_LIT:0> , <NUM_LIT:0> , shape [ <NUM_LIT:1> ] - <NUM_LIT:1> , shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> dist = self . perturb_ratio * np . sqrt ( shape [ <NUM_LIT:0> ] * shape [ <NUM_LIT:1> ] ) <EOL> newbox = perturb_BB ( shape , box , dist , <EOL> self . rng , self . max_aspect_ratio_diff ) <EOL> img . arr = newbox . roi ( img . arr ) <EOL> if img . coords : <EOL> raise NotImplementedError ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print ( perturb_BB ( [ <NUM_LIT:100> , <NUM_LIT:100> ] , Rect ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:50> , <NUM_LIT:50> ) , <NUM_LIT:50> ) ) </s>
