def train(self,featurefiles,k=100,subsampling=10): """ Train a vocabulary from features in files listed in featurefiles using k-means with k number of words. Subsampling of training data can be used for speedup. """ nbr_images = len(featurefiles) # read the features from file descr = [] descr.append(sift1.read_features_from_file(featurefiles[0])[1]) descriptors = descr[0] #stack all features for k-means for i in arange(1,nbr_images): descr.append(sift1.read_features_from_file(featurefiles[i])[1]) descriptors = vstack((descriptors,descr[i])) # k-means: last number determines number of runs self.voc,distortion = kmeans(descriptors[::subsampling,:],k,1) self.nbr_words = self.voc.shape[0] # go through all training images and project on vocabulary imwords = zeros((nbr_images,self.nbr_words)) for i in range( nbr_images ): imwords[i] = self.project(descr[i]) nbr_occurences = sum( (imwords > 0)*1 ,axis=0) self.idf = log( (1.0*nbr_images) / (1.0*nbr_occurences+1) ) self.trainingdata = featurefiles
# index of query image and number of results to return q_ind=0 nbr_results=8 # regular query res_reg = [w[1] for w in src.query(newimlist[q_ind])[:nbr_results]] print 'top matches (regular):',res_reg print newimlist[1] # load image features from query image q_locs,q_descr=sift1.read_features_from_file(featlist[q_ind]) fp=homography.make_homog(q_locs[:,:2].T) # RANSAC model for homography fitting model = homography.RansacModel() rank={} # load image features for result for ndx in res_reg[1:]: print ndx locs,descr = sift1.read_features_from_file(featlist[ndx])
imlist=get_imlist('img') nbr_images = len(imlist) featlist = [ imlist[i][:-3]+'sift' for i in range(nbr_images) ] newimlist=[] for i in imlist: newimlist.append(i[4:]) # load vocabulary with open('vocabulary.pkl','rb') as f: voc=pickle.load(f) # create indexer indx = new_imagesearch.Indexer('test.db',voc) """Uncomment when you need to recreate the database""" #indx.create_tables() """ End""" # Add images to database for i in range(nbr_images): locs,descr=sift1.read_features_from_file(featlist[i]) indx.add_to_index(newimlist[i],descr) indx.db_commit()