Beispiel #1
0
def demo():
    src_image_feature_path = 'wine_images/src_image_feature_path.path'

    feature_path_dict = rw.read_dict(src_image_feature_path)

    key_list = feature_path_dict.keys()


    img_name, img_kp, img_des = rw.read_feature(feature_path_dict[key_list[0]].strip())
    print( img_name )
    img_name2, img_kp2, img_des2 = rw.read_feature(feature_path_dict[key_list[100]].strip())
    print( img_name2)
    r_good_match, good_match = match(img_kp, img_des.astype(np.float32),img_kp2, img_des2.astype(np.float32), 0.75, 'bf')
    print(len(good_match))

    # MIN_MATCH_COUNT = 10
    # ransanc_good_match = []
    # if len(good_match) > MIN_MATCH_COUNT:
    #     # 获取关键点的坐标
    #     src_pts = np.float32([ img_kp[m[0].queryIdx].pt for m in good_match ]).reshape(-1,1,2)
    #     dst_pts = np.float32([ img_kp2[m[0].trainIdx].pt for m in good_match ]).reshape(-1,1,2)

    #     H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
    # for i in range(len(mask)):
    #     if mask[i]:
    #         ransanc_good_match.append(good_match[i])
    # print( 'ransanc_good_match:', len( ransanc_good_match))

    print( len( r_good_match ) )

    print( img_des.shape )
Beispiel #2
0
def demo2():
    # test for save_dict and read_dict function
    pdict = dict()
    pdict['a'] = 'b'
    pdict[''] = 'd'

    save_dict_path = './test/key_value.path'
    rwOperate.save_dict(pdict, save_dict_path)
    dd = rwOperate.read_dict(save_dict_path)
    print(dd)
def read_image_path( protofile_path ):
    #read data from the protofile
    #protofile_path: the protofile path
    
    if not os.path.exists( protofile_path):
        print('ErrorMessage:', protofile_path, ' is not exisit !!!')
        return -1
    image_path_dict = rw.read_dict( protofile_path )

    return image_path_dict
Beispiel #4
0
def getDescriptors(src_image_feature_path):
    #read image feature for
    #path = '../datafolder/test'
    image_feature_path = os.path.join(src_image_feature_path,
                                      'src_image_feature_path.path')
    if os.path.exists(image_feature_path):
        path_dict = rwOperate.read_dict(image_feature_path)
    else:
        print('have no src_image_feature_path.path to read!',
              image_feature_path)
        return -1

    descriptors = list()

    for key in path_dict.keys():
        one_image_feature_path = path_dict[key]
        _, _, img_des = rwOperate.read_feature(one_image_feature_path)
        descriptors.extend(img_des.tolist())

    descriptors = np.asarray(descriptors)
    return descriptors
Beispiel #5
0
def save_VLAD_to_proto(src_image_feature_path, visualDictionary):
    image_feature_path = os.path.join(src_image_feature_path,
                                      'src_image_feature_path.path')
    if os.path.exists(image_feature_path):
        path_dict = rwOperate.read_dict(image_feature_path)
    else:
        print('have no src_image_feature_path.path to read!')

    descriptors_dict = dict()

    for key in path_dict.keys():
        try:
            one_image_feature_path = path_dict[key]
            _, _, img_des = rwOperate.read_feature(one_image_feature_path)
            v = VLAD.VLAD(img_des, visualDictionary)
            descriptors_dict[key] = v
        except:
            print('extract vlad feature failure')
            continue
    rwOperate.save_dict_des(
        descriptors_dict,
        os.path.join(src_image_feature_path, 'descriptors_dict.vlad'))
Beispiel #6
0
def class_image_slow( imageDir ):
    #特征读取一个一个读取,运行速度相对慢些,但是占用电脑资源更少
    #读取原始图像路径, image_path_dict记录原始图像路径,如 ‘a.jpg', '/root/kenneth/a.jpg'格式 
    src_image_path = os.path.join( imageDir, 'src_image_path.path')
    if not os.path.exists(src_image_path):
        print('ErrorMessage:', src_image_path, 'the image path protofile is not exisit!')
        return -1
    image_path_dict = rw.read_dict(src_image_path)

    #获取原始图像特征路径, feature_path_dict记录图像特征路径, 如 ’a.jpg':‘/root/kenneth/a.surf'
    src_image_feature_path = os.path.join( imageDir, 'src_image_feature_path.path')
    if not os.path.exists(src_image_feature_path):
        print('ErrorMessage:', src_image_feature_path, 'the image feature save path protofile is not exisit!')
        return -1
    feature_path_dict = rw.read_dict(src_image_feature_path)

    #查看image_class_floder 文件夹是否存在,如果不存在创建一个
    image_class_floder = os.path.join( imageDir, 'image_class_folder' )
    if not os.path.exists( image_class_floder ):
        os.mkdir( image_class_floder )
    
#   获取索引特征字典,如果存在读取,如果不存在创建一个dict,用于后面存储特征索引,
    iif_save_path = os.path.join( imageDir, 'index_image_feature.path' )

    index_image_feature_path_dict = dict()
    
    #从特征文件夹中获取所以的kp和des数据,并以字典方式保存,key是图像名称
#    kp_dict, des_dict = read_des_feature(feature_path_dict)

    key_list = list()
    
    break_from_flag = int( config.get('MetaData', 'break_from_flag'))
    if break_from_flag: 
        key_list_path = config.get('MetaData', 'key_list_path')
        index_image_feature_path_dict = rw.read_dict(iif_save_path)

        with open(key_list_path, 'r') as f:
            temp_key = f.readline().strip()
            while temp_key:
                key_list.append(temp_key)
                temp_key = f.readline().strip()
        break_from_flag = 0
    else:
        #获取所以特征的key,即所以特征的图像名称list
        key_list = feature_path_dict.keys()
        key_list_path = os.path.join( imageDir, 'key_list.txt' )

    #测试
    if test_flag:
        print(key_list)
        data_ana = []  #测试时候使用 

    svm_clf = ana_svm()
#    res = svm_clf.predict( [[5.5, 1.7]] )


    #将图像分类、并拷贝一份到image_class_floder文件夹中,其中的子文件夹是以first_key的名称命名,即用来比对的图像名称
    while key_list:

        first_key = key_list.pop(0)
        _, first_kp, first_des = read_des_feature_by_path(feature_path_dict[first_key])
#        first_kp = kp_dict[first_key]
        first_des = first_des.astype(np.float32)

        #将索引特征路径加入index_image_feature.path中
        index_image_feature_path_dict[first_key] = feature_path_dict[first_key]

        #创建分类图像文件夹,以第一图像名称命名文件夹,并将第一图像拷贝到该文件夹中,如果已经存在则报错
        img_name, _ = os.path.splitext(first_key)
        cfd_folder_name = os.path.join( image_class_floder, img_name )
        if not os.path.exists(cfd_folder_name):
            os.mkdir(cfd_folder_name)
            shutil.copyfile(image_path_dict[first_key], os.path.join(cfd_folder_name, first_key))
        else:
            print('warnning:', cfd_folder_name, 'is already exisit!')
        
        for i, key in enumerate(key_list):
            _, one_kp, one_des = read_des_feature_by_path(feature_path_dict[key])
            ransanc_gmatch, gmatch = match(first_kp, first_des,one_kp, one_des.astype(np.float32))

#            if len(ransanc_gmatch) > 30 and len(ransanc_gmatch)/float(len(gmatch)) > 0.4:
            if svm_clf.predict([[float( len(ransanc_gmatch)), float( len(gmatch))]]):
                shutil.copyfile( image_path_dict[key], os.path.join(cfd_folder_name, key) )

                #测试
                if test_flag:
                    data_ana.append([len(ransanc_gmatch), len(gmatch), len(ransanc_gmatch)/float(len(gmatch)), first_key, key_list[i], 1])
                    
                key_list[i] = 0
            else:
                #测试
                if test_flag:
                    data_ana.append([len(ransanc_gmatch), len(gmatch), len(ransanc_gmatch)/float(len(gmatch)), first_key, key_list[i], 0])
        key_list = [elem for elem in key_list if elem is not 0]


        rw.save_dict(index_image_feature_path_dict, iif_save_path)
            
        with open(key_list_path, 'w') as f:
            for one_key in key_list:
                f.writelines(one_key+'\n')
            

    #测试时候保存数据,用于分析
    if test_flag:
        
        with open(os.path.join(current_path,'data_ana.txt'), 'w') as f:
            for one_data in data_ana:
                txt = ''
                for elem in one_data:
                    if isinstance(elem, int) or isinstance( elem, float):
                        elem = str(elem)
                    txt += elem +'\t'
                f.writelines(txt+'\n')
Beispiel #7
0
from FeatureExtract import feature_extract
from Classification import feature_match
from IOoperate import rwOperate

name_list, feature_train = cnn_search.load_feature(
    '../datafolder/com/image_cnn_dict.feature')

image_path = '../datafolder/demo.jpg'

net = cnn_search.load_net()

sf = cv2.BFMatcher()
svm_clf = feature_match.ana_svm()

image_feature_path = '../datafolder/test/src_image_feature_path.path'
feature_path_dict = rwOperate.read_dict(image_feature_path)

surf = feature_extract.create_detector('surf')

test_image_path = '/home/kenneth/ckwork/image_search/original_wine_images/'
test_image_list = os.listdir(test_image_path)

search_time = []
search_result = []

for image_name in test_image_list:
    image_path = os.path.join(test_image_path, image_name)
    st_time = time.time()
    one_feature = cnn_search.one_extract_feature(net, image_path)
    print(one_feature)
    print(type(one_feature))
Beispiel #8
0
def save_feature(proto_path='', save_root_path=''):
    # save feature proto and feature path proto
    #proto_path : image path dict, proto file
    # save_root_path : save root folder, the subfolder will be create

    if not proto_path or not os.path.exists(proto_path):
        proto_path = config.get('PATH', 'src_image_path')
        print(
            'warnning: src image proto path is not exists, will use the default parameter which defined in the fe_config.ini'
        )

    if not save_root_path or not os.path.exists(save_root_path):
        save_root_path = config.get('PATH', 'save_root_path')
        print(
            'warnning: save root path is not exists, will use the default parameter defined in the fe_config.ini instead'
        )

    if not os.path.exists(proto_path):
        print('ErrorMessage:', proto_path, ' is not exisit!!!')
        return -1

    if not os.path.isdir(save_root_path):
        os.makedirs(save_root_path)

    image_feature_folder = os.path.join(save_root_path, 'image_feature_folder')
    if os.path.isdir(image_feature_folder):
        print('Warnning:', image_feature_folder,
              ' is exisit, please change a new path')
    else:
        os.makedirs(image_feature_folder)

    image_path_dict = image_path.read_image_path(proto_path)

    surf_detector = create_detector()

    image_feature_path_file = os.path.join(save_root_path,
                                           'src_image_feature_path.path')

    if not os.path.exists(image_feature_path_file):
        image_feature_path_dict = dict()
    else:
        image_feature_path_dict = rw.read_dict(image_feature_path_file)

    image_idx = int(config.get('MetaData', 'image_idx'))  #从中断处加载,继续处理
    save_interval = int(config.get('MetaData', 'save_interval'))

    for k, img_path_key in enumerate(image_path_dict.keys()[image_idx:]):

        try:
            image_idx += 1
            img_path = image_path_dict[img_path_key]
            img = cv2.imread(img_path)
            img = cv2.resize(img, (360, 360))

        except:
            print('wrong read!')
            continue
        kp, des = detect(surf_detector, img)
        if not kp:
            continue
        _, tmpfilename = os.path.split(img_path)
        filename, _ = os.path.splitext(tmpfilename)

        one_image_feature_path = os.path.join(image_feature_folder,
                                              filename + '.surf')
        rw.save_feature(tmpfilename, kp, des, one_image_feature_path)
        image_feature_path_dict[img_path_key] = one_image_feature_path
        if (not k % save_interval) or image_idx == len(image_path_dict.keys()):
            rw.save_dict(image_feature_path_dict, image_feature_path_file)

            config.set('MetaData', 'image_idx', str(image_idx - 1))
            with open(config_path, 'wb') as configfile:
                config.write(configfile)