def extract_features(params):

    Train_or_Val_dic = {}
    
    with open(os.path.join(params['root'],params['root_save'],params['image_lists'],params['split'] + '.txt'),'r') as f:
        image_list = f.readlines()
    
   
    #Train_files= os.listdir(r'C:\Users\Albert\Documents\UNI\Q-5\GDSA\Projecte\TerrassaBuildings900\Train\images')
    #Val_files= os.listdir(r'C:\Users\Albert\Documents\UNI\Q-5\GDSA\Projecte\TerrassaBuildings900\Val\images')
    
    descriptors = []
    
    if params['split'] == 'train':
        
        for img in image_list:
            im = cv2.imread(os.path.join(params['root'],params['database'],params['split'],'images',img.rstrip()))
            
            # Resize image
            im = resize_image(params,im)
        
            des=get_local_features(params,im)
            
            if len(descriptors) == 0:
                descriptors = des
            else:
                descriptors = np.vstack((descriptors,des))
            
        code=train_codebook(params,descriptors)
        
        # Save to disk
        pickle.dump(code,open(os.path.join(params['root'],params['root_save'],
                                        params['codebooks_dir'],'codebook_'
                                        + str(params['descriptor_size']) + "_"
                                        + params['descriptor_type']
                                        + "_" + params['keypoint_type'] + '.cb'),'wb'))
    
    # Get trained codebook
    code = pickle.load(open(os.path.join(params['root'],params['root_save'],
                                     params['codebooks_dir'],'codebook_'
                                     + str(params['descriptor_size']) + "_"
                                     + params['descriptor_type']
                                     + "_" + params['keypoint_type'] + '.cb'),'rb'))
        
    for img in image_list:
        
        im = cv2.imread(os.path.join(params['root'],params['database'],params['split'],'images',img.rstrip()))
        # Resize image
        im = resize_image(params,im)
        des=get_local_features(params,im)
        assign=get_assignments(code,des)
        Train_or_Val_dic[img] = build_bow(assign,code)
        
    # Save dictionary to disk with unique name
    save_file = os.path.join(params['root'],params['root_save'],params['feats_dir'],
                             params['split'] + "_" + str(params['descriptor_size']) + "_"
                             + params['descriptor_type'] + "_" + params['keypoint_type'] + '.p')

    pickle.dump(Train_or_Val_dic,open(save_file,'wb'))
Beispiel #2
0
def codebook(params):

    
    with open(os.path.join(params['root'],params['root_save'],params['image_lists'],'train.txt'),'r') as f:
        train_image_list = f.readlines()
    
    with open(os.path.join(params['root'],params['root_save'],params['image_lists'], 'val.txt'),'r') as f:
        val_image_list = f.readlines()
    
     

    descriptors = []
    
    #if params['split'] == 'train':
        
    for img in train_image_list:
        im = cv2.imread(os.path.join(params['root'],params['database'],'train','images',img.rstrip()))
        
        # Resize image
        im = resize_image(params,im)
    
        des=get_local_features(params,im)
        
        if len(descriptors) == 0:
            descriptors = des
        else:
            descriptors = np.vstack((descriptors,des))
            
    print "descriptors train: "
    print np.shape(descriptors)
    for img in val_image_list:
        im = cv2.imread(os.path.join(params['root'],params['database'],'val','images',img.rstrip()))
        
        # Resize image
        im = resize_image(params,im)
    
        des=get_local_features(params,im)
        
        if len(descriptors) == 0:
            descriptors = des
        else:
            descriptors = np.vstack((descriptors,des))

    print "descriptors train + val: "
    print np.shape(descriptors)
    code=train_codebook(params,descriptors)
        
        # Save to disk
    pickle.dump(code,open(os.path.join(params['root'],params['root_save'],
                                        params['codebooks_dir'],'codebook_train_val'
                                        + str(params['descriptor_size']) + "_"
                                        + params['descriptor_type']
                                        + "_" + params['keypoint_type'] + '.cb'),'wb'))
def extract_features(params):

    feats_dic = {}
    
    with open(os.path.join(params['root'],params['root_save'],params['image_lists'],params['split'] + '.txt'),'r') as f:
        image_list = f.readlines()


    # Get trained codebook
    code = pickle.load(open(os.path.join(params['root'],params['root_save'],
                                     params['codebooks_dir'],'codebook_train_val_'
                                     + str(params['descriptor_size']) + "_"
                                     + params['descriptor_type']
                                     + "_" + params['keypoint_type'] + '.cb'),'rb'))
        
    for img in image_list:
        
        im = cv2.imread(os.path.join(params['root'],params['database'],params['split'],'images',img.rstrip()))
        im = resize_image(params,im)
        des = get_local_features(params,im)
        assign = get_assignments(code,des)
        feats_dic[img] = build_bow(assign,code)
        
    # Save dictionary to disk with unique name
    save_file = os.path.join(params['root'],params['root_save'],params['feats_dir'],
                             params['split'] + "_" + str(params['descriptor_size']) + "_"
                             + params['descriptor_type'] + "_" + params['keypoint_type'] + '.p')

    pickle.dump(feats_dic,open(save_file,'wb'))