-
Notifications
You must be signed in to change notification settings - Fork 94
/
classify.py
57 lines (47 loc) · 1.77 KB
/
classify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import libsvm
import argparse
from cPickle import load
from learn import extractSift, computeHistograms, writeHistogramsToFile
HISTOGRAMS_FILE = 'testdata.svm'
CODEBOOK_FILE = 'codebook.file'
MODEL_FILE = 'trainingdata.svm.model'
def parse_arguments():
parser = argparse.ArgumentParser(description='classify images with a visual bag of words model')
parser.add_argument('-c', help='path to the codebook file', required=False, default=CODEBOOK_FILE)
parser.add_argument('-m', help='path to the model file', required=False, default=MODEL_FILE)
parser.add_argument('input_images', help='images to classify', nargs='+')
args = parser.parse_args()
return args
print "---------------------"
print "## extract Sift features"
all_files = []
all_files_labels = {}
all_features = {}
args = parse_arguments()
model_file = args.m
codebook_file = args.c
fnames = args.input_images
all_features = extractSift(fnames)
for i in fnames:
all_files_labels[i] = 0 # label is unknown
print "---------------------"
print "## loading codebook from " + codebook_file
with open(codebook_file, 'rb') as f:
codebook = load(f)
print "---------------------"
print "## computing visual word histograms"
all_word_histgrams = {}
for imagefname in all_features:
word_histgram = computeHistograms(codebook, all_features[imagefname])
all_word_histgrams[imagefname] = word_histgram
print "---------------------"
print "## write the histograms to file to pass it to the svm"
nclusters = codebook.shape[0]
writeHistogramsToFile(nclusters,
all_files_labels,
fnames,
all_word_histgrams,
HISTOGRAMS_FILE)
print "---------------------"
print "## test data with svm"
print libsvm.test(HISTOGRAMS_FILE, model_file)