001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.faces; 031 032import java.io.IOException; 033import java.util.List; 034 035import org.openimaj.demos.Demo; 036import org.openimaj.image.DisplayUtilities; 037import org.openimaj.image.FImage; 038import org.openimaj.image.ImageUtilities; 039import org.openimaj.image.MBFImage; 040import org.openimaj.image.colour.Transforms; 041import org.openimaj.image.processing.face.alignment.AffineAligner; 042import org.openimaj.image.processing.face.detection.HaarCascadeDetector; 043import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector; 044import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace; 045import org.openimaj.image.processing.resize.ResizeProcessor; 046import org.openimaj.image.renderer.MBFImageRenderer; 047import org.openimaj.math.geometry.shape.Shape; 048import org.openimaj.math.geometry.transforms.TransformUtilities; 049import org.openimaj.video.VideoDisplay; 050import org.openimaj.video.VideoDisplayListener; 051import org.openimaj.video.capture.VideoCapture; 052 053import Jama.Matrix; 054 055/** 056 * Mustache demo 057 * 058 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 059 * 060 */ 061@Demo( 062 author = "Jonathon Hare", 063 description = "Demonstration of the face keypoint pipeline by taking the " + 064 "webcam image, detecting faces and applying a moustache to the found " + 065 "faces.", 066 keywords = { "moustache", "video", "face", "webcam" }, 067 title = "Moustaches", 068 arguments = { "-v" }, 069 icon = "/org/openimaj/demos/icons/video/mustache-icon.png") 070public class Mustache { 071 MBFImage mustache; 072 private FKEFaceDetector detector; 073 074 /** 075 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 076 * 077 * @created 26 Sep 2011 078 */ 079 public static class VideoMustache { 080 private Mustache m = new Mustache(); 081 082 /** 083 * Default constructor 084 * 085 * @throws IOException 086 */ 087 public VideoMustache() throws IOException { 088 final VideoDisplay<MBFImage> vd = VideoDisplay.createVideoDisplay(new VideoCapture(320, 240)); 089 090 vd.addVideoListener(new VideoDisplayListener<MBFImage>() 091 { 092 @Override 093 public void beforeUpdate(MBFImage frame) 094 { 095 frame.internalAssign(m.addMustaches(frame)); 096 } 097 098 @Override 099 public void afterUpdate(VideoDisplay<MBFImage> display) 100 { 101 } 102 }); 103 } 104 } 105 106 /** 107 * Default constructor 108 * 109 * @throws IOException 110 */ 111 public Mustache() throws IOException { 112 this(ImageUtilities.readMBFAlpha(Mustache.class.getResourceAsStream("mustache.png"))); 113 } 114 115 /** 116 * Construct with custom mustache image 117 * 118 * @param mustache 119 */ 120 public Mustache(MBFImage mustache) { 121 this.mustache = mustache; 122 this.detector = new FKEFaceDetector(new HaarCascadeDetector(80)); 123 } 124 125 /** 126 * Detect faces in the image and render mustaches. 127 * 128 * @param image 129 * @return image with rendered mustaches 130 */ 131 public MBFImage addMustaches(MBFImage image) { 132 MBFImage cimg; 133 134 if (image.getWidth() > image.getHeight() && image.getWidth() > 640) { 135 cimg = image.process(new ResizeProcessor(640, 480)); 136 } else if (image.getHeight() > image.getWidth() && image.getHeight() > 640) { 137 cimg = image.process(new ResizeProcessor(480, 640)); 138 } else { 139 cimg = image.clone(); 140 } 141 142 final FImage img = Transforms.calculateIntensityNTSC(cimg); 143 144 final List<KEDetectedFace> faces = detector.detectFaces(img); 145 final MBFImageRenderer renderer = cimg.createRenderer(); 146 147 for (final KEDetectedFace face : faces) { 148 final Matrix tf = AffineAligner.estimateAffineTransform(face); 149 final Shape bounds = face.getBounds(); 150 151 final MBFImage m = mustache.transform(tf.times(TransformUtilities.scaleMatrix(1f / 4f, 1f / 4f))); 152 153 renderer.drawImage(m, (int) bounds.minX(), (int) bounds.minY()); 154 } 155 156 return cimg; 157 } 158 159 /** 160 * Main method. 161 * 162 * @param args 163 * @throws IOException 164 */ 165 public static void main(String[] args) throws IOException { 166 args = new String[] { "-v" }; 167 if (args.length > 0 && args[0].equals("-v")) { 168 new Mustache.VideoMustache(); 169 } else { 170 MBFImage cimg = ImageUtilities.readMBF(Mustache.class 171 .getResourceAsStream("/org/openimaj/demos/image/sinaface.jpg")); 172 173 cimg = new Mustache().addMustaches(cimg); 174 175 DisplayUtilities.display(cimg); 176 } 177 } 178}