Commit d83903df authored by s5_linde_o's avatar s5_linde_o
Browse files

Initial commit

parents
Pipeline #247 failed with stages
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.idea/
*.iml
.externalNativeBuild
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "io.github.introml.activityrecognition"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.tensorflow:tensorflow-android:1.12.0'
testCompile 'junit:junit:4.12'
compile 'org.apache.commons:commons-math3:3.6.1'
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\aqib_\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.introml.activityrecognition">
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="io.github.introml.activityrecognition.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
// 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;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
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 static final int N_SAMPLES = 200;
private static List<Float> x;
private static List<Float> y;
private static List<Float> 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 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"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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());
}
@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() {
getSensorManager().unregisterListener(this);
super.onPause();
}
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();
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",
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
public void onAccuracyChanged(Sensor sensor, int i) {
}
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 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;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((String.valueOf(keyCode)).equals("127") || (String.valueOf(keyCode)).equals("126")){
buttonPressed();
}
return super.onKeyDown(keyCode, event);
}
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());
}
}
//Šī funkcija ir lai notīrītu failu
private void writeToFile(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("File (" + filename + ") cleaned");
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
class secondThread implements Runnable {
List<Float> data = new ArrayList<>();
private List<Float> x;
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) {
this.x = x;
this.y = y;
this.z = z;
this.timestamps = timestamps;
//interpolateLists(timestamps, x);
data.addAll(x);
data.addAll(y);
data.addAll(z);
}
@Override
public void run() {
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));
}
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++) {
if (results[i] > max) {
idx = i;
max = results[i];
}
}
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") ;
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)));
}
}
}
}
package io.github.introml.activityrecognition;
import android.content.Context;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;
public class TensorFlowClassifier {
static {
System.loadLibrary("tensorflow_inference");
}
private TensorFlowInferenceInterface inferenceInterface;
private static final String MODEL_FILE = "file:///android_asset/workindData_w34_w35_wklibo.pb";
// private static final String MODEL_FILE = "file:///android_asset/frozen_har.pb";
//private static final String INPUT_NODE = "conv1d_1_input";
private static final String INPUT_NODE = "conv1d_1_input";
private static final String[] OUTPUT_NODES = {"dense_3/Softmax"};
private static final String OUTPUT_NODE = "dense_3/Softmax";
private static final long[] INPUT_SIZE = {1, 200, 3};
private static final int OUTPUT_SIZE = 6;
public TensorFlowClassifier(final Context context) {
inferenceInterface = new TensorFlowInferenceInterface(context.getAssets(), MODEL_FILE);
}
public float[] predictProbabilities(float[] data) {
float[] result = new float[OUTPUT_SIZE];
inferenceInterface.feed(INPUT_NODE, data, INPUT_SIZE);
inferenceInterface.run(OUTPUT_NODES);
inferenceInterface.fetch(OUTPUT_NODE, result);
//Downstairs Jogging Sitting Standing Upstairs Walking
return result;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
android:background="#ededed"
android:padding="22dp"
tools:context="io.github.introml.activityrecognition.MainActivity">
<TableRow
android:id="@+id/title_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="22dp"
android:textAlignment="center">
<TextView
android:id="@+id/title"
android:layout_weight="1"
android:text="@string/activity"
android:textAlignment="center"
android:textSize="20sp"