<s> import sys <EOL> import os <EOL> import glob <EOL> import heapq <EOL> import math <EOL> import time <EOL> import numpy as np <EOL> import cv2 <EOL> from matplotlib import pylab as plt <EOL> from matplotlib import delaunay as triang <EOL> from scipy . misc import imread <EOL> def xy2ij ( pts ) : <EOL> """<STR_LIT>""" <EOL> pts = np . array ( pts ) <EOL> return pts [ : , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] <EOL> ij2xy = xy2ij <EOL> def conv2 ( im , kernel , mode = '<STR_LIT>' , dst = None ) : <EOL> source = im <EOL> if mode == '<STR_LIT>' : <EOL> additional_rows = kernel . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> additional_cols = kernel . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> source = cv2 . copyMakeBorder ( im , <EOL> ( additional_rows + <NUM_LIT:1> ) / <NUM_LIT:2> , additional_rows / <NUM_LIT:2> , <EOL> ( additional_cols + <NUM_LIT:1> ) / <NUM_LIT:2> , additional_cols / <NUM_LIT:2> , <EOL> cv2 . BORDER_CONSTANT , value = <NUM_LIT:0> ) <EOL> anchor = ( kernel . shape [ <NUM_LIT:1> ] - kernel . shape [ <NUM_LIT:1> ] / <NUM_LIT:2> - <NUM_LIT:1> , <EOL> kernel . shape [ <NUM_LIT:0> ] - kernel . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> - <NUM_LIT:1> ) <EOL> if not dst : <EOL> dst = np . zeros ( im . shape ) <EOL> fk = np . fliplr ( np . flipud ( kernel ) ) . copy ( ) <EOL> dst = cv2 . filter2D ( source , - <NUM_LIT:1> , fk , anchor = anchor , delta = <NUM_LIT:0> , <EOL> borderType = cv2 . BORDER_CONSTANT ) <EOL> if mode == '<STR_LIT>' : <EOL> dst = dst [ ( kernel . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ) / <NUM_LIT:2> : dst . shape [ <NUM_LIT:1> ] - kernel . shape [ <NUM_LIT:1> ] / <NUM_LIT:2> , <EOL> ( kernel . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ) / <NUM_LIT:2> : dst . shape [ <NUM_LIT:0> ] - kernel . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> ] <EOL> return dst <EOL> def implot ( ims , subtitle = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if type ( ims ) == np . ndarray : <EOL> plt . figure ( ) <EOL> plt . imshow ( ims , cmap = plt . cm . gray , interpolation = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> else : <EOL> ims = list ( ims ) <EOL> n = len ( ims ) <EOL> rows = math . floor ( math . sqrt ( n ) ) <EOL> cols = math . ceil ( n / float ( rows ) ) <EOL> plt . figure ( ) <EOL> for i , im in enumerate ( ims ) : <EOL> plt . subplot ( rows , cols , i + <NUM_LIT:1> ) <EOL> plt . imshow ( im , cmap = plt . cm . gray , <EOL> interpolation = '<STR_LIT>' ) <EOL> plt . xticks ( ( ) ) <EOL> plt . yticks ( ( ) ) <EOL> plt . suptitle ( subtitle , fontsize = <NUM_LIT:16> ) <EOL> plt . show ( ) <EOL> def mkfig ( ims , subtitle = '<STR_LIT>' ) : <EOL> fig = plt . figure ( ) <EOL> if type ( ims ) == np . ndarray : <EOL> fig . imshow ( ims , cmap = plt . cm . gray , interpolation = '<STR_LIT>' ) <EOL> else : <EOL> ims = list ( ims ) <EOL> n = len ( ims ) <EOL> rows = math . floor ( math . sqrt ( n ) ) <EOL> cols = math . ceil ( n / float ( rows ) ) <EOL> for i , im in enumerate ( ims ) : <EOL> plt . subplot ( rows , cols , i + <NUM_LIT:1> ) <EOL> plt . imshow ( im , cmap = plt . cm . gray , <EOL> interpolation = '<STR_LIT>' ) <EOL> plt . xticks ( ( ) ) <EOL> plt . yticks ( ( ) ) <EOL> plt . suptitle ( subtitle , fontsize = <NUM_LIT:16> ) <EOL> return fig <EOL> def rect_xy2ij ( rect_xy ) : <EOL> """<STR_LIT>""" <EOL> return np . array ( [ rect_xy [ <NUM_LIT:1> ] , rect_xy [ <NUM_LIT:0> ] , rect_xy [ <NUM_LIT:3> ] , rect_xy [ <NUM_LIT:2> ] ] , <EOL> dtype = rect_xy . dtype ) <EOL> def facedet ( im , cascade = None , cascade_xml = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if len ( im . shape ) != <NUM_LIT:2> : <EOL> im = cv2 . cvtColor ( im , cv2 . COLOR_BGR2GRAY ) <EOL> if not cascade : <EOL> if os . path . exists ( cascade_xml ) : <EOL> cascade = cv2 . CascadeClassifier ( cascade_xml ) <EOL> else : <EOL> raise IOError ( "<STR_LIT>" <EOL> % cascade_xml ) <EOL> im = cv2 . equalizeHist ( im ) <EOL> face_rects_xy = cascade . detectMultiScale ( im ) <EOL> face_rects = np . vstack ( [ rect_xy2ij ( rect_xy ) for rect_xy in face_rects_xy ] ) <EOL> return face_rects <EOL> def draw_landmarks ( im , points , orientation = '<STR_LIT>' ) : <EOL> if orientation == '<STR_LIT>' : <EOL> points = [ pt [ : : - <NUM_LIT:1> ] for pt in points ] <EOL> elif orientation == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % orientation ) <EOL> for pt in points : <EOL> cv2 . circle ( im , tuple ( pt ) , <NUM_LIT:1> , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , - <NUM_LIT:1> ) <EOL> return im <EOL> def plot_landmarks ( im , points , orientation = '<STR_LIT>' ) : <EOL> im = im . copy ( ) <EOL> im = draw_landmarks ( im , points , orientation ) <EOL> implot ( im ) <EOL> def delaunay ( landmarks ) : <EOL> tri = triang . delaunay ( landmarks [ : , <NUM_LIT:0> ] , landmarks [ : , <NUM_LIT:1> ] ) [ <NUM_LIT:2> ] <EOL> return tri <EOL> def draw_tri ( im , lms , tri , copy = True , color = ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> if copy : <EOL> im = im . copy ( ) <EOL> for tr in tri : <EOL> i0 , j0 = lms [ tr [ <NUM_LIT:0> ] ] <EOL> i1 , j1 = lms [ tr [ <NUM_LIT:1> ] ] <EOL> i2 , j2 = lms [ tr [ <NUM_LIT:2> ] ] <EOL> cv2 . line ( im , ( j0 , i0 ) , ( j1 , i1 ) , color , thickness = <NUM_LIT:1> ) <EOL> cv2 . line ( im , ( j1 , i1 ) , ( j2 , i2 ) , color , thickness = <NUM_LIT:1> ) <EOL> cv2 . line ( im , ( j2 , i2 ) , ( j0 , i0 ) , color , thickness = <NUM_LIT:1> ) <EOL> return im <EOL> def _parse_coords ( line ) : <EOL> coords = map ( float , line . strip ( ) . split ( ) ) <EOL> coords = map ( lambda x : x if x > <NUM_LIT:0> else <NUM_LIT:0> , coords ) <EOL> return coords <EOL> def read_landmarks ( path , orientation = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , '<STR_LIT:r>' ) as fin : <EOL> lines = fin . readlines ( ) <EOL> lms = [ _parse_coords ( line ) for line in lines ] <EOL> lms = np . array ( lms ) . astype ( np . uint32 ) <EOL> if orientation == '<STR_LIT>' : <EOL> lms [ : , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] = lms [ : , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] <EOL> return lms <EOL> def write_landmarks ( path , lms , orientation = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if orientation == '<STR_LIT>' : <EOL> lms = lms . copy ( ) <EOL> lms [ : , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] = lms [ : , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] <EOL> with open ( path , '<STR_LIT:w>' ) as fout : <EOL> for lm in lms : <EOL> fout . write ( '<STR_LIT>' % ( lm [ <NUM_LIT:0> ] , lm [ <NUM_LIT:1> ] ) ) <EOL> def read_label ( path ) : <EOL> with open ( path ) as fin : <EOL> return int ( float ( fin . read ( ) . strip ( ) ) ) <EOL> def move_landmarks ( landmarks , new_origin ) : <EOL> """<STR_LIT>""" <EOL> oi , oj = new_origin <EOL> return np . array ( [ [ lm [ <NUM_LIT:0> ] - oi , lm [ <NUM_LIT:1> ] - oj ] for lm in landmarks ] ) <EOL> def get_patch ( im , shape ) : <EOL> """<STR_LIT>""" <EOL> h , w = shape <EOL> max_i = im . shape [ <NUM_LIT:0> ] - h <EOL> max_j = im . shape [ <NUM_LIT:1> ] - w <EOL> i = np . random . randint ( <NUM_LIT:0> , max_i ) <EOL> j = np . random . randint ( <NUM_LIT:0> , max_j ) <EOL> return im [ i : i + h , j : j + w ] <EOL> def standartize ( im ) : <EOL> if im . ndim == <NUM_LIT:3> : <EOL> im = cv2 . cvtColor ( im , cv2 . COLOR_BGR2GRAY ) <EOL> im = cv2 . equalizeHist ( im ) <EOL> im = cv2 . resize ( im , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> return im <EOL> def standartize_landmarks ( lms , original_shape ) : <EOL> oh , ow = original_shape <EOL> h , w = ( <NUM_LIT> , <NUM_LIT> ) <EOL> lms = lms . copy ( ) <EOL> lms [ : , <NUM_LIT:0> ] = lms [ : , <NUM_LIT:0> ] * h / oh <EOL> lms [ : , <NUM_LIT:1> ] = lms [ : , <NUM_LIT:1> ] * w / ow <EOL> return lms </s>
