Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions AndroidApp/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
compileSdkVersion 28
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "io.github.introml.activityrecognition"
minSdkVersion 21
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
Expand All @@ -25,11 +28,11 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('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.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private float[] results;
private TensorFlowClassifier classifier;

private String[] labels = {"Downstairs", "Jogging", "Sitting", "Standing", "Upstairs", "Walking"};
private final String[] labels = {"Downstairs", "Jogging", "Sitting", "Standing", "Upstairs", "Walking"};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -44,12 +44,12 @@ protected void onCreate(Bundle savedInstanceState) {
y = new ArrayList<>();
z = 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);
downstairsTextView = findViewById(R.id.downstairs_prob);
joggingTextView = findViewById(R.id.jogging_prob);
sittingTextView = findViewById(R.id.sitting_prob);
standingTextView = findViewById(R.id.standing_prob);
upstairsTextView = findViewById(R.id.upstairs_prob);
walkingTextView = findViewById(R.id.walking_prob);

classifier = new TensorFlowClassifier(getApplicationContext());

Expand Down Expand Up @@ -112,12 +112,12 @@ private void activityPrediction() {

results = classifier.predictProbabilities(toFloatArray(data));

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)));
downstairsTextView.setText(Float.toString(round(results[0])));
joggingTextView.setText(Float.toString(round(results[1])));
sittingTextView.setText(Float.toString(round(results[2])));
standingTextView.setText(Float.toString(round(results[3])));
upstairsTextView.setText(Float.toString(round(results[4])));
walkingTextView.setText(Float.toString(round(results[5])));

x.clear();
y.clear();
Expand All @@ -135,9 +135,9 @@ private float[] toFloatArray(List<Float> list) {
return array;
}

private static float round(float d, int decimalPlace) {
private static float round(float d) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class TensorFlowClassifier {
System.loadLibrary("tensorflow_inference");
}

private TensorFlowInferenceInterface inferenceInterface;
private final TensorFlowInferenceInterface inferenceInterface;
private static final String MODEL_FILE = "file:///android_asset/frozen_model.pb";
// private static final String MODEL_FILE = "file:///android_asset/frozen_har.pb";
// private static final String MODEL_FILE = "file:///android_asset/frozen_har.pb";
private static final String INPUT_NODE = "inputs";
private static final String[] OUTPUT_NODES = {"y_"};
private static final String OUTPUT_NODE = "y_";
Expand Down
2 changes: 1 addition & 1 deletion AndroidApp/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ededed"
Expand Down
8 changes: 7 additions & 1 deletion AndroidApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/jcenter/'}
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:4.1.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -14,7 +17,10 @@ buildscript {

allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/jcenter/'}
jcenter()
google()
}
}

Expand Down
2 changes: 1 addition & 1 deletion AndroidApp/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,34 @@ Interested in deeper understanding of Machine Learning algorithms? Implement the
</a>

<a href="https://leanpub.com/hmls" target="_blank">Read the book here</a>



---

### My Environments (2021)

- Android SDK Platform 28 (Android 9.0 Pie)
- Android SDK Build-Tools 30.0.3 (Higher version Supports lower version)
- a Android *Phone* with Android 10 OS (Support lower SDK built apps I guess)



**It works fine on my phone!**

<img src="assets/image-20210114132032334.png" alt="image-20210114132032334" style="zoom:50%;" />

### Changes Should be Made to Run Locally

- `gradle-wrapper.properties`
- To fit your own `gradle` version.
- BTW, my written version works fine, you can let it run.
- `build.gradle`
- Top-level
- Change to your local **repositories**, I used `aliyun`
- `build.gradle`
- App-level
- `compileSdkVersion`
- `buildToolsVersion`
- `targetSdkVersion`

Binary file added assets/image-20210114132032334.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.