In Google Play Services Activity Recognition there is
DetectedActivity.RUNNING,
DetectedActivity.WALKING,
DetectedActivity.ON_FOOT
when ever I get an activity update for either walking or running I get ON_FOOT
how do I differentiate?
I know it says that RUNNING and WALKING : "This is a sub-activity of ON_FOOT"
Thanks for your help.
The Activity Recognition API automatically detects activities by periodically reading short bursts of sensor data and processing them using machine learning models.
The walkingOrRunning() method provided by emil10001 works, however it will not be able to get the activity(running or walking) with the highest confidence, this because, the condition of the second if clause in his for loop always compares the activity's confidence to 0.
To clarify this, let assume we pass a List of size 2 "probableActivities" as argument to the walkingOrRunning() method i.e: we call walkingOrRunning(probableActivities).
Supposing,
List probableActivities = [ activity1, activity2 ],
where:
activity1 = "walking" with 75% confidence
activity2 = "running" 5% confidence.
In brief, the excution of the method walkingOrRunning(probableActivities) is as follows:
1)After the 1st iteration of the for loop, myActivity = "walking"
2)After the 2nd iteration of the for loop, myActivity = "running"
and the method retruns "running" as the activity type , meanwhile we expect the returned activity to be "walking".
In sum, in order to get the activity type (walking/running) with the highest confidence, I modified walkingOrRunning() method to the following
[ fyi: I have implemented and tested the code and it's working as expected, I welcome any feedback/comment/question].
private DetectedActivity walkingOrRunning(List<DetectedActivity> probableActivities) {
DetectedActivity myActivity = null;
int confidence = 0;
for (DetectedActivity activity : probableActivities) {
if (activity.getType() != DetectedActivity.RUNNING && activity.getType() != DetectedActivity.WALKING)
continue;
if (activity.getConfidence() >= confidence) {
confidence = activity.getConfidence();
myActivity = activity;
}
}
return myActivity;
}
As Sam mentioned, the WALKING
and RUNNING
activities come in as secondary activities in a list (ActivityRecognitionResult.getProbableActivities()
), and you'll need to parse them out.
// Get the update
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Get the most probable activity from the list of activities in the update
DetectedActivity mostProbableActivity = result.getMostProbableActivity();
// Get the type of activity
int activityType = mostProbableActivity.getType();
if (activityType == DetectedActivity.ON_FOOT) {
DetectedActivity betterActivity = walkingOrRunning(result.getProbableActivities());
if (null != betterActivity)
mostProbableActivity = betterActivity;
}
private DetectedActivity walkingOrRunning(List<DetectedActivity> probableActivities) {
DetectedActivity myActivity = null;
int confidence = 0;
for (DetectedActivity activity : probableActivities) {
if (activity.getType() != DetectedActivity.RUNNING && activity.getType() != DetectedActivity.WALKING)
continue;
if (activity.getConfidence() > confidence)
myActivity = activity;
}
return myActivity;
}
I tested the above code this evening, both walking and running and it seemed to do fairly well. If you don't explicitly filter on only RUNNING
or WALKING
, you will likely get erroneous results.
I've observed similar behavior. My theory is that when you receive a ActivityRecognitionResult
object, it will often contain several parseable DetectedActivity
objects each with a confidence score specified by an integer. In the case of WALKING
, there will be at least two DetectedActivity
objects - one DetectedActivity
object for ON_FOOT
with a higher confidence level, and one DetectedActivity
object for WALKING
with a lower or equal confidence level.
In practice I imagine you'll often get some permutation of ON_FOOT
+ WALKING
|| RUNNING
, or all three with varying confidence scores, with ON_FOOT
likely being the highest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With