<s> from numpy import * <EOL> import pickle <EOL> from pysqlite2 import dbapi2 as sqlite <EOL> class Indexer ( object ) : <EOL> def __init__ ( self , db , voc ) : <EOL> """<STR_LIT>""" <EOL> self . con = sqlite . connect ( db ) <EOL> self . voc = voc <EOL> def __del__ ( self ) : <EOL> self . con . close ( ) <EOL> def db_commit ( self ) : <EOL> self . con . commit ( ) <EOL> def get_id ( self , imname ) : <EOL> """<STR_LIT>""" <EOL> cur = self . con . execute ( <EOL> "<STR_LIT>" % imname ) <EOL> res = cur . fetchone ( ) <EOL> if res == None : <EOL> cur = self . con . execute ( <EOL> "<STR_LIT>" % imname ) <EOL> return cur . lastrowid <EOL> else : <EOL> return res [ <NUM_LIT:0> ] <EOL> def is_indexed ( self , imname ) : <EOL> """<STR_LIT>""" <EOL> im = self . con . execute ( "<STR_LIT>" % imname ) . fetchone ( ) <EOL> return im != None <EOL> def add_to_index ( self , imname , descr ) : <EOL> """<STR_LIT>""" <EOL> if self . is_indexed ( imname ) : return <EOL> print '<STR_LIT>' , imname <EOL> imid = self . get_id ( imname ) <EOL> imwords = self . voc . project ( descr ) <EOL> nbr_words = imwords . shape [ <NUM_LIT:0> ] <EOL> for i in range ( nbr_words ) : <EOL> word = imwords [ i ] <EOL> self . con . execute ( "<STR_LIT>" , ( imid , word , self . voc . name ) ) <EOL> self . con . execute ( "<STR_LIT>" , ( imid , pickle . dumps ( imwords ) , self . voc . name ) ) <EOL> def create_tables ( self ) : <EOL> """<STR_LIT>""" <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . con . execute ( '<STR_LIT>' ) <EOL> self . db_commit ( ) <EOL> class Searcher ( object ) : <EOL> def __init__ ( self , db , voc ) : <EOL> """<STR_LIT>""" <EOL> self . con = sqlite . connect ( db ) <EOL> self . voc = voc <EOL> def __del__ ( self ) : <EOL> self . con . close ( ) <EOL> def get_imhistogram ( self , imname ) : <EOL> """<STR_LIT>""" <EOL> im_id = self . con . execute ( <EOL> "<STR_LIT>" % imname ) . fetchone ( ) <EOL> s = self . con . execute ( <EOL> "<STR_LIT>" % im_id ) . fetchone ( ) <EOL> return pickle . loads ( str ( s [ <NUM_LIT:0> ] ) ) <EOL> def candidates_from_word ( self , imword ) : <EOL> """<STR_LIT>""" <EOL> im_ids = self . con . execute ( <EOL> "<STR_LIT>" % imword ) . fetchall ( ) <EOL> return [ i [ <NUM_LIT:0> ] for i in im_ids ] <EOL> def candidates_from_histogram ( self , imwords ) : <EOL> """<STR_LIT>""" <EOL> words = imwords . nonzero ( ) [ <NUM_LIT:0> ] <EOL> candidates = [ ] <EOL> for word in words : <EOL> c = self . candidates_from_word ( word ) <EOL> candidates += c <EOL> tmp = [ ( w , candidates . count ( w ) ) for w in set ( candidates ) ] <EOL> tmp . sort ( cmp = lambda x , y : cmp ( x [ <NUM_LIT:1> ] , y [ <NUM_LIT:1> ] ) ) <EOL> tmp . reverse ( ) <EOL> return [ w [ <NUM_LIT:0> ] for w in tmp ] <EOL> def query ( self , imname ) : <EOL> """<STR_LIT>""" <EOL> h = self . get_imhistogram ( imname ) <EOL> candidates = self . candidates_from_histogram ( h ) <EOL> matchscores = [ ] <EOL> for imid in candidates : <EOL> cand_name = self . con . execute ( <EOL> "<STR_LIT>" % imid ) . fetchone ( ) <EOL> cand_h = self . get_imhistogram ( cand_name ) <EOL> cand_dist = sqrt ( sum ( self . voc . idf * ( h - cand_h ) ** <NUM_LIT:2> ) ) <EOL> matchscores . append ( ( cand_dist , imid ) ) <EOL> matchscores . sort ( ) <EOL> return matchscores <EOL> def get_filename ( self , imid ) : <EOL> """<STR_LIT>""" <EOL> s = self . con . execute ( <EOL> "<STR_LIT>" % imid ) . fetchone ( ) <EOL> return s [ <NUM_LIT:0> ] <EOL> def tf_idf_dist ( voc , v1 , v2 ) : <EOL> v1 /= sum ( v1 ) <EOL> v2 /= sum ( v2 ) <EOL> return sqrt ( sum ( voc . idf * ( v1 - v2 ) ** <NUM_LIT:2> ) ) <EOL> def compute_ukbench_score ( src , imlist ) : <EOL> """<STR_LIT>""" <EOL> nbr_images = len ( imlist ) <EOL> pos = zeros ( ( nbr_images , <NUM_LIT:4> ) ) <EOL> for i in range ( nbr_images ) : <EOL> pos [ i ] = [ w [ <NUM_LIT:1> ] - <NUM_LIT:1> for w in src . query ( imlist [ i ] ) [ : <NUM_LIT:4> ] ] <EOL> score = array ( [ ( pos [ i ] // <NUM_LIT:4> ) == ( i // <NUM_LIT:4> ) for i in range ( nbr_images ) ] ) * <NUM_LIT:1.0> <EOL> return sum ( score ) / ( nbr_images ) <EOL> from PIL import Image <EOL> from pylab import * <EOL> def plot_results ( src , res ) : <EOL> """<STR_LIT>""" <EOL> figure ( ) <EOL> nbr_results = len ( res ) <EOL> for i in range ( nbr_results ) : <EOL> imname = src . get_filename ( res [ i ] ) <EOL> subplot ( <NUM_LIT:1> , nbr_results , i + <NUM_LIT:1> ) <EOL> imshow ( array ( Image . open ( imname ) ) ) <EOL> axis ( '<STR_LIT>' ) <EOL> show ( ) </s>
