org.openimaj.image.processing.face.feature.FacePatchFeature Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of faces Show documentation
Show all versions of faces Show documentation
Implementation of a flexible face-recognition pipeline,
including pluggable detectors, aligners, feature extractors
and recognisers.
/**
* Copyright (c) 2011, The University of Southampton and the individual contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the University of Southampton nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.openimaj.image.processing.face.feature;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openimaj.feature.FeatureVectorProvider;
import org.openimaj.feature.FloatFV;
import org.openimaj.image.FImage;
import org.openimaj.image.pixel.Pixel;
import org.openimaj.image.processing.face.alignment.AffineAligner;
import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector;
import org.openimaj.image.processing.face.detection.keypoints.FacialKeypoint;
import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace;
import org.openimaj.image.processing.face.detection.keypoints.FacialKeypoint.FacialKeypointType;
import org.openimaj.io.ReadWriteableBinary;
import org.openimaj.io.wrappers.ReadableListBinary;
import org.openimaj.io.wrappers.WriteableListBinary;
import org.openimaj.math.geometry.point.Point2d;
import org.openimaj.math.geometry.point.Point2dImpl;
import Jama.Matrix;
/**
* A {@link FacialFeature} that is built by concatenating
* each of the normalised facial part patches from a detected
* face.
*
* @author Jonathon Hare ([email protected])
*
*/
public class FacePatchFeature implements FacialFeature, FeatureVectorProvider {
/**
* A {@link FacialFeatureExtractor} for producing {@link FacialFeature}s
*
* @author Jonathon Hare ([email protected])
*
*/
public static class Extractor implements FacialFeatureExtractor {
/**
* Default constructor
*/
public Extractor() {}
@Override
public FacePatchFeature extractFeature(KEDetectedFace face) {
FacePatchFeature f = new FacePatchFeature();
f.initialise(face);
return f;
}
@Override
public void readBinary(DataInput in) throws IOException {
//Do nothing
}
@Override
public byte[] binaryHeader() {
//Do nothing
return null;
}
@Override
public void writeBinary(DataOutput out) throws IOException {
//Do nothing
}
}
/**
* A {@link FacialKeypoint} with an associated feature
*
* @author Jonathon Hare ([email protected])
*/
public static class DetectedFacePart extends FacialKeypoint implements ReadWriteableBinary {
float [] featureVector;
int featureRadius;
/**
* Default constructor
*/
public DetectedFacePart() {
super();
}
/**
* Construct with the given parameters
* @param type the type of keypoint
* @param position the position of the keypoint
*/
public DetectedFacePart(FacialKeypointType type, Point2d position) {
super(type, position);
}
/**
* @return the image patch around the keypoint
*/
public FImage getImage() {
FImage image = new FImage(2*featureRadius+1,2*featureRadius+1);
for (int i=0, rr=-featureRadius; rr<=featureRadius; rr++) {
for (int cc=-featureRadius; cc<=featureRadius; cc++) {
float r2 = rr*rr + cc*cc;
if (r2<=featureRadius*featureRadius) { //inside circle
float value = featureVector[i++];
image.pixels[rr + featureRadius][cc + featureRadius] = value < -3 ? 0 : value >=3 ? 1 : (3f + value) / 6f;
}
}
}
return image;
}
@Override
public void readBinary(DataInput in) throws IOException {
super.readBinary(in);
int sz = in.readInt();
if (sz<0) {
featureVector = null;
} else {
featureVector = new float[sz];
for (int i=0; i faceParts = new ArrayList();
/**
* Default constructor.
*/
public FacePatchFeature() {
}
protected void initialise(KEDetectedFace face) {
extractFeatures(face);
this.featureVector = createFeatureVector();
}
protected FloatFV createFeatureVector() {
int length = faceParts.get(0).featureVector.length;
FloatFV fv = new FloatFV(faceParts.size() * length);
for (int i=0; i transformed = new ArrayList();
List nontransformed = new ArrayList();
for (int rr=-radius; rr<=radius; rr++) {
for (int cc=-radius; cc<=radius; cc++) {
float r2 = rr*rr + cc*cc;
if (r2<=radius*radius) { //inside circle
//Note: do transform without the translation!!!
float px = (float) (cc*scl* T.get(0, 0) + rr*scl*T.get(0, 1));
float py = (float) (cc*scl* T.get(1, 0) + rr*scl*T.get(1, 1));
transformed.add(new Point2dImpl(px, py));
nontransformed.add(new Pixel(cc,rr));
}
}
}
for (int j=0; j(faceParts) {
@Override
protected DetectedFacePart readValue(DataInput in) throws IOException {
DetectedFacePart v = new DetectedFacePart();
v.readBinary(in);
return v;
}
}.readBinary(in);
}
@Override
public byte[] binaryHeader() {
return this.getClass().getName().getBytes();
}
@Override
public void writeBinary(DataOutput out) throws IOException {
featureVector.writeBinary(out);
out.writeInt(radius);
out.writeFloat(scl);
new WriteableListBinary(faceParts) {
@Override
protected void writeValue(DetectedFacePart v, DataOutput out) throws IOException {
v.writeBinary(out);
}
}.writeBinary(out);
}
}