added fps display

This commit is contained in:
Mautisim Munir 2022-10-19 20:28:41 +05:00
parent f899deaafd
commit 1acaa4a5b1
4 changed files with 69 additions and 3 deletions

View File

@ -17,10 +17,12 @@ package com.google.mediapipe.examples.posetracking_lindera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@ -33,6 +35,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
/**
@ -64,6 +67,17 @@ public class MainActivity extends AppCompatActivity {
lindera.setCamera("FRONT");
lindera.setCameraRotation(CameraRotation.AUTOMATIC);
lindera.fpsHelper.onFpsUpdate = new Consumer<Double>() {
@Override
public void accept(Double fps) {
String text = "FPS: "+String.format("%04.1f" ,fps);
runOnUiThread(()-> {
TextView view = findViewById(R.id.fps_view);
view.setText(text);
});
}
};
}

View File

@ -1,15 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:id="@+id/buttons"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/buttonBarStyle" android:gravity="center"
android:gravity="center"
android:orientation="horizontal">
<Button
@ -32,11 +33,18 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Model" />
<TextView
android:id="@+id/fps_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<FrameLayout
android:id="@+id/preview_display_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/no_view"
android:layout_width="match_parent"

View File

@ -0,0 +1,42 @@
package com.google.mediapipe.solutions.lindera;
import java.util.function.Consumer;
public class FpsHelper {
// 0 no smoothing, 1 is constant output
double smoothingFactor = 0.8;
double _fps = -1;
long startTime = -1;
public FpsHelper(double smoothingFactor){
this.smoothingFactor = smoothingFactor;
}
public FpsHelper(){}
public Consumer<Double> onFpsUpdate = null;
public void logNewPoint(){
if (startTime==-1){
startTime = System.nanoTime();
}
else{
long currTime = System.nanoTime();
long dur = currTime - startTime;
startTime = currTime;
double fps = 1e9/dur;
if (_fps==-1){
_fps = fps;
}else {
_fps = (1 - smoothingFactor) * fps + _fps * smoothingFactor;
}
if (onFpsUpdate!=null) {
onFpsUpdate.accept(fps);
}
}
}
public double getFPS(){
return _fps;
}
}

View File

@ -22,6 +22,7 @@ import java.util.stream.Collectors;
public class Lindera {
private ComputerVisionPlugin plugin;
public FpsHelper fpsHelper = new FpsHelper();
private PoseTracking poseTracking;
// TODO: Verify that this is the timestamp used in Actual Plugin
private int timeStamp = 0;
@ -78,6 +79,7 @@ public class Lindera {
public void setupEventListener() {
poseTracking.setResultListener(
poseTrackingResult -> {
fpsHelper.logNewPoint();
glSurfaceView.setRenderData(poseTrackingResult);
glSurfaceView.requestRender();
ImmutableList<LandmarkProto.Landmark> landmarks = poseTrackingResult.multiPoseLandmarks();