json format conversion working
This commit is contained in:
parent
008f1a0bfa
commit
39d6041f03
|
@ -49,6 +49,7 @@ android_binary(
|
|||
"@maven//:androidx_fragment_fragment",
|
||||
"@maven//:com_google_guava_guava",
|
||||
"@maven//:com_afollestad_material_dialogs_core",
|
||||
"@maven//:androidx_annotation_annotation",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -1,18 +1,118 @@
|
|||
package com.google.mediapipe.examples.posetracking_lindera;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.mediapipe.solutions.lindera.BodyJoints;
|
||||
import com.google.mediapipe.solutions.lindera.ComputerVisionPlugin;
|
||||
import com.google.mediapipe.solutions.lindera.XYZPointWithConfidence;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Locale;
|
||||
|
||||
public class ComputerVisionPluginImpl implements ComputerVisionPlugin {
|
||||
|
||||
LinkedList<BodyJointsEvent> bodyJointsEventList = new LinkedList<>();
|
||||
static class BodyJointsEvent{
|
||||
BodyJoints bodyJoints;
|
||||
Long timestamp;
|
||||
|
||||
public BodyJointsEvent(long timestamp, BodyJoints bodyJoints) {
|
||||
this.bodyJoints = bodyJoints;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
boolean isLogging = false;
|
||||
|
||||
public void startLogging(){
|
||||
isLogging = true;
|
||||
bodyJointsEventList = new LinkedList<>();
|
||||
}
|
||||
|
||||
public JSONObject stopLoggingAndDumpOutput() throws JSONException, IllegalAccessException {
|
||||
isLogging = false;
|
||||
// base json string
|
||||
String json = "{\n" +
|
||||
" \"identifier\": \"some_name_here.Capture\",\n" +
|
||||
" \"metaData\": {\n" +
|
||||
" \"userIdentifier\": \"unknown user\",\n" +
|
||||
" \"originatingEquipment\": \"Apperture\",\n" +
|
||||
" \"activityName\": \"exercise name\",\n" +
|
||||
" \"activityDescription\": \"\",\n" +
|
||||
" \"tags\": [\n" +
|
||||
" \n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"bodyObjects\": [\n" +
|
||||
" \n" +
|
||||
" ]}";
|
||||
|
||||
JSONObject eLog = new JSONObject(json);
|
||||
JSONArray bodyJointsArr = new JSONArray();
|
||||
for (BodyJointsEvent bodyJointsEvent:bodyJointsEventList){
|
||||
JSONObject jBodyJointEvent = new JSONObject();
|
||||
jBodyJointEvent.put("ts",bodyJointsEvent.timestamp);
|
||||
BodyJoints bodyJoints = bodyJointsEvent.bodyJoints;
|
||||
String bodyJointsString = "";
|
||||
// iterate over fields of BodyJoints and put them in json format
|
||||
for (Field field : BodyJoints.class.getDeclaredFields()) {
|
||||
Class<?> type = field.getType();
|
||||
|
||||
if (type == XYZPointWithConfidence.class) {
|
||||
|
||||
String name = field.getName();
|
||||
// get abbreviation of name for example leftShoulder -> LS
|
||||
String abbrev = String.valueOf(name.charAt(0));
|
||||
for (int i = 1;i<name.length();++i){
|
||||
char chari = name.charAt(i);
|
||||
if (Character.isUpperCase(chari)){
|
||||
abbrev = abbrev.concat(String.valueOf(chari));
|
||||
}
|
||||
}
|
||||
abbrev = abbrev.toUpperCase(Locale.ROOT);
|
||||
XYZPointWithConfidence data = (XYZPointWithConfidence) field.get(bodyJoints);
|
||||
assert data != null;
|
||||
bodyJointsString = bodyJointsString.concat(String.format(abbrev+":%f,%f,%f=",data.x,data.y,data.z));
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
// remove the last equal sign
|
||||
bodyJointsString = bodyJointsString.substring(0,bodyJointsString.length()-1);
|
||||
|
||||
jBodyJointEvent.put("bj",bodyJointsString);
|
||||
|
||||
bodyJointsArr.put(jBodyJointEvent);
|
||||
|
||||
|
||||
|
||||
}
|
||||
eLog.put("bodyJoints",bodyJointsArr);
|
||||
|
||||
return eLog;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void bodyJoints(int timestamp, BodyJoints bodyJoints) {
|
||||
XYZPointWithConfidence nose = bodyJoints.nose;
|
||||
Log.v("ComputerVisionPluginImpl", String.format(
|
||||
if (isLogging){
|
||||
this.bodyJointsEventList.add(new BodyJointsEvent(timestamp,bodyJoints));
|
||||
|
||||
}
|
||||
// XYZPointWithConfidence nose = bodyJoints.nose;
|
||||
// Log.v("ComputerVisionPluginImpl", String.format(
|
||||
//
|
||||
// "Lindera BodyJoint of Nose: x=%f, y=%f, z=%f", nose.x, nose.y, nose.z));
|
||||
|
||||
|
||||
"Lindera BodyJoint of Nose: x=%f, y=%f, z=%f", nose.x, nose.y, nose.z));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,27 +14,49 @@
|
|||
|
||||
package com.google.mediapipe.examples.posetracking_lindera;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.MediaStore;
|
||||
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 android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.mediapipe.solutions.lindera.BodyJoints;
|
||||
import com.google.mediapipe.solutions.lindera.CameraRotation;
|
||||
import com.google.mediapipe.solutions.lindera.ComputerVisionPlugin;
|
||||
import com.google.mediapipe.solutions.lindera.Lindera;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.FileStore;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
|
@ -45,9 +67,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
private static final String TAG = "MainActivity";
|
||||
|
||||
private Lindera lindera;
|
||||
private ComputerVisionPlugin plugin;
|
||||
private ComputerVisionPluginImpl plugin;
|
||||
private boolean isLinderaInitialized = false;
|
||||
private boolean isDetectionStarted = false;
|
||||
private boolean isLoggingStarted = false;
|
||||
|
||||
// Live camera demo UI and camera components.
|
||||
|
||||
|
||||
|
@ -59,8 +83,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
findViewById(R.id.button_set_model).setVisibility(View.GONE);
|
||||
findViewById(R.id.button_toggle_landmarks).setVisibility(View.GONE);
|
||||
findViewById(R.id.button_start_capture).setVisibility(View.GONE);
|
||||
setupLiveDemoUiComponents();
|
||||
plugin = new ComputerVisionPluginImpl();
|
||||
|
||||
|
||||
lindera = new Lindera(plugin);
|
||||
List<String> cameras = lindera.getAvailableCameras();
|
||||
// FRONT or BACK
|
||||
|
@ -89,7 +116,9 @@ public class MainActivity extends AppCompatActivity {
|
|||
Button startDetectionButton = findViewById(R.id.button_start_detection);
|
||||
Button toggleLandmarks = findViewById(R.id.button_toggle_landmarks);
|
||||
Button modelComplexity = findViewById(R.id.button_set_model);
|
||||
Button startCapture = findViewById(R.id.button_start_capture);
|
||||
FrameLayout frameLayout = findViewById(R.id.preview_display_layout);
|
||||
|
||||
startDetectionButton.setOnClickListener(
|
||||
v -> {
|
||||
// startCameraButton.setVisibility(View.GONE);
|
||||
|
@ -100,6 +129,8 @@ public class MainActivity extends AppCompatActivity {
|
|||
startDetectionButton.setVisibility(View.GONE);
|
||||
findViewById(R.id.button_set_model).setVisibility(View.VISIBLE);
|
||||
findViewById(R.id.button_toggle_landmarks).setVisibility(View.VISIBLE);
|
||||
findViewById(R.id.button_start_capture).setVisibility(View.VISIBLE);
|
||||
|
||||
updateLandmarkButtonText();
|
||||
updateModelComplexityButtonText();
|
||||
});
|
||||
|
@ -118,6 +149,54 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
);
|
||||
|
||||
startCapture.setOnClickListener(v->{
|
||||
|
||||
if (isLoggingStarted){
|
||||
startCapture.setText("Start Capture");
|
||||
isLoggingStarted = false;
|
||||
try {
|
||||
JSONObject jsonObject = plugin.stopLoggingAndDumpOutput();
|
||||
// save to downloads folder
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "data.json");
|
||||
values.put(MediaStore.MediaColumns.MIME_TYPE, "application/json");
|
||||
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
|
||||
|
||||
final ContentResolver resolver = getContentResolver();
|
||||
Uri uri = null;
|
||||
final Uri contentUri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
|
||||
uri = resolver.insert(contentUri, values);
|
||||
final OutputStream stream = resolver.openOutputStream(uri);
|
||||
stream.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
|
||||
stream.close();
|
||||
Toast.makeText(getApplicationContext(), "data.json saved to Downloads", Toast.LENGTH_LONG).show();
|
||||
|
||||
|
||||
} catch (JSONException | IllegalAccessException | IOException e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(getApplicationContext(), "Failed to save data", Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
ProgressBar pbar = new ProgressBar(this);
|
||||
frameLayout.addView(pbar);
|
||||
final Handler handler = new Handler(Looper.getMainLooper());
|
||||
startCapture.setEnabled(false);
|
||||
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
frameLayout.removeView(pbar);
|
||||
startCapture.setEnabled(true);
|
||||
startCapture.setText("Stop Capture");
|
||||
plugin.startLogging();
|
||||
}
|
||||
}, 5000);
|
||||
isLoggingStarted = true;
|
||||
}
|
||||
});
|
||||
|
||||
modelComplexity.setOnClickListener(v->{
|
||||
int modelComplexityVal = lindera.getModelComplexity();
|
||||
|
||||
|
@ -207,4 +286,5 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -10,8 +10,7 @@
|
|||
style="?android:attr/buttonBarStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:orientation="horizontal">
|
||||
android:gravity="center">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_start_detection"
|
||||
|
@ -34,6 +33,13 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:text="Set Model" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_start_capture"
|
||||
style="?android:attr/buttonBarButtonStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Start Capture" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/fps_view"
|
||||
android:layout_width="wrap_content"
|
||||
|
|
Loading…
Reference in New Issue
Block a user