In [1]:
from numpy import *
from PIL import *
import pickle
from pylab import *
import os
In [2]:
import sift
import dsift
dsift = reload(dsift)
import imtools
imtools = reload(imtools)
In [3]:
def read_gesture_features_labels(path):
# make a list of the files with .dsift at the end
featlist = [os.path.join(path, f) for f in os.listdir(path)
if f.endswith('.dsift')]
# read features
features = []
for featfile in featlist:
l, d = sift.read_features_from_file(featfile)
features.append(d.flatten())
features = array(features)
# generate labels
labels = [featfile.split('/')[-1][0] for featfile in featlist]
return features, array(labels)
In [4]:
features, labels = read_gesture_features_labels('train2/')
test_features, test_labels = read_gesture_features_labels('test2/')
classnames = unique(labels)
In [5]:
# the first letter of the file name is the label
print labels
In [42]:
import knn
knn = reload(knn)
In [44]:
# k neighbors
k = 1
knn_classifier = knn.KnnClassifier(labels, features)
In [45]:
res = array([knn_classifier.classify(features[i], k) for i in
range(len(labels))])
# accuracy
acc = sum(1.0*(res==labels))/len(labels)
print 'Accuracy:', acc
In [46]:
# use larger weight in the center
knn_classifier = knn.KnnClassifier(labels, features, centerweight=True)
res = array([knn_classifier.classify(test_features[i], k) for i in
range(len(test_labels))])
# accuracy
acc = sum(1.0*(res==test_labels))/len(test_labels)
print 'Accuracy:', acc
In [47]:
knn_classifier = knn.KnnClassifier(labels, features, centerweight=False)
res = array([knn_classifier.classify(test_features[i], k) for i in
range(len(test_labels))])
# accuracy
acc = sum(1.0*(res==test_labels))/len(test_labels)
print 'Accuracy:', acc
In [ ]: