<s> from __future__ import division <EOL> from functools import wraps <EOL> import numpy as np <EOL> from menpo . image import Image , MaskedImage , BooleanImage <EOL> from menpo . transform import Translation , NonUniformScale <EOL> def lm_centres_correction ( centres ) : <EOL> r"""<STR_LIT>""" <EOL> t = Translation ( - centres . min ( axis = <NUM_LIT:0> ) . min ( axis = <NUM_LIT:0> ) , skip_checks = True ) <EOL> step_v = centres [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if centres . shape [ <NUM_LIT:0> ] > <NUM_LIT:1> : <EOL> step_v = centres [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] - centres [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> step_h = centres [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> if centres . shape [ <NUM_LIT:1> ] > <NUM_LIT:1> : <EOL> step_h = centres [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] - centres [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> s = NonUniformScale ( ( <NUM_LIT:1.> / step_v , <NUM_LIT:1.> / step_h ) , skip_checks = True ) <EOL> return t . compose_before ( s ) <EOL> def sample_mask_for_centres ( mask , centres ) : <EOL> r"""<STR_LIT>""" <EOL> return BooleanImage ( mask [ centres [ ... , <NUM_LIT:0> ] , centres [ ... , <NUM_LIT:1> ] ] , copy = False ) <EOL> def rebuild_feature_image ( image , f_pixels ) : <EOL> shape_changed = f_pixels . shape [ <NUM_LIT:1> : ] != image . shape <EOL> if hasattr ( image , '<STR_LIT>' ) : <EOL> if shape_changed : <EOL> mask = image . mask . resize ( f_pixels . shape [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> mask = image . mask . copy ( ) <EOL> new_image = MaskedImage ( f_pixels , mask = mask , copy = False ) <EOL> else : <EOL> new_image = Image ( f_pixels , copy = False ) <EOL> if image . has_landmarks : <EOL> if shape_changed : <EOL> sf = np . array ( f_pixels . shape [ <NUM_LIT:1> : ] ) / np . array ( image . shape ) <EOL> new_image . landmarks = NonUniformScale ( sf ) . apply ( image . landmarks ) <EOL> else : <EOL> new_image . landmarks = image . landmarks <EOL> return new_image <EOL> def rebuild_feature_image_with_centres ( image , f_pixels , centres ) : <EOL> if hasattr ( image , '<STR_LIT>' ) : <EOL> mask = sample_mask_for_centres ( image . mask . mask , centres ) <EOL> new_image = MaskedImage ( f_pixels , mask = mask , copy = False ) <EOL> else : <EOL> new_image = Image ( f_pixels , copy = False ) <EOL> if image . has_landmarks : <EOL> t = lm_centres_correction ( centres ) <EOL> new_image . landmarks = t . apply ( image . landmarks ) <EOL> return new_image <EOL> def imgfeature ( wrapped ) : <EOL> @ wraps ( wrapped ) <EOL> def wrapper ( image , * args , ** kwargs ) : <EOL> if isinstance ( image , np . ndarray ) : <EOL> image = Image ( image , copy = False ) <EOL> return wrapped ( image , * args , ** kwargs ) . pixels <EOL> else : <EOL> return wrapped ( image , * args , ** kwargs ) <EOL> return wrapper <EOL> def ndfeature ( wrapped ) : <EOL> @ wraps ( wrapped ) <EOL> def wrapper ( image , * args , ** kwargs ) : <EOL> if not isinstance ( image , np . ndarray ) : <EOL> feature = wrapped ( image . pixels , * args , ** kwargs ) <EOL> return rebuild_feature_image ( image , feature ) <EOL> else : <EOL> return wrapped ( image , * args , ** kwargs ) <EOL> return wrapper <EOL> def winitfeature ( wrapped ) : <EOL> @ wraps ( wrapped ) <EOL> def wrapper ( image , * args , ** kwargs ) : <EOL> if not isinstance ( image , np . ndarray ) : <EOL> feature , centres = wrapped ( image . pixels , * args , ** kwargs ) <EOL> return rebuild_feature_image_with_centres ( image , feature , centres ) <EOL> else : <EOL> return wrapped ( image , * args , ** kwargs ) [ <NUM_LIT:0> ] <EOL> return wrapper </s>
