mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-04 15:52:35 -08:00
Implement AutoBone into the protocol (#187)
Implement event-based AutoBoneHandler & more
This commit is contained in:
1 parent
766cf9ae72
commit
215f3b9f44
11 files changed
+758
-372
No files matched your search
@@ -1,38 +1,36 @@
|
||||
package dev.slimevr.gui;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.EnumMap;
|
||||
|
||||
import io.eiren.util.ann.AWTThread;
|
||||
|
||||
import javax.swing.event.MouseInputAdapter;
|
||||
|
||||
import dev.slimevr.VRServer;
|
||||
import dev.slimevr.autobone.AutoBone;
|
||||
import dev.slimevr.autobone.AutoBoneListener;
|
||||
import dev.slimevr.autobone.AutoBoneProcessType;
|
||||
import dev.slimevr.autobone.AutoBone.Epoch;
|
||||
import dev.slimevr.gui.swing.EJBox;
|
||||
import dev.slimevr.poserecorder.PoseFrames;
|
||||
import dev.slimevr.poserecorder.PoseRecorder;
|
||||
import dev.slimevr.vr.processor.skeleton.SkeletonConfigValue;
|
||||
import io.eiren.util.StringUtils;
|
||||
import io.eiren.util.ann.AWTThread;
|
||||
import io.eiren.util.collections.FastList;
|
||||
import io.eiren.util.logging.LogManager;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.event.MouseInputAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
||||
public class AutoBoneWindow extends JFrame {
|
||||
public class AutoBoneWindow extends JFrame implements AutoBoneListener {
|
||||
|
||||
private EJBox pane;
|
||||
|
||||
private final transient VRServer server;
|
||||
private final transient SkeletonConfigGUI skeletonConfig;
|
||||
private final transient PoseRecorder poseRecorder;
|
||||
private final transient AutoBone autoBone;
|
||||
private final EJBox pane;
|
||||
private transient Thread recordingThread = null;
|
||||
private transient Thread saveRecordingThread = null;
|
||||
private transient Thread autoBoneThread = null;
|
||||
|
||||
private JButton saveRecordingButton;
|
||||
private JButton adjustButton;
|
||||
private JButton applyButton;
|
||||
|
||||
private JLabel processLabel;
|
||||
@@ -43,8 +41,6 @@ public class AutoBoneWindow extends JFrame {
|
||||
|
||||
this.server = server;
|
||||
this.skeletonConfig = skeletonConfig;
|
||||
this.poseRecorder = new PoseRecorder(server);
|
||||
this.autoBone = new AutoBone(server);
|
||||
|
||||
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
|
||||
add(
|
||||
@@ -55,16 +51,11 @@ public class AutoBoneWindow extends JFrame {
|
||||
)
|
||||
);
|
||||
|
||||
server.getAutoBoneHandler().addListener(this);
|
||||
|
||||
build();
|
||||
}
|
||||
|
||||
private float processFrames(PoseFrames frames) {
|
||||
return autoBone
|
||||
.processFrames(frames, autoBone.calcInitError, autoBone.targetHeight, (epoch) -> {
|
||||
processLabel.setText(epoch.toString());
|
||||
lengthsLabel.setText(autoBone.getLengthsString());
|
||||
});
|
||||
}
|
||||
|
||||
@AWTThread
|
||||
private void build() {
|
||||
@@ -76,69 +67,11 @@ public class AutoBoneWindow extends JFrame {
|
||||
addMouseListener(new MouseInputAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// Prevent running multiple times
|
||||
if (!isEnabled() || recordingThread != null) {
|
||||
if (!isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
if (poseRecorder.isReadyToRecord()) {
|
||||
setText("Recording...");
|
||||
// 1000 samples at 20 ms per
|
||||
// sample is
|
||||
// 20 seconds
|
||||
int sampleCount = server.config
|
||||
.getInt("autobone.sampleCount", 1000);
|
||||
long sampleRate = server.config
|
||||
.getLong("autobone.sampleRateMs", 20L);
|
||||
Future<PoseFrames> framesFuture = poseRecorder
|
||||
.startFrameRecording(sampleCount, sampleRate);
|
||||
PoseFrames frames = framesFuture.get();
|
||||
LogManager.info("[AutoBone] Done recording!");
|
||||
|
||||
saveRecordingButton.setEnabled(true);
|
||||
adjustButton.setEnabled(true);
|
||||
|
||||
if (
|
||||
server.config
|
||||
.getBoolean(
|
||||
"autobone.saveRecordings",
|
||||
false
|
||||
)
|
||||
) {
|
||||
setText("Saving...");
|
||||
autoBone.saveRecording(frames);
|
||||
}
|
||||
} else {
|
||||
setText("Not Ready...");
|
||||
LogManager.severe("[AutoBone] Unable to record...");
|
||||
Thread.sleep(3000); // Wait for
|
||||
// 3
|
||||
// seconds
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
setText("Recording Failed...");
|
||||
LogManager.severe("[AutoBone] Failed recording!", e);
|
||||
try {
|
||||
Thread.sleep(3000); // Wait for
|
||||
// 3
|
||||
// seconds
|
||||
} catch (Exception e1) {
|
||||
// Ignore
|
||||
}
|
||||
} finally {
|
||||
setText("Start Recording");
|
||||
recordingThread = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
recordingThread = thread;
|
||||
thread.start();
|
||||
server.getAutoBoneHandler().startRecording();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -146,289 +79,30 @@ public class AutoBoneWindow extends JFrame {
|
||||
|
||||
add(saveRecordingButton = new JButton("Save Recording") {
|
||||
{
|
||||
setEnabled(poseRecorder.hasRecording());
|
||||
setEnabled(false);
|
||||
addMouseListener(new MouseInputAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// Prevent running multiple times
|
||||
if (!isEnabled() || saveRecordingThread != null) {
|
||||
if (!isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Future<PoseFrames> framesFuture = poseRecorder
|
||||
.getFramesAsync();
|
||||
if (framesFuture != null) {
|
||||
setText("Waiting for Recording...");
|
||||
PoseFrames frames = framesFuture.get();
|
||||
|
||||
if (frames.getTrackerCount() <= 0) {
|
||||
throw new IllegalStateException(
|
||||
"Recording has no trackers"
|
||||
);
|
||||
}
|
||||
|
||||
if (frames.getMaxFrameCount() <= 0) {
|
||||
throw new IllegalStateException(
|
||||
"Recording has no frames"
|
||||
);
|
||||
}
|
||||
|
||||
setText("Saving...");
|
||||
autoBone.saveRecording(frames);
|
||||
|
||||
setText("Recording Saved!");
|
||||
try {
|
||||
Thread.sleep(3000); // Wait
|
||||
// for 3
|
||||
// seconds
|
||||
} catch (Exception e1) {
|
||||
// Ignore
|
||||
}
|
||||
} else {
|
||||
setText("No Recording...");
|
||||
LogManager
|
||||
.severe(
|
||||
"[AutoBone] Unable to save, no recording was done..."
|
||||
);
|
||||
try {
|
||||
Thread.sleep(3000); // Wait
|
||||
// for 3
|
||||
// seconds
|
||||
} catch (Exception e1) {
|
||||
// Ignore
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
setText("Saving Failed...");
|
||||
LogManager
|
||||
.severe("[AutoBone] Failed to save recording!", e);
|
||||
try {
|
||||
Thread.sleep(3000); // Wait for
|
||||
// 3
|
||||
// seconds
|
||||
} catch (Exception e1) {
|
||||
// Ignore
|
||||
}
|
||||
} finally {
|
||||
setText("Save Recording");
|
||||
saveRecordingThread = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
saveRecordingThread = thread;
|
||||
thread.start();
|
||||
server.getAutoBoneHandler().saveRecording();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
add(adjustButton = new JButton("Auto-Adjust") {
|
||||
add(new JButton("Auto-Adjust") {
|
||||
{
|
||||
// If there are files to load, enable the button
|
||||
setEnabled(
|
||||
poseRecorder.hasRecording()
|
||||
|| (AutoBone.getLoadDir().isDirectory()
|
||||
&& AutoBone.getLoadDir().list().length > 0)
|
||||
);
|
||||
addMouseListener(new MouseInputAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// Prevent running multiple times
|
||||
if (!isEnabled() || autoBoneThread != null) {
|
||||
if (!isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
setText("Load...");
|
||||
List<Pair<String, PoseFrames>> frameRecordings = autoBone
|
||||
.loadRecordings();
|
||||
|
||||
if (!frameRecordings.isEmpty()) {
|
||||
LogManager.info("[AutoBone] Done loading frames!");
|
||||
} else {
|
||||
Future<PoseFrames> framesFuture = poseRecorder
|
||||
.getFramesAsync();
|
||||
if (framesFuture != null) {
|
||||
setText("Waiting for Recording...");
|
||||
PoseFrames frames = framesFuture.get();
|
||||
|
||||
if (frames.getTrackerCount() <= 0) {
|
||||
throw new IllegalStateException(
|
||||
"Recording has no trackers"
|
||||
);
|
||||
}
|
||||
|
||||
if (frames.getMaxFrameCount() <= 0) {
|
||||
throw new IllegalStateException(
|
||||
"Recording has no frames"
|
||||
);
|
||||
}
|
||||
|
||||
frameRecordings
|
||||
.add(Pair.of("<Recording>", frames));
|
||||
} else {
|
||||
setText("No Recordings...");
|
||||
LogManager
|
||||
.severe(
|
||||
"[AutoBone] No recordings found in \""
|
||||
+ AutoBone.getLoadDir().getPath()
|
||||
+ "\" and no recording was done..."
|
||||
);
|
||||
try {
|
||||
Thread.sleep(3000); // Wait
|
||||
// for 3
|
||||
// seconds
|
||||
} catch (Exception e1) {
|
||||
// Ignore
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setText("Processing...");
|
||||
LogManager.info("[AutoBone] Processing frames...");
|
||||
FastList<Float> heightPercentError = new FastList<Float>(
|
||||
frameRecordings.size()
|
||||
);
|
||||
for (
|
||||
Pair<String, PoseFrames> recording : frameRecordings
|
||||
) {
|
||||
LogManager
|
||||
.info(
|
||||
"[AutoBone] Processing frames from \""
|
||||
+ recording.getKey()
|
||||
+ "\"..."
|
||||
);
|
||||
|
||||
heightPercentError
|
||||
.add(processFrames(recording.getValue()));
|
||||
LogManager.info("[AutoBone] Done processing!");
|
||||
applyButton.setEnabled(true);
|
||||
|
||||
// #region Stats/Values
|
||||
Float neckLength = autoBone
|
||||
.getConfig(SkeletonConfigValue.NECK);
|
||||
Float chestDistance = autoBone
|
||||
.getConfig(SkeletonConfigValue.CHEST);
|
||||
Float torsoLength = autoBone
|
||||
.getConfig(SkeletonConfigValue.TORSO);
|
||||
Float hipWidth = autoBone
|
||||
.getConfig(SkeletonConfigValue.HIPS_WIDTH);
|
||||
Float legsLength = autoBone
|
||||
.getConfig(SkeletonConfigValue.LEGS_LENGTH);
|
||||
Float kneeHeight = autoBone
|
||||
.getConfig(SkeletonConfigValue.KNEE_HEIGHT);
|
||||
|
||||
float neckTorso = neckLength != null
|
||||
&& torsoLength != null
|
||||
? neckLength
|
||||
/ torsoLength
|
||||
: 0f;
|
||||
float chestTorso = chestDistance != null
|
||||
&& torsoLength != null
|
||||
? chestDistance
|
||||
/ torsoLength
|
||||
: 0f;
|
||||
float torsoWaist = hipWidth != null
|
||||
&& torsoLength != null
|
||||
? hipWidth
|
||||
/ torsoLength
|
||||
: 0f;
|
||||
float legTorso = legsLength != null
|
||||
&& torsoLength != null
|
||||
? legsLength
|
||||
/ torsoLength
|
||||
: 0f;
|
||||
float legBody = legsLength != null
|
||||
&& torsoLength != null
|
||||
&& neckLength != null
|
||||
? legsLength
|
||||
/ (torsoLength + neckLength)
|
||||
: 0f;
|
||||
float kneeLeg = kneeHeight != null
|
||||
&& legsLength != null
|
||||
? kneeHeight
|
||||
/ legsLength
|
||||
: 0f;
|
||||
|
||||
LogManager
|
||||
.info(
|
||||
"[AutoBone] Ratios: [{Neck-Torso: "
|
||||
+ StringUtils.prettyNumber(neckTorso)
|
||||
+ "}, {Chest-Torso: "
|
||||
+ StringUtils.prettyNumber(chestTorso)
|
||||
+ "}, {Torso-Waist: "
|
||||
+ StringUtils.prettyNumber(torsoWaist)
|
||||
+ "}, {Leg-Torso: "
|
||||
+ StringUtils.prettyNumber(legTorso)
|
||||
+ "}, {Leg-Body: "
|
||||
+ StringUtils.prettyNumber(legBody)
|
||||
+ "}, {Knee-Leg: "
|
||||
+ StringUtils.prettyNumber(kneeLeg)
|
||||
+ "}]"
|
||||
);
|
||||
|
||||
String lengthsString = autoBone.getLengthsString();
|
||||
LogManager
|
||||
.info(
|
||||
"[AutoBone] Length values: " + lengthsString
|
||||
);
|
||||
lengthsLabel.setText(lengthsString);
|
||||
}
|
||||
|
||||
if (!heightPercentError.isEmpty()) {
|
||||
float mean = 0f;
|
||||
for (float val : heightPercentError) {
|
||||
mean += val;
|
||||
}
|
||||
mean /= heightPercentError.size();
|
||||
|
||||
float std = 0f;
|
||||
for (float val : heightPercentError) {
|
||||
float stdVal = val - mean;
|
||||
std += stdVal * stdVal;
|
||||
}
|
||||
std = (float) Math
|
||||
.sqrt(std / heightPercentError.size());
|
||||
|
||||
LogManager
|
||||
.info(
|
||||
"[AutoBone] Average height error: "
|
||||
+ StringUtils.prettyNumber(mean, 6)
|
||||
+ " (SD "
|
||||
+ StringUtils.prettyNumber(std, 6)
|
||||
+ ")"
|
||||
);
|
||||
}
|
||||
// #endregion
|
||||
} catch (Exception e) {
|
||||
setText("Failed...");
|
||||
LogManager.severe("[AutoBone] Failed adjustment!", e);
|
||||
try {
|
||||
Thread.sleep(3000); // Wait for
|
||||
// 3
|
||||
// seconds
|
||||
} catch (Exception e1) {
|
||||
// Ignore
|
||||
}
|
||||
} finally {
|
||||
setText("Auto-Adjust");
|
||||
autoBoneThread = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
autoBoneThread = thread;
|
||||
thread.start();
|
||||
server.getAutoBoneHandler().processRecording();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -444,7 +118,7 @@ public class AutoBoneWindow extends JFrame {
|
||||
return;
|
||||
}
|
||||
|
||||
autoBone.applyConfig();
|
||||
server.getAutoBoneHandler().applyValues();
|
||||
// Update GUI values after applying
|
||||
skeletonConfig.refreshAll();
|
||||
}
|
||||
@@ -464,7 +138,7 @@ public class AutoBoneWindow extends JFrame {
|
||||
pane.add(new EJBox(BoxLayout.LINE_AXIS) {
|
||||
{
|
||||
setBorder(new EmptyBorder(i(5)));
|
||||
add(lengthsLabel = new JLabel(autoBone.getLengthsString()));
|
||||
add(lengthsLabel = new JLabel("No config changes"));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -473,4 +147,73 @@ public class AutoBoneWindow extends JFrame {
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAutoBoneProcessStatus(
|
||||
AutoBoneProcessType processType,
|
||||
String message,
|
||||
long current,
|
||||
long total,
|
||||
boolean completed,
|
||||
boolean success
|
||||
) {
|
||||
if (message != null) {
|
||||
if (total == 0) {
|
||||
processLabel.setText(String.format("%s: %s", processType.name(), message));
|
||||
} else {
|
||||
processLabel
|
||||
.setText(
|
||||
String
|
||||
.format(
|
||||
"%s (%d/%d) [%.2f%%]: %s",
|
||||
processType.name(),
|
||||
current,
|
||||
total,
|
||||
(current / (double) total) * 100.0,
|
||||
message
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (total != 0) {
|
||||
processLabel
|
||||
.setText(
|
||||
String
|
||||
.format(
|
||||
"%s (%d/%d) [%.2f%%]",
|
||||
processType.name(),
|
||||
current,
|
||||
total,
|
||||
(current / (double) total) * 100.0
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAutoBoneRecordingEnd(PoseFrames recording) {
|
||||
saveRecordingButton.setEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAutoBoneEpoch(Epoch epoch) {
|
||||
processLabel
|
||||
.setText(
|
||||
String
|
||||
.format(
|
||||
"PROCESS: Epoch %d/%d (%.2f%%) Error: %.4f",
|
||||
epoch.epoch,
|
||||
epoch.totalEpochs,
|
||||
(epoch.epoch / (double) epoch.totalEpochs) * 100.0,
|
||||
epoch.epochError
|
||||
)
|
||||
);
|
||||
lengthsLabel.setText(server.getAutoBoneHandler().getLengthsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAutoBoneEnd(EnumMap<SkeletonConfigValue, Float> configValues) {
|
||||
applyButton.setEnabled(true);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user