<s> from PIL import Image <EOL> from numpy import * <EOL> from pylab import * <EOL> from svmutil import * <EOL> import os <EOL> from PCV . classifiers import knn , bayes <EOL> from PCV . tools import imtools <EOL> from PCV . localdescriptors import dsift , sift <EOL> """<STR_LIT>""" <EOL> def read_gesture_features_labels ( path ) : <EOL> featlist = [ os . path . join ( path , f ) for f in os . listdir ( path ) if f . endswith ( '<STR_LIT>' ) ] <EOL> features = [ ] <EOL> for featfile in featlist : <EOL> l , d = sift . read_features_from_file ( featfile ) <EOL> features . append ( d . flatten ( ) ) <EOL> features = array ( features ) <EOL> labels = [ featfile . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] for featfile in featlist ] <EOL> return features , array ( labels ) <EOL> def convert_labels ( labels , transl ) : <EOL> """<STR_LIT>""" <EOL> return [ transl [ l ] for l in labels ] <EOL> def print_confusion ( res , labels , classnames ) : <EOL> n = len ( classnames ) <EOL> class_ind = dict ( [ ( classnames [ i ] , i ) for i in range ( n ) ] ) <EOL> confuse = zeros ( ( n , n ) ) <EOL> for i in range ( len ( test_labels ) ) : <EOL> confuse [ class_ind [ res [ i ] ] , class_ind [ test_labels [ i ] ] ] += <NUM_LIT:1> <EOL> print '<STR_LIT>' <EOL> print classnames <EOL> print confuse <EOL> features , labels = read_gesture_features_labels ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' , features . shape , len ( labels ) <EOL> test_features , test_labels = read_gesture_features_labels ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' , test_features . shape , len ( test_labels ) <EOL> classnames = unique ( labels ) <EOL> nbr_classes = len ( classnames ) <EOL> if False : <EOL> from PCV . tools import pca <EOL> V , S , m = pca . pca ( features ) <EOL> V = V [ : <NUM_LIT:50> ] <EOL> features = array ( [ dot ( V , f - m ) for f in features ] ) <EOL> test_features = array ( [ dot ( V , f - m ) for f in test_features ] ) <EOL> if True : <EOL> k = <NUM_LIT:1> <EOL> knn_classifier = knn . KnnClassifier ( labels , features ) <EOL> res = array ( [ knn_classifier . classify ( test_features [ i ] , k ) for i in range ( len ( test_labels ) ) ] ) <EOL> if False : <EOL> bc = bayes . BayesClassifier ( ) <EOL> blist = [ features [ where ( labels == c ) [ <NUM_LIT:0> ] ] for c in classnames ] <EOL> bc . train ( blist , classnames ) <EOL> res = bc . classify ( test_features ) [ <NUM_LIT:0> ] <EOL> if False : <EOL> features = map ( list , features ) <EOL> test_features = map ( list , test_features ) <EOL> transl = { } <EOL> for i , c in enumerate ( classnames ) : <EOL> transl [ c ] , transl [ i ] = i , c <EOL> prob = svm_problem ( convert_labels ( labels , transl ) , features ) <EOL> param = svm_parameter ( '<STR_LIT>' ) <EOL> m = svm_train ( prob , param ) <EOL> res = svm_predict ( convert_labels ( labels , transl ) , features , m ) <EOL> res = svm_predict ( convert_labels ( test_labels , transl ) , test_features , m ) [ <NUM_LIT:0> ] <EOL> res = convert_labels ( res , transl ) <EOL> acc = sum ( <NUM_LIT:1.0> * ( res == test_labels ) ) / len ( test_labels ) <EOL> print '<STR_LIT>' , acc <EOL> print_confusion ( res , test_labels , classnames ) </s>
