Commit 0faa8b0b authored by sedmidubsky's avatar sedmidubsky
Browse files

* Decomposed orientation normalizator

* Updated caffe object extractor for NTU dataset + added batch normalization methods
parent 4ee06bc4
Loading
Loading
Loading
Loading
+22 −3
Original line number Diff line number Diff line
@@ -68,14 +68,16 @@ public class CaffeObjectMotionImageSocketExtractor {
        this.caffeObjectExtractor = Extractors.createSocketExtractor(ObjectFloatVectorNeuralNetworkL2.class, caffeObjectExtractorHost, new int[]{caffeObjectExtractorPort}, 0);
    }

    //************ Methods ************//
    //************ Static methods ************//
    /**
     * Normalizes the input sequence by all specified convertors.
     *
     * @param sequence sequence to be normalized
     * @param sequenceConvertors convertors for normalizing the sequence
     *
     * @return normalized input sequence
     */
    public SequenceMocap<?> normalizeSequence(SequenceMocap<?> sequence) {
    public static SequenceMocap<?> normalizeSequence(SequenceMocap<?> sequence, List<SequenceMocapConvertor<SequenceMocap<?>>> sequenceConvertors) {
        SequenceMocap<?> normalizedSequence = sequence;
        for (SequenceMocapConvertor<SequenceMocap<?>> sequenceConvertor : sequenceConvertors) {
            try {
@@ -88,6 +90,17 @@ public class CaffeObjectMotionImageSocketExtractor {
        return normalizedSequence;
    }

    //************ Methods ************//
    /**
     * Normalizes the input sequence by all convertors loaded in advance.
     *
     * @param sequence sequence to be normalized
     * @return normalized input sequence
     */
    public SequenceMocap<?> normalizeSequence(SequenceMocap<?> sequence) {
        return normalizeSequence(sequence, this.sequenceConvertors);
    }

    /**
     * Generates a motion image from the input sequence, which should be already
     * normalized.
@@ -148,14 +161,17 @@ public class CaffeObjectMotionImageSocketExtractor {
     * @throws Exception
     */
    public void extractObjects(String inputMotionImageFolder, String outputFilename, FilenameFilter filenameFilter) throws Exception {
        int objectCount = 0;
        FileOutputStream stream = new FileOutputStream(outputFilename);
        for (File file : new File(inputMotionImageFolder).listFiles(filenameFilter)) {
            if (file.isFile()) {
                String[] filenameSplit = file.getName().split("\\|/");
                extractObject(ImageIO.read(file), new AbstractObjectKey(filenameSplit[filenameSplit.length - 1])).write(stream);
                objectCount++;
            }
        }
        stream.close();
        System.out.println("Extracted object count: " + objectCount);
    }

    //************ Factory methods ************//
@@ -233,8 +249,11 @@ public class CaffeObjectMotionImageSocketExtractor {

    public static CaffeObjectMotionImageSocketExtractor createNTUExtractor(Class<? extends SequenceMocap<?>> sequenceClass) throws NoSuchInstantiatorException {
        // PaOaSn normalization convertors
        List<SequenceMocapConvertor<SequenceMocap<?>>> sequenceConvertors = createSequenceConvertors(sequenceClass, 30, 30, true, false, true, false, true, KinematicTree.BONE_LENGTH_MAP_HDM05, KinematicTree.KINEMATIC_TREE_VICON);
//        List<SequenceMocapConvertor<SequenceMocap<?>>> sequenceConvertors = createSequenceConvertors(sequenceClass, 30, 30, true, false, true, false, true, KinematicTree.BONE_LENGTH_MAP_HDM05, KinematicTree.KINEMATIC_TREE_VICON);
        // NTU dataset (PaOaSn normalization): minCoordValue=-21.342054, maxCoordValue=20.033237
        // PaSn normalization convertors
        // NTU dataset (PaOaSn normalization): minCoordValue=-19.65231, maxCoordValue=19.168163
        List<SequenceMocapConvertor<SequenceMocap<?>>> sequenceConvertors = createSequenceConvertors(sequenceClass, 30, 30, true, false, false, false, true, KinematicTree.BONE_LENGTH_MAP_HDM05, KinematicTree.KINEMATIC_TREE_VICON);
        MotionImageConvertor motionImageConvertor = new MotionImageConvertor(-21.342054f, 20.033237f);
        return new CaffeObjectMotionImageSocketExtractor(sequenceConvertors, motionImageConvertor, HOST_NAME, PORT_NUMBER);
    }
+56 −9
Original line number Diff line number Diff line
package mcdr.preprocessing.transformation.impl;

import java.util.ArrayList;
import java.util.List;
import mcda.commons.constants.LandmarkConstant;
import mcdr.objects.ObjectMocapPose;
import mcdr.sequence.SequenceMocap;
@@ -40,7 +42,7 @@ public class NormalizationOfOrientationConvertor<O extends SequenceMocap<?>> ext
     * @return the angle according to which the skeleton has to be rotated so
     * that the subject faces a fixed position
     */
    private float getHipsRotationAngle(ObjectMocapPose o) {
    public float getHipsRotationAngle(ObjectMocapPose o) {
        float[][] coords = o.getJointCoordinates();
        float leftHipX = coords[LandmarkConstant.getLandmarkPos(LandmarkConstant.LANDMARK_LHIPJOINT_ID)][0];
        float leftHipZ = coords[LandmarkConstant.getLandmarkPos(LandmarkConstant.LANDMARK_LHIPJOINT_ID)][2];
@@ -53,8 +55,9 @@ public class NormalizationOfOrientationConvertor<O extends SequenceMocap<?>> ext
     * Rotates the pose according to the given angle around the Y-axis.
     *
     * @param o pose to be rotated
     * @param angle the angle according to which the skeleton is rotated
     */
    private void rotatePoseByY(ObjectMocapPose o, float angle) {
    public void rotatePoseByY(ObjectMocapPose o, float angle) {
        float cosPhi = (float) Math.cos(angle);
        float sinPhi = (float) Math.sin(angle);
        for (float[] coord : o.getJointCoordinates()) {
@@ -65,6 +68,56 @@ public class NormalizationOfOrientationConvertor<O extends SequenceMocap<?>> ext
        }
    }

    /**
     * Rotates the skeleton in each pose by a specified angle.
     *
     * @param sequence sequence to be normalized
     * @param angle angle of rotation
     * @return normalized sequence
     */
    public O rotateByAngle(O sequence, float angle) {
        O ts = (O) sequence.duplicate();
        for (int i = 0; i < ts.getObjectCount(); i++) {
            ObjectMocapPose o = ts.getObject(i);
            rotatePoseByY(o, angle);
        }
        return ts;
    }

    /**
     * Rotates the skeleton in each pose by a specified angle.
     *
     * @param poses list of poses to be normalized
     * @param angle angle of rotation
     * @return normalized poses
     */
    public List<? extends ObjectMocapPose> rotateByAngle(List<? extends ObjectMocapPose> poses, float angle) {
        List<ObjectMocapPose> rtv = new ArrayList<>(poses.size());
        for (ObjectMocapPose pose : poses) {
            ObjectMocapPose o = pose.duplicate();
            rotatePoseByY(o, angle);
            rtv.add(o);
        }
        return rtv;
    }

    /**
     * Aligns the rotated pose.
     *
     * @param o rotated pose
     */
    private void alignRotatedPose(ObjectMocapPose o) {
        float[][] coords = o.getJointCoordinates();

        // We want left hip to be positive z coordinate and right hip negative
        if (!rotateByFirstPoseOnly && coords[LandmarkConstant.getLandmarkPos(LandmarkConstant.LANDMARK_LHIPJOINT_ID)][2] > coords[LandmarkConstant.getLandmarkPos(LandmarkConstant.LANDMARK_RHIPJOINT_ID)][2]) {
            rotatePoseByY(o, (float) Math.PI);
        }

        // Side view
        rotatePoseByY(o, (float) Math.PI / 2);
    }

    //************ Implemented interface Convertor ************//
    /**
     * Rotates the skeleton so that it faces a fixed angle.
@@ -79,7 +132,6 @@ public class NormalizationOfOrientationConvertor<O extends SequenceMocap<?>> ext
        float phi = Float.NaN;
        for (int i = 0; i < ts.getObjectCount(); i++) {
            ObjectMocapPose o = ts.getObject(i);
            float[][] coords = o.getJointCoordinates();

            // Computes the rotation angle
            if (!rotateByFirstPoseOnly || i == 0) {
@@ -88,13 +140,8 @@ public class NormalizationOfOrientationConvertor<O extends SequenceMocap<?>> ext

            // Rotates the pose
            rotatePoseByY(o, phi);
            // We want left hip to be positive z coordinate and right hip negative
            if (!rotateByFirstPoseOnly && coords[LandmarkConstant.getLandmarkPos(LandmarkConstant.LANDMARK_LHIPJOINT_ID)][2] > coords[LandmarkConstant.getLandmarkPos(LandmarkConstant.LANDMARK_RHIPJOINT_ID)][2]) {
                rotatePoseByY(o, (float) Math.PI);
            }

            // Side view
            rotatePoseByY(o, (float) Math.PI / 2);
            alignRotatedPose(o);
        }
        return ts;
    }