create_sift_dataset.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 """
00004 Create sift feature dataset from assigned images.
00005 
00006 Input:
00007 
00008     container_folder/
00009         category_1/
00010             file_1
00011             file_2
00012             ...
00013             file_42
00014         category_2/
00015             file_43
00016             file_44
00017             ...
00018 
00019 Output:
00020 
00021     container_folder_sift_feature.pkl.gz
00022 
00023 """
00024 
00025 import argparse
00026 import gzip
00027 import os
00028 import pickle
00029 
00030 import cv2
00031 import imagesift
00032 import numpy as np
00033 from sklearn.datasets.base import Bunch
00034 from sklearn.datasets import load_files
00035 
00036 
00037 def create_sift_dataset():
00038     parser = argparse.ArgumentParser()
00039     parser.add_argument('container_path', help='image data container path')
00040     parser.add_argument('-O', '--output', default=None, help='output file')
00041     args = parser.parse_args()
00042 
00043     container_path = args.container_path
00044     output = (args.output or
00045               os.path.basename(container_path) + '_sift_feature.pkl.gz')
00046 
00047     # See: http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_files.html
00048     bunch_files = load_files(container_path=container_path,
00049                              description='image data',
00050                              shuffle=False,
00051                              load_content=False)
00052 
00053     targets, pos_list, scale_list, ori_list, desc_list = [], [], [], [], []
00054     for i, (filename, target) in enumerate(zip(bunch_files.filenames,
00055                                                bunch_files.target)):
00056         print('filename: {}, label: {}'.format(filename, target))
00057         targets.append(target)
00058         # extract feature
00059         img = cv2.imread(filename, 0)
00060         frames, desc = imagesift.get_sift_keypoints(img)
00061         # save feature data
00062         pos_list.append(np.hstack([frames[:, 0], frames[:, 1]]))
00063         ori_list.append(frames[:, 2])
00064         scale_list.append(frames[:, 3])
00065         desc_list.append(desc)
00066 
00067     dataset = Bunch(target=np.array(targets),
00068                     target_names=bunch_files.target_names,
00069                     positions=pos_list,
00070                     scales=scale_list,
00071                     orientations=ori_list,
00072                     descriptors=desc_list)
00073 
00074     # save features
00075     print('saving sift feature dataset')
00076     with gzip.open(output, 'wb') as f:
00077         pickle.dump(dataset, f)
00078 
00079 
00080 if __name__ == '__main__':
00081     create_sift_dataset()


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Tue Jul 2 2019 19:41:07