def class_image_enhance( 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, ' is not exisit!')
        return -1
    image_path_dict = rw_feature.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, ' is not exisit!')
        return -1
    feature_path_dict = rw_feature.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' )
    if os.path.exists(iif_save_path):
        index_image_feature_path_dict = rw_feature.read_dict(iif_save_path)
    else:
        index_image_feature_path_dict = dict()

    #获取所以特征的key,即所以特征的图像名称list
    key_list = feature_path_dict.keys()

    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])

        cp_flag = 0  #复制标记,当标记为1时候证明已经复制,如果循环结束还是0, 则证明索引目录中没有符合的文件夹,需要新创建文件夹
        for ind_key in index_image_feature_path_dict.keys():
            _, one_kp, one_des = read_des_feature_by_path(feature_path_dict[ind_key])
            ransanc_gmatch, gmatch = match(first_kp, first_des.astype(np.float32),one_kp, one_des.astype(np.float32))
            if svm_clf.predict([[float( len(ransanc_gmatch)), float( len(gmatch))]]):
                img_name, _ = os.path.splitext(ind_key)
                cfd_folder_name = os.path.join( image_class_floder, img_name )  
                shutil.copyfile( image_path_dict[first_key], os.path.join(cfd_folder_name, first_key) )
                cp_flag = 1
                break
        if not cp_flag:
            img_name, _ = os.path.splitext(first_key)
            cfd_folder_name = os.path.join( image_class_floder, img_name )
            os.mkdir( cfd_folder_name )
            shutil.copyfile( image_path_dict[first_key], os.path.join(cfd_folder_name, first_key) )
            index_image_feature_path_dict[first_key] = feature_path_dict[first_key]
    
    #将索引特征路径dict保存
    rw_feature.save_dict(index_image_feature_path_dict, iif_save_path)
Esempio n. 2
0
def demo():
    src_image_feature_path = 'wine_images/src_image_feature_path.path'

    feature_path_dict = rw_feature.read_dict(src_image_feature_path)

    key_list = feature_path_dict.keys()

    img_name, img_kp, img_des = rw_feature.read_feature(
        feature_path_dict[key_list[0]].strip())
    print(img_name)
    img_name2, img_kp2, img_des2 = rw_feature.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)
Esempio n. 3
0
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_feature.read_dict( protofile_path )

    return image_path_dict
Esempio n. 4
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 = rwOperation.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():
        one_image_feature_path = path_dict[key]
        _, _, img_des = rwOperation.read_feature(one_image_feature_path)
        v = VLAD.VLAD(img_des, visualDictionary)
        descriptors_dict[key] = v
    rwOperation.save_dict_des(
        descriptors_dict,
        os.path.join(src_image_feature_path, 'descriptors_dict.vlad'))
Esempio n. 5
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 = rwOperation.read_dict(image_feature_path)
    else:
        print('have no src_image_feature_path.path to read!')

    descriptors = list()

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

    descriptors = np.asarray(descriptors)
    return descriptors
Esempio n. 6
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 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_feature.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_feature.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_feature.save_dict(image_feature_path_dict,
                                 image_feature_path_file)

            config.set('MetaData', 'image_idx', str(image_idx - 1))
            with open('config.ini', 'wb') as configfile:
                config.write(configfile)
import cnn_search
import feature_extract
import feature_match
from RWoperation import rwOperation

name_list, feature_train = cnn_search.load_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 = rwOperation.read_dict(image_feature_path)

surf = feature_extract.create_detector('surf')

st_time = time.time()
one_feature = cnn_search.one_extract_feature(net, image_path)
print(one_feature)
print(type(one_feature))
one_feature = one_feature[np.newaxis, :]

res = sf.knnMatch(one_feature, feature_train, k=10)
print('spend time :', time.time() - st_time)
#print(res )
dest_name_list = []
for idx in res[0]:
    dest_name_list.append(name_list[idx.trainIdx])
Esempio n. 8
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_feature.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_feature.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()
    key_list_path = config.get('MetaData', 'key_list_path')

    break_from_flag = int(config.get('MetaData', 'break_from_flag'))
    if break_from_flag:
        index_image_feature_path_dict = rw_feature.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()

    #测试
    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_feature.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('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')