-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVMActivity.java
279 lines (214 loc) · 7.48 KB
/
SVMActivity.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package emg.svm;
import java.io.IOException;
import java.util.ArrayList;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import libsvm.*;
import emg.backend.*;
//import emg.bluetooth.test;
public class SVMActivity extends Activity implements ITransformListener{
/** Called when the activity is first created. */
private static final String TAG = "Libsvm";
byte[] y;
double Cp, Cn;
String DEVICE_ADDRESS = "00:06:66:04:9B:21";
byte channels = 1;//each bit is a channel (bits1-6)
DataProtocol dataProtocol;
ArrayList<nodeData> nodeContainer;
long Delay = 5000;
svm_problem prob;
TextView resultText;
TextView tv;
View view1;
Button trainButton;
public boolean trainingstate = false;
public boolean testingstate = false;
svm_model initialmodel;
public boolean trainingComplete = false;
long time;
// has a touch press started?
public boolean touchStarted = false;
public boolean trainingInProgress = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.trainButton = (Button)findViewById(R.id.button1);
this.nodeContainer = new ArrayList();
this.view1 = (View)findViewById(R.id.view1);
this.resultText = (TextView) findViewById(R.id.result_text);
this.resultText.setText("onCreate()");
//targetView = (View)findViewById(R.id.mainlayout);
view1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
touchStarted = true;
//Log.d(TAG, "ActionDown Detected" );
view1.setBackgroundColor(0x0000FF00);
}
else if (action == MotionEvent.ACTION_UP)
{
touchStarted = false;
// Log.d(TAG, "ActionUp Detected" );
view1.setBackgroundColor(0xFFFF0000);
}
return touchStarted;
}
});
//this will start the training sequence.
trainButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
train();
}
});
//start classifying on the input from bluetooth
}
@Override
public void onDestroy()
{
this.dataProtocol.StopAndDisconnect();
super.onPause();
}
//this class contains
private class nodeData{
public int[] data;
public double output;
public nodeData(int[] data, double output){
this.data = data;
this.output = output;
}
}
//reading training data from blue-tooth
//put training data into nodeData class and add it to
//1.0 or 0.0 is passed into nodeData depending on whether screentouch is true or false
public void addData(int[] data) {
long timenow = System.currentTimeMillis();
if ((timenow - time) > Delay) trainingstate = false;
nodeData temp;
if(touchStarted){
temp = new nodeData(data, 1.0);
if (trainingstate) nodeContainer.add(temp);
//Log.d(TAG, "ActionDown add" );
}else{
temp = new nodeData(data, 0.0);
if (trainingstate) nodeContainer.add(temp);
//Log.d(TAG, "action up add" );
//System.out.println( 0);
}
//Log.d(TAG, "Length: " + Integer.toString(nodeContainer.size()));
if (!trainingstate){
if(!trainingComplete){
synchronized (this) {
if(!trainingInProgress){
Log.e(TAG, "about to try to make model" );
trainingInProgress = true;
initialmodel = makeModel();
Log.e(TAG, "Model construction complete" );
trainingComplete = true;
}
}
}
if (trainingComplete) predict_results(initialmodel, temp);
}//Log.d(TAG, "Prediction thread" );
}
public svm_model makeModel(){
svm_node node;
prob = new svm_problem();
svm_model model;
svm_parameter subparam = new svm_parameter();
subparam.svm_type = svm_parameter.C_SVC;
subparam.kernel_type = svm_parameter.LINEAR;
subparam.degree = 3;
subparam.gamma = 0;
subparam.coef0 = 0;
subparam.nu = 0.5;
subparam.cache_size = 100;
subparam.C = 0.1;
subparam.eps = 0.001;
subparam.p = 0.1;
subparam.shrinking = 1;
subparam.probability = 0;
subparam.nr_weight = 0;
subparam.weight_label = new int[2];
subparam.weight = new double[2];
subparam.weight[0] = 0;
subparam.weight[1] = 0;
Log.d(TAG, "Making model" );
int length = nodeContainer.size();
prob.x = new svm_node[length][8];
prob.y = new double[length];
prob.l = length;
double output;
Log.d(TAG, "Prob.l x and y defined" );
Log.d(TAG, "Length: " + Integer.toString(length));
for (int i=0; i < length; i++){
output = nodeContainer.get(i).output;
prob.y[i] = output;
//Log.d(TAG, "prob.y set");
int count =0;
for (int j=0;j<nodeContainer.get(i).data.length;j++)
{
prob.x[i][j] = new svm_node();
//Log.d(TAG, "index setting" );
prob.x[i][j].index = count;
//Log.d(TAG, "index set" );
prob.x[i][j].value = (double) nodeContainer.get(i).data[j];
count++;
}
//Log.d(TAG, "trying to build svmnode" );
}Log.d(TAG, "SVM problem defined" );
model = svm.svm_train(prob, subparam);
Log.d(TAG, "Model created" );
return model;
}
public void train(){
Log.d(TAG, "train button clicked" );
tv = (TextView) findViewById(R.id.start_train_text);
tv.setText("Start Training");
//setContentView(R.layout.main);
trainingstate = true;
this.dataProtocol = new DataProtocol(this, (ITransformListener) this, channels, DEVICE_ADDRESS);
this.dataProtocol.Start();
time = System.currentTimeMillis();
//SystemClock.sleep(Delay);
//trainingstate = false;
}
public void predict_results(svm_model initialmodel, nodeData node){
//Log.d(TAG, "prediction thread" );
//nodeData data = nodeContainer.get(nodeContainer.size() -1);
svm_node[] currentNode = new svm_node[8];
for (int i = 0; i < 8; i++){
currentNode[i] = new svm_node();
currentNode[i].index = i;
currentNode[i].value = node.data[i];
}
double result = svm.svm_predict(initialmodel, currentNode);
Message msg = new Message();
msg.obj = result;
this.newDataHandler.sendMessage(msg);
}
Handler newDataHandler = new Handler(){
@Override
public void handleMessage(Message msg){
double result = (Double) msg.obj;
String s = "";
s = Double.toString(result);
resultText.setText(s);
if (result == 1.0){
view1.setBackgroundColor(0xFFFF0000);
}else view1.setBackgroundColor(0x0000FF00);
//Log.d(TAG, "end of handler()");
}
};
}