create_sift_dataset.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Create sift feature dataset from assigned images.
5 
6 Input:
7 
8  container_folder/
9  category_1/
10  file_1
11  file_2
12  ...
13  file_42
14  category_2/
15  file_43
16  file_44
17  ...
18 
19 Output:
20 
21  container_folder_sift_feature.pkl.gz
22 
23 """
24 
25 import argparse
26 import gzip
27 import os
28 import pickle
29 
30 import cv2
31 import imagesift
32 import numpy as np
33 from sklearn.datasets.base import Bunch
34 from sklearn.datasets import load_files
35 
36 
38  parser = argparse.ArgumentParser()
39  parser.add_argument('container_path', help='image data container path')
40  parser.add_argument('-O', '--output', default=None, help='output file')
41  args = parser.parse_args()
42 
43  container_path = args.container_path
44  output = (args.output or
45  os.path.basename(container_path) + '_sift_feature.pkl.gz')
46 
47  # See: http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_files.html
48  bunch_files = load_files(container_path=container_path,
49  description='image data',
50  shuffle=False,
51  load_content=False)
52 
53  targets, pos_list, scale_list, ori_list, desc_list = [], [], [], [], []
54  for i, (filename, target) in enumerate(zip(bunch_files.filenames,
55  bunch_files.target)):
56  print(('filename: {}, label: {}'.format(filename, target)))
57  targets.append(target)
58  # extract feature
59  img = cv2.imread(filename, 0)
60  frames, desc = imagesift.get_sift_keypoints(img)
61  # save feature data
62  pos_list.append(np.hstack([frames[:, 0], frames[:, 1]]))
63  ori_list.append(frames[:, 2])
64  scale_list.append(frames[:, 3])
65  desc_list.append(desc)
66 
67  dataset = Bunch(target=np.array(targets),
68  target_names=bunch_files.target_names,
69  positions=pos_list,
70  scales=scale_list,
71  orientations=ori_list,
72  descriptors=desc_list)
73 
74  # save features
75  print('saving sift feature dataset')
76  with gzip.open(output, 'wb') as f:
77  pickle.dump(dataset, f)
78 
79 
80 if __name__ == '__main__':
def get_sift_keypoints(img)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27