Created
November 2, 2014 17:51
-
-
Save defvol/97a7f1f60da30af36fea to your computer and use it in GitHub Desktop.
A Leap Motion sign language classifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import getopt | |
| import csv | |
| import random | |
| def parse_csv(file): | |
| with open(file) as csvfile: | |
| csvreader = csv.reader(csvfile) | |
| data = [] | |
| target = [] | |
| for row in csvreader: | |
| target.append(row.pop()) | |
| data.append([float(coord) for coord in row]) | |
| return data, target | |
| def train_knn(training): | |
| d,t = parse_csv(training) | |
| cut = int(len(d) * 0.7) | |
| training_set = d[cut:] | |
| labeling_set = t[cut:] | |
| validation_set = d[:cut] | |
| from sklearn import neighbors | |
| knn = neighbors.KNeighborsClassifier() | |
| knn.fit(training_set, labeling_set) | |
| return knn | |
| def predict(data, training): | |
| classifier = train_knn(training) | |
| klass = classifier.predict(data) | |
| return klass | |
| def usage(): | |
| print 'Usage:' | |
| print sys.argv[0], '--predict <DATA> --from <TRAINING_SET>' | |
| def main(argv): | |
| training, predictable = '', '' | |
| try: | |
| opts, args = getopt.getopt(argv, "htf:p:", ["help", "test", "from=", "predict="]) | |
| except getopt.GetoptError, e: | |
| print str(e) | |
| usage() | |
| sys.exit(2) | |
| for opt, arg in opts: | |
| if opt in ("-h", "--help"): | |
| usage() | |
| sys.exit() | |
| elif opt in ("-t", "--test"): | |
| data = '0.516162, 0.856276, -0.0192088, -0.659962, 0.411919, 0.628309, 0.545918, -0.311632, 0.777727, 0.402093, 0.9001, -0.167755, -0.487101, 0.365433, 0.793216, 0.775276, -0.237233, 0.585378, 0.402093, 0.9001, -0.167755, -0.439081, 0.350339, 0.827328, 0.803448, -0.259005, 0.536085, 0.402093, 0.9001, -0.167755, -0.330934, 0.313705, 0.889984, 0.8537, -0.302341, 0.424012, 0.940846, -0.0730333, -0.330869, 0.120909, 0.984572, 0.126485, 0.316527, -0.159008, 0.935162, 0.944734, -0.0750178, -0.319138, 0.104466, 0.991608, 0.0761574, 0.310747, -0.105288, 0.944643, 0.944734, -0.0750178, -0.319138, 0.0899903, 0.995415, 0.0324095, 0.315244, -0.0593377, 0.947154, 0.944734, -0.0750178, -0.319138, 0.0784608, 0.996915, -0.00207365, 0.318309, -0.0230808, 0.947706, 0.94047, -0.260613, -0.218166, 0.285149, 0.954321, 0.0892271, 0.184946, -0.146125, 0.971824, 0.953749, -0.273267, -0.125253, 0.276753, 0.960878, 0.0109837, 0.117351, -0.0451397, 0.992064, 0.953749, -0.273267, -0.125253, 0.270764, 0.961937, -0.0369247, 0.130575, 0.00130301, 0.991438, 0.953749, -0.273267, -0.125253, 0.265647, 0.961204, -0.0742896, 0.140694, 0.0375806, 0.98934, 0.933247, -0.35047, -0.0788773, 0.357363, 0.928118, 0.104346, 0.0366372, -0.125569, 0.991408, 0.933495, -0.351402, -0.0714364, 0.356172, 0.931711, 0.0711032, 0.0415723, -0.0918181, 0.994908, 0.933495, -0.351402, -0.0714364, 0.353771, 0.935041, 0.0233504, 0.0585906, -0.0470696, 0.997172, 0.933495, -0.351402, -0.0714364, 0.35129, 0.936154, -0.0145415, 0.0719854, -0.0115205, 0.997339, 0.859253, -0.51115, 0.0202481, 0.49999, 0.847544, 0.177984, -0.108137, -0.142809, 0.983825, 0.847765, -0.520984, 0.0993472, 0.401102, 0.752342, 0.522589, -0.347003, -0.403184, 0.846777, 0.847765, -0.520984, 0.0993472, 0.521307, 0.85301, 0.0247423, -0.0976346, 0.0308148, 0.994745, 0.847765, -0.520984, 0.0993472, 0.508154, 0.744228, -0.433478, 0.151898, 0.417972, 0.895671' | |
| from subprocess import call | |
| call(["python", sys.argv[0], "-f", "training.csv", "-p", data]) | |
| elif opt in ("-f", "--from"): | |
| training = arg | |
| elif opt in ("-p", "--predict"): | |
| predictable = arg.split(',') | |
| if(len(predictable) > 0): | |
| prediction = predict(predictable, training) | |
| print "Class: ", prediction | |
| else: | |
| return -1 | |
| if __name__ == "__main__": | |
| main(sys.argv[1:]) | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In action:
➜ slang python slang.py --from training.csv --predict '0.656128, 0.536473, 0.530747, -0.210324, -0.545438, 0.811333, 0.724748, -0.643967, -0.245044, 0.745088, 0.447858, 0.494234, -0.0857279, -0.670564, 0.736881, 0.661434, -0.591411, -0.461235, 0.745088, 0.447858, 0.494234, -0.481741, -0.151113, 0.863186, 0.46127, -0.881243, 0.103159, 0.745088, 0.447858, 0.494234, -0.58217, 0.0751144, 0.80959, 0.325457, -0.890944, 0.316696, 0.664667, 0.519156, -0.537303, 0.404042, 0.355157, 0.842979, 0.628465, -0.777393, 0.0262997, 0.671776, 0.510275, -0.536969, -0.575021, 0.816203, 0.0562469, 0.466977, 0.270983, 0.841725, 0.671776, 0.510275, -0.536969, -0.662847, 0.0904882, -0.743267, -0.330681, 0.855238, 0.399022, 0.671776, 0.510275, -0.536969, -0.435463, -0.314371, -0.84353, -0.59924, 0.800494, 0.0110191, 0.66478, 0.341151, -0.664592, 0.524295, 0.42066, 0.740378, 0.532148, -0.840631, 0.100782, 0.670273, 0.3324, -0.663509, -0.467466, 0.883515, -0.0296136, 0.576377, 0.330017, 0.747582, 0.670273, 0.3324, -0.663509, -0.718938, 0.069146, -0.691626, -0.184018, 0.9406, 0.285322, 0.670273, 0.3324, -0.663509, -0.55395, -0.370861, -0.745387, -0.493837, 0.867163, -0.0644449, 0.694417, 0.181039, -0.696427, 0.587147, 0.416946, 0.69384, 0.415985, -0.890719, 0.183238, 0.687182, 0.196285, -0.699466, -0.3608, 0.927886, -0.09408, 0.630558, 0.317018, 0.708446, 0.687182, 0.196285, -0.699466, -0.723782, 0.101997, -0.682448, -0.0626105, 0.975228, 0.212158, 0.687182, 0.196285, -0.699466, -0.613871, -0.358011, -0.703556, -0.388514, 0.912853, -0.125525, 0.631272, 0.00533413, -0.775543, 0.722436, 0.359687, 0.590518, 0.282103, -0.933058, 0.223207, 0.639684, -0.0234884, -0.768279, 0.00313799, 0.999604, -0.0279479, 0.768631, 0.015467, 0.639505, 0.639684, -0.0234884, -0.768279, -0.716368, 0.34408, -0.606981, 0.278607, 0.938646, 0.203276, 0.639684, -0.0234884, -0.768279, -0.755755, -0.201453, -0.623098, -0.140136, 0.979217, -0.146617'
Class: [" 'y'"]