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
// 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;
import android.content.Context;
......@@ -17,6 +14,9 @@ import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import java.io.IOException;
......@@ -28,136 +28,52 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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 {
private float[] results;
private static final int N_SAMPLES = 200;
private static List<Float> x;
private static List<Float> y;
private static List<Float> z;
private static List<Float> x, y, z;
private static List<Long> timestamps;
private static List<Float> xGyro;
private static List<Float> yGyro;
private static List<Float> zGyro;
private TextView downstairsTextView;
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 static String filename;
private static String[] labels = {"Downstairs", "Limping", "Standing", "Upstairs", "Walking", ""};
private TextView downstairsTextView, joggingTextView, sittingTextView, standingTextView, upstairsTextView, walkingTextView;
private TextView firstTextView, secondTextView, thirdTextView, forthTextView, fiftTextView, sixtTextView;
private TextView logTextView;
private static boolean startDataRegister = true;
private TextToSpeech textToSpeech;
private float[] results;
private TensorFlowClassifier classifier;
private static String filename;
private String dataLine;
// 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"};
private Switch ClassifierSwitch;
private Switch SoundSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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<>();
y = new ArrayList<>();
z = 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());
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.setLanguage(Locale.US);
writeToFile(getBaseContext());
cleanFile(getBaseContext());
}
@Override
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() {
......@@ -168,46 +84,22 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
protected void onResume() {
super.onResume();
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
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
activityPrediction();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
x.add(event.values[0]);
y.add(event.values[1]);
z.add(event.values[2]);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
dataLine = String.format("%d,%s,%s,%s%n",
String 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));
writeToFile(dataLine, getBaseContext());
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
......@@ -217,92 +109,77 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private void activityPrediction() {
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();
// xGyro.clear();
// yGyro.clear();
// zGyro.clear();
}
}
private float[] toFloatArray(List<Float> list) {
int i = 0;
float[] array = new float[list.size()];
for (Float f : list) {
array[i++] = (f != null ? f : Float.NaN);
}
return array;
private void cleanFile(Context context) {
try {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
filename = "accData-" + dateFormat.format(date) + ".txt";
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());
}
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() {
return (SensorManager) getSystemService(SENSOR_SERVICE);
}
public void buttonPressed(){
if(startDataRegister){
logTextView.setText("Writing Started");
onResume();
}
else {
logTextView.setText("Writing Ended");
onPause();
}
startDataRegister = !startDataRegister;
private void prepareTextviews() {
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);
logTextView = (TextView) findViewById(R.id.log);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((String.valueOf(keyCode)).equals("127") || (String.valueOf(keyCode)).equals("126")){
buttonPressed();
private void setTextviews() {
firstTextView.setText(labels[0]);
secondTextView.setText(labels[1]);
thirdTextView.setText(labels[2]);
forthTextView.setText(labels[3]);
fiftTextView.setText(labels[4]);
sixtTextView.setText(labels[5]);
}
return super.onKeyDown(keyCode, event);
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 void writeToFile(String data,Context context) {
try {
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd") ;
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");
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
private float[] toFloatArray(List<Float> list) {
int i = 0;
float[] array = new float[list.size()];
for (Float f : list) {
array[i++] = (f != null ? f : Float.NaN);
}
return array;
}
//Šī funkcija ir lai notīrītu failu
private void writeToFile(Context context) {
private void writeToFile(String data, Context context) {
try {
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd") ;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
filename = "accData-" + dateFormat.format(date) + ".txt";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_PRIVATE));
outputStreamWriter.write("");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_APPEND));
outputStreamWriter.write(data);
outputStreamWriter.close();
logTextView.setText("File (" + filename + ") cleaned");
}
catch (IOException e) {
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
......@@ -313,12 +190,12 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private List<Float> y;
private List<Float> z;
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.y = y;
this.z = z;
this.timestamps = timestamps;
//interpolateLists(timestamps, x);
data.addAll(x);
data.addAll(y);
data.addAll(z);
......@@ -329,17 +206,15 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
List<Float> new_data = new ArrayList<>();
for (int j = 0; j < 200; j++) {
new_data.add(data.get(j));
new_data.add(data.get(200+j));
new_data.add(data.get(400+j));
new_data.add(data.get(200 + j));
new_data.add(data.get(400 + j));
}
results = classifier.predictProbabilities(toFloatArray(new_data));
timestamps.clear();
x.clear();
y.clear();
z.clear();
float max = -1;
int idx = -1;
for (int i = 0; i < results.length; i++) {
......@@ -348,50 +223,23 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
max = results[i];
}
}
if(SoundSwitch.isChecked() == true){
textToSpeech.speak(labels[idx], TextToSpeech.QUEUE_ADD, null, null);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("mm-ss") ;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("mm-ss");
downstairsTextView.setText(Float.toString(round(results[0], 2)));
joggingTextView.setText(Float.toString(round(results[1], 2)));
sittingTextView.setText(Float.toString(round(results[2], 2)));
standingTextView.setText(Float.toString(round(results[3], 2)));
upstairsTextView.setText(Float.toString(round(results[4], 2)));
walkingTextView.setText(Float.toString(round(results[5], 2)));
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 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
......@@ -12,6 +14,24 @@
android:padding="22dp"
tools:context="io.github.introml.activityrecognition.MainActivity">
<Switch
android:id="@+id/classifierSwitch"
android:layout_width="287dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Classifier"/>
<Switch
android:id="@+id/soundSwitch"
android:layout_width="287dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Sound"/>
<TableRow
android:id="@+id/title_row"
android:layout_width="match_parent"
......@@ -162,131 +182,6 @@
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/x_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="22dp">
<TextView
android:id="@+id/x_row_title"
android:layout_weight="1"
android:text="@string/acc_x"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/x_data"
android:layout_weight="1"
android:textAlignment="center"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/y_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="22dp">
<TextView
android:id="@+id/y_row_title"
android:layout_weight="1"
android:text="@string/acc_y"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/y_data"
android:layout_weight="1"
android:textAlignment="center"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/z_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="22dp">
<TextView
android:id="@+id/z_row_title"
android:layout_weight="1"
android:text="@string/acc_z"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/z_data"
android:layout_weight="1"
android:textAlignment="center"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/xGyro_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="22dp">
<TextView
android:id="@+id/xGyro_row_title"
android:layout_weight="1"
android:text="@string/gyro_x"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/xGyro_data"
android:layout_weight="1"
android:textAlignment="center"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/yGyro_row"
android:layout_width="match_parent"
android:layout_height="match_parent"