Skip to content
This repository was archived by the owner on Jul 2, 2023. It is now read-only.

Commit 5ce5860

Browse files
committed
Initial commit
0 parents  commit 5ce5860

File tree

126 files changed

+10721
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+10721
-0
lines changed

LICENSE

+674
Large diffs are not rendered by default.

README.md

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# **BeSafeBox - fall detection Android app**
2+
3+
[![API](https://img.shields.io/badge/API-19%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=19) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
4+
5+
![together](images/together.png)
6+
7+
BeSafeBox is a fall detection Android application. With one button, you can start a background service, which will track your activity - accelerometer data. If you collapse / fall down on a ground, the model will detect an impact and inactivity afterwards. If the fall is recognized, the alarm is triggered. It will try to attract attention from surrounding in hope, that someone will notice you that you need help probably. You can use the unlocked screen to call help for yourself or dismiss the alarm. First responder can also use your phone to contact emergency services, because it will override lock of the screen and show the emergency button to a dialer. If no one is responding to alarm, the app sends SMS and mail after 60s to emergency contact with coordinates and formatted Google map link to your location from GPS. For higher reliability, the phone should be placed in pocket of trousers. The app is easy to use, it is only one button to click. :older_woman::older_man:
8+
9+
## About implementation
10+
11+
BeSafeBox uses picked math features calculated from acceleration of the impact event. These features are then passed to the machine learning model, which will decide about the event. The model was trained on the 3260 simulated events by real people, which resemble the fall of the person like sitting, laying down or walking or is the actual fall of the person. **The model reached accuracy of 90.5%, with precision is 78% and recall 90,5% in average for the falls alone.** The app was tested also on the real life data with length of 300h (no fall occurred), when from 4000 indications for the fall, only 1.53% triggered false alarm. You have to take into account, that these data were gather by young people, who led active life and went to gym and other physical activities, where the resemblance to fall can be high.
12+
13+
**We measured additional 104 falls independently to main dataset and 93 / 104 (89,42%) were correctly classified.** :white_check_mark:
14+
15+
**This is purely about Android app implementations, so for more information, numbers, related work and about research on this topic, go to the separate repository:**
16+
17+
## **[BeSafeBox - research](https://github.com/Foxpace/BeSafeBox_research)** - **open Jupyter notebook Research.ipynb** :blue_book:
18+
19+
## About technical implementation
20+
21+
These project has 2 iterations, when the approach to fall detection was changed from feedforward neural network to random forest with simpler features. This resulted in higher accuracy, when we managed to get **from 86% to 90.5%**. Whole project is programmed mainly in **Java, but some parts are in Kotlin and C++.**
22+
23+
### Java implementation - main objects​
24+
25+
Whole detection is made under background service **DetectionService**, which is completely independent from the main activity (no binding between them). There are 2 main objects, **BatteryOptimizer** and **ModelManager**.
26+
27+
**BatteryOptimizer** controls life cycle of the ModelManager. The app is active while the phone is placed in pocket - controls value of proximity sensor. If the sensor has 0 proximity, the detector is active. If the value is higher, the model goes to sleep. Also it monitors state of the battery, so the detection is turned off, when the battery is low. Also it can monitor activity with Activity recognition API in Android / screen activity. If the person is actively moving, the detector is active, but if he stops, the detector stops too. Unfortunately, this API comes with a lot of drawbacks, because of battery restrictions. Read about it later in the text.
28+
29+
**ModelManager** controls all the technical aspects required for the detector. It consists of temporary storage for sensor samples, sensor manager, GPS handler and creates instances of the detector by requirement. There is only one used, but the implementation can be easily expanded to consist multiple detectors.
30+
31+
**DetectorFall** has method to process all the sensor samples and ask for the storage, when it is needed from ModelManager. It has access to machine learning model and other methods to calculate all the features by requirement.
32+
33+
Java implementation has iteration of the detector with neural network and also with random forest.
34+
35+
### Fall algorithm implementation :deciduous_tree:
36+
37+
1. waits for the first sample with magnitude higher than 3g
38+
2. waits for 750ms to pass - the impact is occurring - BUFFER
39+
3. if another 3g value appears, the indication is restarted
40+
4. if no 3g value appears for next 5s, the last 10s are passed for next evaluation
41+
5. if the average activity (mean value) after the main event (ends with last value of 1.5g) is below 10.25 m/s2, the indication is passed to machine leaning model
42+
6. if the signal consists values below 2 m/s2, it is high chance, that phone is falling freely on the ground (not user) - this condition is omitted if the user uses "active in pocket" option
43+
7. calculation of features --> decision of the model --> triggers alarm or the detector is restarted for next events
44+
45+
### Diagram of the implementation
46+
47+
### ![fall_detection_diagram](images/fall_detection_diagram.png)
48+
49+
50+
51+
### C++ implementation - main objects 🧠
52+
53+
C++ implementation consists only neural network implementation and the temporary storage, other aspects are still controlled by BatteryOptimizer and ModelManager. Prior to creation of simpler model with random forest, the simple Tensorflow neural network with feedforward layers was created with accuracy of 86%. It was created due to easy implementation back then and high performance. The weights and biases were extracted from Python objects to raw C++ vectors. Whole calculation of the neural network was also implemented with objects, which can create network of any depth, if the parameters are provided.
54+
55+
Due to the Android architecture, the C++ implementation has structure of state model, when the detector goes from the one state to the another in dependence to the behavior of the acceleration signal. Otherwise, the concept remained the same like in the Java implementation. The samples and other commands are provided through JNI (Java native interface).
56+
57+
**States:**
58+
59+
1. **FILLING** - not enough samples to process event
60+
2. **WAITING** - waits for the 3g value
61+
3. **BUFFER** - saving another samples - occurring impact
62+
4. **HIGH_ACTIVITY** - waits for 5s. If another 3g value comes, goes to the WAITING again
63+
5. otherwise -> analyze signal like in the java implementation
64+
65+
![cpp_diagram](images/cpp_algorithm.png)
66+
67+
## Car accident detection :car:
68+
69+
Part of the bachelor work was not only the fall detection, but detection car collision also. It is impossible to record complete range of the acceleration, which occurs during car crash. Phone has range of 8g, but during car crash, the phone can go through dozens of gs. The algorithm has to take from other perspective. The proposed algorithm has multiple conditions, which does not take into account high accelerations.
70+
71+
1. Phone is not used during the car driving like during daily life -> waiting for the acceleration higher than 4g - hard to achieve with normal use
72+
2. waiting for the new regular GPS income in specific time interval
73+
3. if car accident occurred, it would result in stopping the car
74+
1. this can be detected with change of speed with difference of 20km/h and higher
75+
2. during the drive, car vibrates in some manner, so the variance of the acceleration is higher in comparison with idle car
76+
3. no detection of movement by activity recognition API - user can get out of the car with phone / put it in a pocket
77+
4. phone can be in charger of the car, so we can be certain of static phone
78+
79+
All this can be done, if the phone survives the car accident of course. This is only proposal based on more than 2500 km of car driving data from different types of road in 5 different countries in EU.
80+
81+
**The car accident detection is not part of the created Android app, because it would create more confusion than sense. However, the implementation would be easy -> new Detector object would be sufficient.**
82+
83+
### Car accident diagram
84+
85+
<img src="images/car_collision_detection.png" width="251" height="641">
86+
87+
88+
## Why not on Google Play or in real use ? Battery restrictions of different OEMs ... :battery::x::bangbang:
89+
90+
The app is not a problem itself, but phone manufacturers are. The manufactures implement their own battery optimization routines, which put to sleep / kill background apps to safe battery. Usually there is not even way how to avoid it, even if user wants it. The user is required to dig in settings to turn off the optimizations or to add app to white list. However, there is no unified way of doing it. Google lacks policy / will to enforce the rules, which would force manufacturers to unify battery optimization techniques. Phones from Sony or Pixels are likely to function properly, but in case of Samsung, Huawei, OnePlus and etc. it can be lost cause. It has become next to impossible to create app for the background for phones, which use newer Androids.
91+
92+
You can still try app by downloading APK file and installing it to your phone. It is recommended to check [dontkillmyapp.com](https://dontkillmyapp.com/) website, where you can find tutorials how to avoid some battery restrictions. There are many ways, but it can be hard to explain to a lot of people without some technical background. This is another reason, why the app is not on Google Play. Developer does not have all the tools to go around all the battery restrictions at one move and make life easier for the user.
93+
94+
To see more examples of other devs, you can check Google issue tracker for Android: [Chinese OEMs constantly violating Android compliance](https://issuetracker.google.com/issues/122098785) (not only Chinese)
95+
96+
Until the policy about battery restrictions will not be resolved, this project is discontinued meanwhile. :see_no_evil:
97+
98+
## Disclaimer :point_left:
99+
100+
BeSafeBox is only prototype app, not fully fledged product. Creator of the app does not provide any kind of warranty or guarantees of reliability of the app. If you use the app, you use it on your own full responsibility. Creator does not take any responsibility for misbehaving of the app or for any injuries taken during the usage of the app. The app can send SMS messages, which cost is not covered by the creator.
101+
102+
## Citation - BibTeX
103+
104+
For citation and more resources, you can check:
105+
106+
```
107+
@thesis{Repcik2019,
108+
author = {Rep{\v{c}}{\'{i}}k, Tom{\'{a}}{\v{s}}},
109+
title = {{Detection of car accident and collapse by Android smartphone}},
110+
keywords = {Android,Collapse,Java,Python,car accident,machine learning,neural network},
111+
pages = {72},
112+
publisher = {Brno University of Technology. Faculty of Electrical Engineering and Communication},
113+
school = {Brno University of Technology},
114+
type = {Bachelor's Thesis},
115+
url = {http://dspace.lib.vutbr.cz/xmlui/handle/11012/173624},
116+
month = {june},
117+
year = {2019}
118+
}
119+
```
120+
121+
## Libraries used in code and thanks goes to:
122+
123+
[AppIntro](https://github.com/AppIntro/AppIntro) - Make a cool intro for your Android app. [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
124+
125+
[CountryCodePickerProject](https://github.com/hbb20/CountryCodePickerProject) - Country Code Picker (CCP) is an android library which provides an easy way to search and select country or international phone code. Also supports Android EditText phone mask and international phone validation. [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
126+
127+
[LicensesDialog](https://github.com/PSDev/LicensesDialog) - LicensesDialog is an open source library to display licenses of third-party libraries in an Android app [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
128+
129+
[android-about-page](https://github.com/medyo/android-about-page) - Create an awesome About Page for your Android App in 2 minutes [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
130+
131+
[maildroid](https://github.com/nedimf/maildroid) - Maildroid is a small robust android library for sending emails using SMTP server [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
132+
133+
[CircularProgressBar](https://github.com/lopspower/CircularProgressBar) - Create circular ProgressBar in Android ⭕ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
134+
135+
[slidetoact](https://github.com/cortinico/slidetoact) - A simple 'Slide to Unlock' Material widget for Android, written in Kotlin 📱🎨🦄 [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
136+
137+
[android-ripple-pulse-animation](https://github.com/gaurav414u/android-ripple-pulse-animation) - A cool ripple and pulse background animation for android [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
138+
139+
[material-dialogs](https://github.com/afollestad/material-dialogs) - 😍 A beautiful, fluid, and extensible dialogs API for Kotlin & Android [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
140+
141+
[Toasty](https://github.com/GrenderG/Toasty) - The usual Toast, but with steroids 💪 [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) (no changes to library)
142+
143+
[Dexter](https://github.com/Karumi/Dexter) - Android library that simplifies the process of requesting permissions at runtime [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) (no changes to library)
144+
145+
146+
147+
## Resources used in code:
148+
149+
**1.**
150+
**authors:** SANTOYO-RAMÓN, José Antonio, Eduardo CASILARI and José Manue CANO-GARCÍA\
151+
**work:** _Analysis of a smartphone-based architecture with multiple mobility sensors for fall detection with
152+
supervised learning._\
153+
**DOI:** [doi:10.3390/s18041155](https://doi.org/10.3390/s18041155)
154+
155+
**2.**
156+
**authors:** FIGUEIREDO, Isabel N., Carlos LEAL, Luís PINTO, Jason BOLITO a André LEMOS.\
157+
**work:** Exploring smartphone sensors for fall detection\
158+
**DOI**: [doi:10.1186/s13678-016-0004-1](https://doi.org/10.1186/s13678-016-0004-1)
159+
160+
**3.**
161+
**authors:** ABBATE, Stefano, Marco AVVENUTI, Guglielmo COLA, Paolo CORSINI, Janet LIGHT a Alessio VECCHIO.\
162+
**work:** Recognition of false alarms in fall detection systems\
163+
**DOI:** [doi:10.1109/CCNC.2011.5766464](https://doi.org/10.1109/CCNC.2011.5766464)
164+
165+
**4.**
166+
**author:** HJORT, Bo\
167+
**work:** EEG Analysis Based On Time Domain Properties\
168+
**DOI:** [doi:10.1016/0013-4694(70)90143-4](https://doi.org/10.1016/0013-4694(70)90143-4)
169+
170+
**5.**
171+
**author:** MARAGOS, P., J.F. KAISER a T.F. QUATIERI\
172+
**work:** On amplitude and frequency demodulation using energy operators\
173+
**DOI:** [doi:10.1109/78.212729](https://doi.org/10.1109/78.212729)

build.gradle

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
ext.kotlin_version = "1.4.32"
5+
repositories {
6+
google()
7+
jcenter()
8+
}
9+
dependencies {
10+
classpath 'com.android.tools.build:gradle:4.1.3'
11+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12+
classpath 'com.google.gms:google-services:4.3.5'
13+
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
14+
// NOTE: Do not place your application dependencies here; they belong
15+
// in the individual module build.gradle files
16+
}
17+
}
18+
19+
allprojects {
20+
repositories {
21+
google()
22+
jcenter()
23+
maven {url "https://maven.java.net/content/groups/public/"}
24+
maven {url 'https://google.bintray.com/tensorflow'}
25+
maven {url 'https://maven.google.com/'}
26+
maven {url "https://jitpack.io" }
27+
}
28+
}
29+
30+
task clean(type: Delete) {
31+
delete rootProject.buildDir
32+
}

gradle.properties

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Project-wide Gradle settings.
2+
# IDE (e.g. Android Studio) users:
3+
# Gradle settings configured through the IDE *will override*
4+
# any settings specified in this file.
5+
# For more details on how to configure your build environment visit
6+
# http://www.gradle.org/docs/current/userguide/build_environment.html
7+
# Specifies the JVM arguments used for the daemon process.
8+
# The setting is particularly useful for tweaking memory settings.
9+
android.enableJetifier=true
10+
android.useAndroidX=true
11+
org.gradle.jvmargs=-Xmx1536m
12+
# When configured, Gradle will run in incubating parallel mode.
13+
# This option should only be used with decoupled projects. More details, visit
14+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
15+
# org.gradle.parallel=true
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sun Mar 28 17:15:16 CEST 2021
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

images/alarm.png

416 KB
Loading

images/car_collision_detection.png

152 KB
Loading

images/cpp_algorithm.png

1 MB
Loading

images/fall_detection_diagram.png

2.5 MB
Loading

images/main_active.png

358 KB
Loading

images/main_inactive.png

356 KB
Loading

images/together.png

162 KB
Loading

mobile/CMakeLists.txt

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Sets the minimum version of CMake required to build your native library.
2+
# This ensures that a certain set of CMake features is available to
3+
# your build.
4+
5+
cmake_minimum_required(VERSION 3.4.1)
6+
7+
# Specifies a library name, specifies whether the library is STATIC or
8+
# SHARED, and provides relative paths to the source code. You can
9+
# define multiple libraries by adding multiple add_library() commands,
10+
# and CMake builds them for you. When you build your app, Gradle
11+
# automatically packages shared libraries with your APK.
12+
13+
14+
add_library( # Specifies the name of the library.
15+
native-lib
16+
17+
# Sets the library as a shared library.
18+
SHARED
19+
20+
# Provides a relative path to your source file(s).
21+
src/main/cpp/native-lib.cpp
22+
src/main/cpp/NativeModel.cpp
23+
src/main/cpp/calculations/Calculations.cpp
24+
src/main/cpp/calculations/Classifier.cpp
25+
src/main/cpp/datatypes/SensorOutput.cpp
26+
src/main/cpp/datatypes/DataCarrier.cpp
27+
src/main/cpp/calculations/nn/KerasSequentialModel.cpp
28+
src/main/cpp/calculations/nn/Parameters.cpp)
29+
30+
include_directories(src/main/cpp/include/)
31+
32+
find_library( # Defines the name of the path variable that stores the
33+
# location of the NDK library.
34+
35+
android-lib
36+
37+
# Specifies the name of the NDK library that
38+
# you want CMake to locate.
39+
log
40+
android)
41+
42+
target_link_libraries( # Specifies the target library.
43+
native-lib
44+
app-glue
45+
android
46+
# Links the log library to the target library.
47+
${log-lib}
48+
${android-lib})
49+
50+
add_library( app-glue
51+
STATIC
52+
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
53+
54+
# You need to link static libraries against your shared native library.

0 commit comments

Comments
 (0)