AutoBone: Make PoseRecordIO static and add height to algorithm error

This commit is contained in:
ButterscotchVanilla committed 2021-08-16 18:14:07 -04:00
1 parent c6cd13d9cd
commit e0ac3bb853
3 files changed
+128 -64

No files matched your search

@@ -107,20 +107,22 @@ public class SkeletonConfig extends EJBag {
@Override
public void run() {
try {
File recording = new File("ABRecording_Load.abf");
File saveRecording = new File("ABRecording.abf");
File loadRecording = new File("ABRecording_Load.abf");
if (recording.exists()) {
if (loadRecording.exists()) {
setText("Load");
PoseRecordIO importer = new PoseRecordIO("ABRecording_Load.abf");
PoseFrame[] frames = importer.readFromFile();
LogManager.log.info("[AutoBone] Detected recording at \"" + loadRecording.getPath() + "\", loading frames...");
PoseFrame[] frames = PoseRecordIO.readFromFile(loadRecording);
if (frames == null) {
throw new NullPointerException("Reading frames from \"ABRecording_Load.abf\" failed...");
throw new NullPointerException("Reading frames from \"" + loadRecording.getPath() + "\" failed...");
}
autoBone.setFrames(frames);
setText("Wait");
LogManager.log.info("[AutoBone] Done loading frames! Processing frames...");
} else {
setText("Move");
autoBone.startFrameRecording(250, 60);
@@ -130,10 +132,9 @@ public class SkeletonConfig extends EJBag {
}
setText("Wait");
LogManager.log.info("[AutoBone] Done recording! Exporting frames to \"ABRecording.abf\"...");
LogManager.log.info("[AutoBone] Done recording! Exporting frames to \"" + saveRecording.getPath() + "\"...");
PoseRecordIO.writeToFile(saveRecording, autoBone.getFrames());
PoseRecordIO exporter = new PoseRecordIO("ABRecording.abf");
exporter.writeToFile(autoBone.getFrames());
LogManager.log.info("[AutoBone] Done exporting! Processing frames...");
}
@@ -13,13 +13,13 @@ import io.eiren.vr.processor.HumanSkeletonWithWaist;
public class AutoBone {
protected final static int MIN_DATA_DISTANCE = 5;
protected final static int MIN_DATA_DISTANCE = 4;
protected final static int MAX_DATA_DISTANCE = 10;
protected final static int NUM_EPOCHS = 100;
protected final static int NUM_EPOCHS = 200;
protected final static float INITIAL_ADJUSTMENT_RATE = 1f;
protected final static float ADJUSTMENT_RATE_DECAY = 1.05f;
protected final static float INITIAL_ADJUSTMENT_RATE = 0.9f;
protected final static float ADJUSTMENT_RATE_DECAY = 1.057f;
protected final VRServer server;
@@ -35,7 +35,7 @@ public class AutoBone {
protected final SimpleSkeleton skeleton2;
public final HashMap<String, Float> configs = new HashMap<String, Float>() {{
put("Head", HumanSkeletonWithWaist.HEAD_SHIFT_DEFAULT);
//put("Head", HumanSkeletonWithWaist.HEAD_SHIFT_DEFAULT);
put("Neck", HumanSkeletonWithWaist.NECK_LENGTH_DEFAULT);
put("Waist", 0.85f);
put("Chest", 0.42f);
@@ -59,7 +59,7 @@ public class AutoBone {
public void reloadConfigValues() {
// Load waist configs
configs.put("Head", server.config.getFloat("body.headShift", HumanSkeletonWithWaist.HEAD_SHIFT_DEFAULT));
//configs.put("Head", server.config.getFloat("body.headShift", HumanSkeletonWithWaist.HEAD_SHIFT_DEFAULT));
configs.put("Neck", server.config.getFloat("body.neckLength", HumanSkeletonWithWaist.NECK_LENGTH_DEFAULT));
configs.put("Waist", server.config.getFloat("body.waistDistance", 0.85f));
configs.put("Chest", server.config.getFloat("body.chestDistance", 0.42f));
@@ -127,6 +127,16 @@ public class AutoBone {
this.frames = frames;
}
public float getHeight() {
float height = 0f;
for (float length : configs.values()) {
height += length;
}
return height;
}
public void processFrames() {
int epochs = NUM_EPOCHS;
int epochCounter = 0;
@@ -138,6 +148,8 @@ public class AutoBone {
float sumError = 0f;
int errorCount = 0;
float originalHeight = getHeight();
for (;;) {
// Detect end of iteration
if (cursorOffset >= frames.length || cursorOffset > MAX_DATA_DISTANCE) {
@@ -211,12 +223,21 @@ public class AutoBone {
for (Entry<String, Float> entry : configs.entrySet()) {
float originalLength = entry.getValue();
float newLength = originalLength + adjustVal;
float heightChange = originalHeight - (getHeight() + adjustVal);
// Use Math.abs() to retain the sign while being squared
float heightAdjustVal = (heightChange * Math.abs(heightChange));
float newLength = originalLength + adjustVal + heightAdjustVal;
updateSekeletonBoneLength(entry.getKey(), newLength);
float newError = getFootError(skeleton1, skeleton2);
if (newError >= error) {
newLength = originalLength - adjustVal;
heightChange = originalHeight - (getHeight() - adjustVal);
// Use Math.abs() to retain the sign while being squared
heightAdjustVal = (heightChange * Math.abs(heightChange));
newLength = (originalLength - adjustVal) + heightAdjustVal;
updateSekeletonBoneLength(entry.getKey(), newLength);
newError = getFootError(skeleton1, skeleton2);
@@ -240,6 +261,8 @@ public class AutoBone {
}
} while (++frameCursor1 < frames.length && ++frameCursor2 < frames.length);
}
LogManager.log.info("[AutoBone] Original height: " + originalHeight + " New height: " + getHeight());
}
protected static float getFootError(SimpleSkeleton skeleton1, SimpleSkeleton skeleton2) {
@@ -15,78 +15,118 @@ import com.jme3.math.Vector3f;
import io.eiren.util.logging.LogManager;
public class PoseRecordIO {
public final class PoseRecordIO {
protected final File file;
public PoseRecordIO(String file) {
this.file = new File(file);
private PoseRecordIO() {
// Do not allow instantiating
}
public boolean writeToFile(PoseFrame[] frames) {
public static boolean writeToFile(File file, PoseFrame[] frames) {
try (DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
// Write every frame
outputStream.writeInt(frames.length);
for (PoseFrame frame : frames) {
// Write root position vector
outputStream.writeFloat(frame.rootPos.x);
outputStream.writeFloat(frame.rootPos.y);
outputStream.writeFloat(frame.rootPos.z);
// Write rotations
outputStream.writeInt(frame.rotations.size());
for (Entry<String, Quaternion> entry : frame.rotations.entrySet()) {
// Write the label string
outputStream.writeUTF(entry.getKey());
// Write the rotation quaternion
Quaternion quat = entry.getValue();
outputStream.writeFloat(quat.getX());
outputStream.writeFloat(quat.getY());
outputStream.writeFloat(quat.getZ());
outputStream.writeFloat(quat.getW());
}
writeFrame(outputStream, frame);
}
} catch (Exception e) {
LogManager.log.severe("Error writing to file", e);
LogManager.log.severe("Error writing frames to file", e);
return false;
}
return true;
}
public PoseFrame[] readFromFile() {
public static boolean writeFrame(DataOutputStream outputStream, PoseFrame frame) {
try {
// Write root position vector
outputStream.writeFloat(frame.rootPos.x);
outputStream.writeFloat(frame.rootPos.y);
outputStream.writeFloat(frame.rootPos.z);
// Write rotations
outputStream.writeInt(frame.rotations.size());
for (Entry<String, Quaternion> entry : frame.rotations.entrySet()) {
// Write the label string
outputStream.writeUTF(entry.getKey());
// Write the rotation quaternion
Quaternion quat = entry.getValue();
outputStream.writeFloat(quat.getX());
outputStream.writeFloat(quat.getY());
outputStream.writeFloat(quat.getZ());
outputStream.writeFloat(quat.getW());
}
} catch (Exception e) {
LogManager.log.severe("Error writing frame to stream", e);
return false;
}
return true;
}
public static boolean writeFrame(File file, PoseFrame frame) {
try (DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
writeFrame(outputStream, frame);
} catch (Exception e) {
LogManager.log.severe("Error writing frame to file", e);
return false;
}
return true;
}
public static PoseFrame[] readFromFile(File file) {
try (DataInputStream inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) {
int frameCount = inputStream.readInt();
PoseFrame[] frames = new PoseFrame[frameCount];
for (int i = 0; i < frameCount; i++) {
float vecX = inputStream.readFloat();
float vecY = inputStream.readFloat();
float vecZ = inputStream.readFloat();
Vector3f vector = new Vector3f(vecX, vecY, vecZ);
int rotationCount = inputStream.readInt();
HashMap<String, Quaternion> rotations = new HashMap<String, Quaternion>(rotationCount);
for (int j = 0; j < rotationCount; j++) {
String label = inputStream.readUTF();
float quatX = inputStream.readFloat();
float quatY = inputStream.readFloat();
float quatZ = inputStream.readFloat();
float quatW = inputStream.readFloat();
Quaternion quaternion = new Quaternion(quatX, quatY, quatZ, quatW);
rotations.put(label, quaternion);
}
frames[i] = new PoseFrame(vector, rotations);
frames[i] = readFrame(inputStream);
}
return frames;
} catch (Exception e) {
LogManager.log.severe("Error reading from file", e);
LogManager.log.severe("Error reading frames from file", e);
}
return null;
}
public static PoseFrame readFrame(DataInputStream inputStream) {
try {
float vecX = inputStream.readFloat();
float vecY = inputStream.readFloat();
float vecZ = inputStream.readFloat();
Vector3f vector = new Vector3f(vecX, vecY, vecZ);
int rotationCount = inputStream.readInt();
HashMap<String, Quaternion> rotations = new HashMap<String, Quaternion>(rotationCount);
for (int j = 0; j < rotationCount; j++) {
String label = inputStream.readUTF();
float quatX = inputStream.readFloat();
float quatY = inputStream.readFloat();
float quatZ = inputStream.readFloat();
float quatW = inputStream.readFloat();
Quaternion quaternion = new Quaternion(quatX, quatY, quatZ, quatW);
rotations.put(label, quaternion);
}
return new PoseFrame(vector, rotations);
} catch (Exception e) {
LogManager.log.severe("Error reading frame from stream", e);
}
return null;
}
public static PoseFrame readFrame(File file) {
try {
return readFrame(new DataInputStream(new BufferedInputStream(new FileInputStream(file))));
} catch (Exception e) {
LogManager.log.severe("Error reading frame from file", e);
}
return null;