aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornia <b01902104@linux19.csie.ntu.edu.tw>2014-07-11 20:49:12 +0800
committernia <b01902104@linux19.csie.ntu.edu.tw>2014-07-11 20:49:12 +0800
commit84cfff24ab73228a91b3f0a8661fa3df0b6578ca (patch)
tree4c5e8326db74fb41e0e1d19d5da40c7024d82f13
parent54557c7ec51b6238f4b9996eb7e306140a9266b6 (diff)
downloadmyoparasite-84cfff24ab73228a91b3f0a8661fa3df0b6578ca.tar.gz
myoparasite-84cfff24ab73228a91b3f0a8661fa3df0b6578ca.tar.zst
myoparasite-84cfff24ab73228a91b3f0a8661fa3df0b6578ca.zip
init
-rw-r--r--Makefile2
-rw-r--r--click.c45
-rw-r--r--code.py47
-rwxr-xr-xmouse_action.py73
-rwxr-xr-xmyoparasite.py26
-rwxr-xr-xrecord.py176
-rwxr-xr-xvisualize.py23
7 files changed, 392 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..1de52fc
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+all: click.c
+ gcc -o click -Dxdo_click=xdo_click_window click.c -lxdo
diff --git a/click.c b/click.c
new file mode 100644
index 0000000..8f6d416
--- /dev/null
+++ b/click.c
@@ -0,0 +1,45 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <xdo.h>
+
+/*
+int main () {
+ xdo_t* xdo = xdo_new (NULL);
+ if (xdo == NULL) {
+ perror ("xdo_new");
+ return 1;
+ }
+
+ int c;
+ while ((c = getchar ()) != EOF) {
+ int button = c - '0';
+ if (!isdigit (c)) {
+ continue;
+ }
+ xdo_click (xdo, CURRENTWINDOW, button);
+ }
+
+ return 0;
+}
+*/
+
+static xdo_t *xdo = NULL;
+
+int myoparasite_mouse_init (void) {
+ xdo = xdo_new (NULL);
+ if (xdo == NULL) {
+ return -1;
+ }
+ return 0;
+}
+
+void myoparasite_mouse_click (int action) {
+ switch (action) {
+ case 1: case 2: case 3: case 4: case 5:
+ xdo_click (xdo, CURRENTWINDOW, button);
+ break;
+ }
+}
diff --git a/code.py b/code.py
new file mode 100644
index 0000000..d670607
--- /dev/null
+++ b/code.py
@@ -0,0 +1,47 @@
+import numpy as np
+import random
+from sklearn.svm import SVC
+from sklearn.grid_search import GridSearchCV
+from sklearn.preprocessing import StandardScaler
+from sklearn.neighbors import KNeighborsClassifier
+from sklearn.cross_validation import KFold
+from sklearn.externals import joblib
+import matplotlib.pyplot as plt
+
+scaler = StandardScaler()
+
+def gen_classifier(X, y):
+ #scaler.fit_transform(X)
+ clf = KNeighborsClassifier(n_neighbors=1, weights='distance')
+ clf.fit(X, y)
+ #joblib.dump(clf, filename)
+ return clf
+
+def main(datas, labels):
+ global scaler
+ # datas, labels = loadFile('data/data3.txt')
+
+ scaler.fit_transform(datas)
+
+ for i in range(len(datas)):
+ #datas[i] = datas[i][1000:-300]
+ for j in range(1050, 1700, 50):
+ datas.append(np.absolute(np.fft.fft(datas[i][j:j+50])))
+ labels.append(labels[i])
+ datas[i] = np.absolute(np.fft.fft(datas[i][1000:1050]))
+
+ #datas[i] = np.absolute(np.fft.fft( datas[i] ))
+ datas = np.array(datas)
+ labels = np.array(labels)
+
+ kf = KFold(len(labels), n_folds=5)
+ for train, test in kf:
+ neigh = KNeighborsClassifier(n_neighbors=1, weights='distance')
+ neigh.fit(datas[train], labels[train])
+ #print neigh.predict(datas[test])
+ #print labels[test]
+ print neigh.score(datas[test], labels[test])
+
+
+if __name__ == '__main__':
+ main()
diff --git a/mouse_action.py b/mouse_action.py
new file mode 100755
index 0000000..bcd48a1
--- /dev/null
+++ b/mouse_action.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python2
+
+from ctypes import *
+
+libxdo = None
+xdo_handle = None
+xdo_func_new = None
+xdo_func_mouse_move = None
+xdo_func_mouse_click = None
+
+class xdo_t(Structure):
+ pass
+
+def init_libxdo():
+ global libxdo
+ global xdo_func_new
+ global xdo_func_mouse_move
+ global xdo_func_mouse_click
+
+ try:
+ libxdo = cdll.LoadLibrary('libxdo.so.3')
+ xdo_func_new = getattr(libxdo, 'xdo_new')
+ xdo_func_mouse_move = getattr(libxdo, 'xdo_move_mouse_relative')
+ xdo_func_mouse_click = getattr(libxdo, 'xdo_click_window')
+ return True
+ except OSError:
+ pass
+
+ try:
+ libxdo = cdll.LoadLibrary('libxdo.so.2')
+ xdo_func_new = getattr(libxdo, 'xdo_new')
+ xdo_func_mouse_move = getattr(libxdo, 'xdo_mousemove_relative')
+ xdo_func_mouse_click = getattr(libxdo, 'xdo_click')
+ return True
+ except OSError:
+ pass
+
+ return False
+
+
+def init_func_args():
+ xdo_func_new.argtypes = [c_char_p]
+ xdo_func_new.restype = POINTER(xdo_t)
+ xdo_func_mouse_move.argtypes = [POINTER(xdo_t), c_int, c_int]
+ xdo_func_mouse_move.restype = c_int
+ xdo_func_mouse_click.argtypes = [POINTER(xdo_t), c_long, c_int]
+ xdo_func_mouse_click.restype = c_int
+
+def init_xdo_handle():
+ global xdo_handle
+ xdo_handle = xdo_func_new(None)
+ if xdo_handle == None:
+ return False
+ return True
+
+def mouse_action(action):
+ if libxdo == None:
+ if not init_libxdo():
+ raise 'Cannot load library: libxdo'
+ else:
+ init_func_args()
+
+ if xdo_handle == None:
+ if not init_xdo_handle():
+ raise 'Cannot create object: xdo_t'
+
+ if action >= 1 and action <= 5:
+ xdo_func_mouse_click(xdo_handle, 0, action)
+
+
+if __name__ == '__main__':
+ import sys
+ mouse_action(int(sys.argv[1]))
diff --git a/myoparasite.py b/myoparasite.py
new file mode 100755
index 0000000..67ace3b
--- /dev/null
+++ b/myoparasite.py
@@ -0,0 +1,26 @@
+#! /usr/bin/env python2
+
+from mouse_action import mouse_action
+import record
+import subprocess
+
+
+def main () :
+ predict_proc = subprocess.Popen (["./record.py", "read"], stdout = subprocess.PIPE)
+ mouse_proc = subprocess.Popen (["nc", "192.168.247.2", "5555"], stdout = subprocess.PIPE)
+
+ import time
+
+ while True :
+ predict_proc.stdout.flush ()
+ predict = int (predict_proc.stdout.readline ())
+ mouse_proc.stdout.flush ()
+ mouse = int (mouse_proc.stdout.readline ())
+ print (predict, mouse)
+ if predict == 1 :
+ pass
+ mouse_action (mouse)
+
+
+if __name__ == "__main__" :
+ main ()
diff --git a/record.py b/record.py
new file mode 100755
index 0000000..0b7db22
--- /dev/null
+++ b/record.py
@@ -0,0 +1,176 @@
+#! /usr/bin/env python
+import fcntl
+import json
+import matplotlib.pyplot as plt
+import numpy as np
+import os
+import random
+import subprocess
+import sys
+import time
+import threading
+
+import Classifier
+from sklearn.externals import joblib
+from sklearn.preprocessing import StandardScaler
+
+import code
+
+#scaler1 = StandardScaler()
+#scaler2 = StandardScaler()
+buf = ""
+
+class readdataThread (threading.Thread) :
+ def __init__ (self, FILE) :
+ super (readdataThread, self).__init__ ()
+ self.FILE = FILE
+ def run (self) :
+ global buf
+
+ while True :
+ buf += self.FILE.readline (50).strip ()
+
+
+def getdata (raw) :
+ data1 = []
+ data2 = []
+ m = 1
+ for j in range (0, len (raw), 4) :
+ n = int (raw[j:j+4], 16)
+ if n >= 32768 :
+ n -= 65536
+ if m == 1 :
+ data1.append (float(n))
+ m = 2
+ else :
+ data2.append (float(n))
+ m = 1
+ return data1, data2
+
+
+def record () :
+ labels = range(Classifier.NUM_OF_LABELS-1, -1, -1) * 15
+ # random.shuffle(labels)
+ datas = []
+ rawdata1 = []
+ rawdata2 = []
+
+ record_time = len (labels)
+ shcmd = "arecord -c 2 -d %d -t raw -r 2000 -f S16_BE - 2>/dev/null | xxd -p | tr -d '\n' > tmppp" % (record_time + 1)
+ proc = subprocess.Popen (shcmd , stdout = subprocess.PIPE, shell = True)
+ for i in range (-1, record_time) :
+ if i > -1 :
+ print (labels[i])
+ time.sleep (1)
+
+ f = open ('tmppp', 'r')
+ for i in range (-1, record_time) :
+ if i == -1 :
+ f.readline (16000)
+ continue
+ data1, data2 = getdata (f.readline (16000))
+ # plt.subplot(2,1,1)
+ # plt.plot(data1)
+ # plt.subplot(2,1,2)
+ # plt.plot(data2)
+ # plt.show()
+
+ rawdata1.append(data1)
+ rawdata2.append(data2)
+
+ f = open ("rawdata", "w")
+ f.write (json.dumps (zip(labels, rawdata1, rawdata2)))
+ f.close ()
+
+ return rawdata1, rawdata2, labels
+
+
+def readdata():
+ with open("rawdata", "r") as f:
+ y, X1, X2 = zip(*json.loads(f.readline()))
+ Classifier.NUM_OF_LABELS = len (set (y))
+ return X1, X2, y
+
+
+def train(rawdata1, rawdata2, y):
+ sys.stderr.write ("start training\n")
+ X = []
+ X1 = rawdata1
+ X2 = rawdata2
+ y_2 = []
+ for yi, x1, x2 in zip(y, X1, X2):
+ for i in range(700, 1300, Classifier.WINDOW_SHIFT_TRAIN):
+ X.append( np.concatenate((
+ np.absolute(np.fft.fft(x1[i: i+Classifier.WINDOW_SIZE])) ,
+ np.absolute(np.fft.fft(x2[i: i+Classifier.WINDOW_SIZE])) ) ).tolist())
+ y_2.append( yi )
+ y = y_2
+ scalers, classifiers, scores = Classifier.gen_model(X, y, verbose=False)
+ sys.stderr.write ("finish training\n")
+ return scalers, classifiers, scores
+
+
+def predict (scalers, classifiers, scores) :
+ global buf
+
+ sys.stderr.write ("start predict\n")
+
+ shcmd = "arecord -t raw -c 2 -r 2000 -f S16_BE - 2>/dev/null | xxd -p"
+ proc = subprocess.Popen (shcmd, stdout = subprocess.PIPE, shell = True)
+ read_thread = readdataThread (proc.stdout)
+ read_thread.start ()
+
+ count = 0
+ p = {}
+ while True :
+ if len (buf) >= Classifier.WINDOW_SIZE * 8 :
+ data1, data2 = getdata (buf[-Classifier.WINDOW_SIZE * 8:])
+ buf = buf[50:]
+
+ data1 = np.absolute(np.fft.fft(data1))
+ data2 = np.absolute(np.fft.fft(data2))
+ X = data1.tolist () + data2.tolist ()
+ tp = Classifier.multi_classification(X, scalers, classifiers, scores)[0]
+ #tp = clf.predict(X)[0]
+ if tp in p:
+ p[tp] += 1
+ else :
+ p[tp] = 1
+ count += 1
+ if count >= 20 :
+ maxi = 0
+ maj = -1
+ for m in p :
+ if p[m] > maxi :
+ maxi = p[m]
+ maj = m
+ count = 0
+ p = {}
+ print (maj)
+ sys.stdout.flush ()
+
+ read_thread.join ()
+
+
+def main () :
+ argv = sys.argv
+ argc = len (argv)
+ if argc != 2 :
+ sys.stderr.write ("Usage: %s {new|read}\n" % (argv[0]))
+ exit (1)
+
+ if argv[1] == "new" :
+ Classifier.NUM_OF_LABELS = 2
+ rawx1, rawx2, y = record()
+ elif argv[1] == "read" :
+ rawx1, rawx2, y = readdata()
+ else :
+ sys.stderr.write ("Wrong arguments\n")
+ exit (1)
+ scalers, classifiers, scores = train (rawx1, rawx2, y)
+
+ predict (scalers, classifiers, scores)
+
+
+if __name__ == "__main__" :
+ main ()
diff --git a/visualize.py b/visualize.py
new file mode 100755
index 0000000..b85e33a
--- /dev/null
+++ b/visualize.py
@@ -0,0 +1,23 @@
+#! /usr/bin/env python2
+import json
+import matplotlib.pyplot as plt
+import sys
+import numpy as np
+
+argv = sys.argv
+argc = len (argv)
+
+if argc != 2 :
+ print ("Usage : %s [data]" % (argv[0]))
+ exit (1)
+
+f = open (argv[1], "r")
+labels, datas1, datas2 = zip(*json.loads (f.readline ()))
+for i in range (0, len (datas1)) :
+ print (labels[i])
+ plt.subplot (2, 1, 1)
+ plt.plot (datas1[i])
+ plt.subplot (2, 1, 2)
+ plt.plot (datas2[i])
+ plt.show ()
+f.close ()