Commit 5da3d52d authored by Oskars Linde's avatar Oskars Linde
Browse files

Cleaned code

parent aaeae4f1
// Katru no vērtībam rakstīt failā ar timestamp un pēc tam pārbaudīt, kāda ir atšķirība procesā
// For data export // For data export
// C:\Users\Oskars\Downloads\adb>adb -d shell "run-as com.example.oskars.xyzregister_v2 cat /data/data/com.example.oskars.xyzregister_v2/files/accData.txt" > accData.txt // C:\Users\Oskars\Downloads\adb>adb -d shell "run-as com.example.oskars.xyzregister_v2 cat /data/data/com.example.oskars.xyzregister_v2/files/accData.txt" > accData.txt
package io.github.introml.activityrecognition; package io.github.introml.activityrecognition;
import android.content.Context; import android.content.Context;
...@@ -17,6 +14,9 @@ import android.speech.tts.TextToSpeech; ...@@ -17,6 +14,9 @@ import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView; import android.widget.TextView;
import java.io.IOException; import java.io.IOException;
...@@ -28,136 +28,52 @@ import java.util.ArrayList; ...@@ -28,136 +28,52 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
//import java.util.Random;
//import java.util.Timer;
//import java.util.TimerTask;
//import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
//import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
public class MainActivity extends AppCompatActivity implements SensorEventListener, TextToSpeech.OnInitListener { public class MainActivity extends AppCompatActivity implements SensorEventListener, TextToSpeech.OnInitListener {
private float[] results;
private static final int N_SAMPLES = 200; private static final int N_SAMPLES = 200;
private static List<Float> x; private static List<Float> x, y, z;
private static List<Float> y;
private static List<Float> z;
private static List<Long> timestamps; private static List<Long> timestamps;
private static List<Float> xGyro; private static String filename;
private static List<Float> yGyro; private static String[] labels = {"Downstairs", "Limping", "Standing", "Upstairs", "Walking", ""};
private static List<Float> zGyro; private TextView downstairsTextView, joggingTextView, sittingTextView, standingTextView, upstairsTextView, walkingTextView;
private TextView downstairsTextView; private TextView firstTextView, secondTextView, thirdTextView, forthTextView, fiftTextView, sixtTextView;
private TextView joggingTextView;
private TextView sittingTextView;
private TextView standingTextView;
private TextView upstairsTextView;
private TextView walkingTextView;
private TextView firstTextView;
private TextView secondTextView;
private TextView thirdTextView;
private TextView forthTextView;
private TextView fiftTextView;
private TextView sixtTextView;
private TextView xTextView;
private TextView yTextView;
private TextView zTextView;
private TextView xGyroTextView;
private TextView yGyroTextView;
private TextView zGyroTextView;
private TextView logTextView; private TextView logTextView;
private static boolean startDataRegister = true;
private TextToSpeech textToSpeech; private TextToSpeech textToSpeech;
private float[] results;
private TensorFlowClassifier classifier; private TensorFlowClassifier classifier;
private static String filename; private Switch ClassifierSwitch;
private String dataLine; private Switch SoundSwitch;
// labels for 6 classes
//private String[] labels = {"Downstairs", "Jogging", "Sitting", "Standing", "Upstairs", "Walking"};
// labels for 3 classes
private String[] labels = {"Downstairs", "Limping", "Standing", "Upstairs", "Walking", "None"};
//private String[] labels = {"Downstairs", "Standing", "Walking", "Upstairs", "None", "None"};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ClassifierSwitch = (Switch) findViewById(R.id.classifierSwitch);
SoundSwitch = (Switch) findViewById(R.id.soundSwitch);
ClassifierSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
onPause();
}
else {
onResume();
}
}
});
prepareTextviews();
setTextviews();
x = new ArrayList<>(); x = new ArrayList<>();
y = new ArrayList<>(); y = new ArrayList<>();
z = new ArrayList<>(); z = new ArrayList<>();
timestamps = new ArrayList<>(); timestamps = new ArrayList<>();
xGyro = new ArrayList<>();
yGyro = new ArrayList<>();
zGyro = new ArrayList<>();
downstairsTextView = (TextView) findViewById(R.id.downstairs_prob);
joggingTextView = (TextView) findViewById(R.id.jogging_prob);
sittingTextView = (TextView) findViewById(R.id.sitting_prob);
standingTextView = (TextView) findViewById(R.id.standing_prob);
upstairsTextView = (TextView) findViewById(R.id.upstairs_prob);
walkingTextView = (TextView) findViewById(R.id.walking_prob);
firstTextView = (TextView) findViewById(R.id.downstairs_title);
secondTextView = (TextView) findViewById(R.id.jogging_title);
thirdTextView = (TextView) findViewById(R.id.sitting_title);
forthTextView = (TextView) findViewById(R.id.standing_title);
fiftTextView = (TextView) findViewById(R.id.upstairs_title);
sixtTextView = (TextView) findViewById(R.id.walking_title);
xTextView = (TextView) findViewById(R.id.x_data);
yTextView = (TextView) findViewById(R.id.y_data);
zTextView = (TextView) findViewById(R.id.z_data);
xGyroTextView = (TextView) findViewById(R.id.xGyro_data);
yGyroTextView = (TextView) findViewById(R.id.yGyro_data);
zGyroTextView = (TextView) findViewById(R.id.zGyro_data);
logTextView = (TextView) findViewById(R.id.log);
classifier = new TensorFlowClassifier(getApplicationContext()); classifier = new TensorFlowClassifier(getApplicationContext());
firstTextView.setText(labels[0]);
secondTextView.setText(labels[1]);
thirdTextView.setText(labels[2]);
forthTextView.setText(labels[3]);
fiftTextView.setText(labels[4]);
sixtTextView.setText(labels[5]);
textToSpeech = new TextToSpeech(this, this); textToSpeech = new TextToSpeech(this, this);
textToSpeech.setLanguage(Locale.US); textToSpeech.setLanguage(Locale.US);
cleanFile(getBaseContext());
writeToFile(getBaseContext());
} }
@Override @Override
public void onInit(int status) { public void onInit(int status) {
// Timer timer = new Timer();
// timer.scheduleAtFixedRate(new TimerTask() {
// @Override
// public void run() {
// if (results == null || results.length == 0) {
// return;
// }
// float max = -1;
// int idx = -1;
// for (int i = 0; i < results.length; i++) {
// if (results[i] > max) {
// idx = i;
// max = results[i];
// }
// }
//
// textToSpeech.speak(labels[idx], TextToSpeech.QUEUE_ADD, null, Integer.toString(new Random().nextInt()));
// }
// }, 2000, 5000);
} }
protected void onPause() { protected void onPause() {
...@@ -168,46 +84,22 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -168,46 +84,22 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
//getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
} }
// @Override
// public void onSensorChanged(SensorEvent event) {
// activityPrediction();
// x.add(event.values[0]);
// y.add(event.values[1]);
// z.add(event.values[2]);
// }
@Override @Override
public void onSensorChanged(SensorEvent event) { public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor; Sensor sensor = event.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) { if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
activityPrediction(); activityPrediction();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
x.add(event.values[0]); x.add(event.values[0]);
y.add(event.values[1]); y.add(event.values[1]);
z.add(event.values[2]); z.add(event.values[2]);
String dataLine = String.format("%d,%s,%s,%s%n",
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
dataLine = String.format("%d,%s,%s,%s%n",
timestamp.getTime(), x.get(x.size() - 1), y.get(y.size() - 1), z.get(z.size() - 1)); timestamp.getTime(), x.get(x.size() - 1), y.get(y.size() - 1), z.get(z.size() - 1));
writeToFile(dataLine, getBaseContext()); writeToFile(dataLine, getBaseContext());
timestamps.add(timestamp.getTime()); timestamps.add(timestamp.getTime());
xTextView.setText(Float.toString(round(event.values[0], 2)));
yTextView.setText(Float.toString(round(event.values[1], 2)));
zTextView.setText(Float.toString(round(event.values[2], 2)));
} }
// } else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
// xGyro.add(event.values[0]);
// yGyro.add(event.values[1]);
// zGyro.add(event.values[2]);
//
// xGyroTextView.setText(Float.toString(round(event.values[0], 2)));
// yGyroTextView.setText(Float.toString(round(event.values[1], 2)));
// zGyroTextView.setText(Float.toString(round(event.values[2], 2)));
// }
} }
@Override @Override
...@@ -217,92 +109,77 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -217,92 +109,77 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private void activityPrediction() { private void activityPrediction() {
if (x.size() == N_SAMPLES && y.size() == N_SAMPLES && z.size() == N_SAMPLES) { if (x.size() == N_SAMPLES && y.size() == N_SAMPLES && z.size() == N_SAMPLES) {
// List<Float> gyroData = new ArrayList<>();
// gyroData.addAll(xGyro);
// gyroData.addAll(yGyro);
// gyroData.addAll(zGyro);
new Thread(new secondThread(x, y, z, timestamps)).start(); new Thread(new secondThread(x, y, z, timestamps)).start();
// xGyro.clear();
// yGyro.clear();
// zGyro.clear();
} }
} }
private float[] toFloatArray(List<Float> list) { private void cleanFile(Context context) {
int i = 0; try {
float[] array = new float[list.size()]; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
for (Float f : list) { filename = "accData-" + dateFormat.format(date) + ".txt";
array[i++] = (f != null ? f : Float.NaN); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_PRIVATE));
outputStreamWriter.write("");
outputStreamWriter.close();
logTextView.setText(String.format("File (%s) cleaned", filename));
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
} }
return array;
}
private static float round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
} }
private SensorManager getSensorManager() { private SensorManager getSensorManager() {
return (SensorManager) getSystemService(SENSOR_SERVICE); return (SensorManager) getSystemService(SENSOR_SERVICE);
} }
public void buttonPressed(){ private void prepareTextviews() {
if(startDataRegister){ downstairsTextView = (TextView) findViewById(R.id.downstairs_prob);
logTextView.setText("Writing Started"); joggingTextView = (TextView) findViewById(R.id.jogging_prob);
onResume(); sittingTextView = (TextView) findViewById(R.id.sitting_prob);
} standingTextView = (TextView) findViewById(R.id.standing_prob);
else { upstairsTextView = (TextView) findViewById(R.id.upstairs_prob);
logTextView.setText("Writing Ended"); walkingTextView = (TextView) findViewById(R.id.walking_prob);
onPause(); firstTextView = (TextView) findViewById(R.id.downstairs_title);
} secondTextView = (TextView) findViewById(R.id.jogging_title);
startDataRegister = !startDataRegister; thirdTextView = (TextView) findViewById(R.id.sitting_title);
forthTextView = (TextView) findViewById(R.id.standing_title);
fiftTextView = (TextView) findViewById(R.id.upstairs_title);
sixtTextView = (TextView) findViewById(R.id.walking_title);
logTextView = (TextView) findViewById(R.id.log);
} }
@Override private void setTextviews() {
public boolean onKeyDown(int keyCode, KeyEvent event) { firstTextView.setText(labels[0]);
secondTextView.setText(labels[1]);
if ((String.valueOf(keyCode)).equals("127") || (String.valueOf(keyCode)).equals("126")){ thirdTextView.setText(labels[2]);
buttonPressed(); forthTextView.setText(labels[3]);
} fiftTextView.setText(labels[4]);
sixtTextView.setText(labels[5]);
return super.onKeyDown(keyCode, event);
} }
private void writeToFile(String data,Context context) { private static float round(float d, int decimalPlace) {
try { BigDecimal bd = new BigDecimal(Float.toString(d));
Date date = new Date() ; bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd") ; return bd.floatValue();
filename = "accData-" + dateFormat.format(date) + ".txt"; }
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_APPEND));
outputStreamWriter.write(data);
outputStreamWriter.close();
//logTextView.setText("File (" + filename + ") updated"); private float[] toFloatArray(List<Float> list) {
} int i = 0;
catch (IOException e) { float[] array = new float[list.size()];
Log.e("Exception", "File write failed: " + e.toString()); for (Float f : list) {
array[i++] = (f != null ? f : Float.NaN);
} }
return array;
} }
//Šī funkcija ir lai notīrītu failu private void writeToFile(String data, Context context) {
private void writeToFile(Context context) {
try { try {
Date date = new Date() ; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd") ; SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
filename = "accData-" + dateFormat.format(date) + ".txt"; filename = "accData-" + dateFormat.format(date) + ".txt";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_APPEND));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_PRIVATE)); outputStreamWriter.write(data);
outputStreamWriter.write("");
outputStreamWriter.close(); outputStreamWriter.close();
} catch (IOException e) {
logTextView.setText("File (" + filename + ") cleaned");
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString()); Log.e("Exception", "File write failed: " + e.toString());
} }
} }
...@@ -313,12 +190,12 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -313,12 +190,12 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private List<Float> y; private List<Float> y;
private List<Float> z; private List<Float> z;
private List<Long> timestamps; private List<Long> timestamps;
public secondThread(List<Float> x, List<Float> y, List<Float> z, List<Long> timestamps) {
secondThread(List<Float> x, List<Float> y, List<Float> z, List<Long> timestamps) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
this.timestamps = timestamps; this.timestamps = timestamps;
//interpolateLists(timestamps, x);
data.addAll(x); data.addAll(x);
data.addAll(y); data.addAll(y);
data.addAll(z); data.addAll(z);
...@@ -329,17 +206,15 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -329,17 +206,15 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
List<Float> new_data = new ArrayList<>(); List<Float> new_data = new ArrayList<>();
for (int j = 0; j < 200; j++) { for (int j = 0; j < 200; j++) {
new_data.add(data.get(j)); new_data.add(data.get(j));
new_data.add(data.get(200+j)); new_data.add(data.get(200 + j));
new_data.add(data.get(400+j)); new_data.add(data.get(400 + j));
} }
results = classifier.predictProbabilities(toFloatArray(new_data)); results = classifier.predictProbabilities(toFloatArray(new_data));
timestamps.clear(); timestamps.clear();
x.clear(); x.clear();
y.clear(); y.clear();
z.clear(); z.clear();
float max = -1; float max = -1;
int idx = -1; int idx = -1;
for (int i = 0; i < results.length; i++) { for (int i = 0; i < results.length; i++) {
...@@ -348,50 +223,23 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -348,50 +223,23 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
max = results[i]; max = results[i];
} }
} }
if(SoundSwitch.isChecked() == true){
textToSpeech.speak(labels[idx], TextToSpeech.QUEUE_ADD, null, null); textToSpeech.speak(labels[idx], TextToSpeech.QUEUE_ADD, null, null);
}
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
Date date = new Date() ; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("mm-ss") ; SimpleDateFormat dateFormat = new SimpleDateFormat("mm-ss");
downstairsTextView.setText(Float.toString(round(results[0], 2))); downstairsTextView.setText(Float.toString(round(results[0], 2)));
joggingTextView.setText(Float.toString(round(results[1], 2))); joggingTextView.setText(Float.toString(round(results[1], 2)));
sittingTextView.setText(Float.toString(round(results[2], 2))); sittingTextView.setText(Float.toString(round(results[2], 2)));
standingTextView.setText(Float.toString(round(results[3], 2))); standingTextView.setText(Float.toString(round(results[3], 2)));
upstairsTextView.setText(Float.toString(round(results[4], 2))); upstairsTextView.setText(Float.toString(round(results[4], 2)));
walkingTextView.setText(Float.toString(round(results[5], 2))); walkingTextView.setText(Float.toString(round(results[5], 2)));
logTextView.setText(dateFormat.format(date)); logTextView.setText(dateFormat.format(date));
} }
}); });
}
private void interpolateLists(List<Long> timestamps, List<Float> x){
List<Long> timeList;
List<Float> valueList;
timeList = timestamps;
valueList = x;
List<Double> doubleTime = null;
List<Double> doubleValue = null;
for (Long i : timeList) {
doubleTime.add(i.doubleValue());
}
for (Float i : valueList) {
doubleValue.add(i.doubleValue());
}
for (int i = 0; i < timeList.size(); i++) {
Log.i(String.valueOf(i), "Time: " + String.valueOf(timeList.get(i)) + " X value:" + String.valueOf(valueList.get(i)));
}
} }
} }
} }
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -12,6 +14,24 @@ ...@@ -12,6 +14,24 @@
android:padding="22dp" android:padding="22dp"