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 save_image_path(imageDir, save_path): 
    #imageDir : src image path
    #save_path : the proto file save path
    if not os.path.exists(imageDir):
        print('ErrorMessage:', imageDir, ' is not exisit!')
        return -1
    
    path_dict = get_path_dict( imageDir )
    rw_feature.save_dict( path_dict, save_path )
Esempio n. 3
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)
Esempio n. 4
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')