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.video.videosift;
031
032import java.awt.event.KeyEvent;
033import java.awt.event.KeyListener;
034import java.util.List;
035
036import javax.swing.SwingUtilities;
037
038import org.openimaj.demos.video.utils.PolygonDrawingListener;
039import org.openimaj.image.FImage;
040import org.openimaj.image.MBFImage;
041import org.openimaj.image.colour.RGBColour;
042import org.openimaj.image.colour.Transforms;
043import org.openimaj.image.processing.face.detection.DetectedFace;
044import org.openimaj.image.processing.face.detection.FaceDetector;
045import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
046import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector;
047import org.openimaj.image.processing.face.detection.keypoints.FacialKeypoint;
048import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace;
049import org.openimaj.image.renderer.MBFImageRenderer;
050import org.openimaj.math.geometry.point.Point2d;
051import org.openimaj.math.geometry.shape.Rectangle;
052import org.openimaj.math.geometry.shape.Shape;
053import org.openimaj.video.VideoDisplay;
054import org.openimaj.video.VideoDisplayListener;
055import org.openimaj.video.capture.VideoCapture;
056
057public class VideoFace implements VideoDisplayListener<MBFImage>, KeyListener {
058        private VideoCapture capture;
059        private VideoDisplay<MBFImage> videoFrame;
060
061        private FaceDetector<DetectedFace, FImage> innerEngine;
062        private FKEFaceDetector engine;
063
064        private PolygonDrawingListener polygonListener;
065
066        boolean findKeypoints = false;
067
068        public VideoFace() throws Exception {
069                capture = new VideoCapture(320, 240);
070
071                innerEngine = new HaarCascadeDetector();
072                engine = new FKEFaceDetector(innerEngine);
073
074                polygonListener = new PolygonDrawingListener();
075                videoFrame = VideoDisplay.createVideoDisplay(capture);
076                videoFrame.getScreen().addMouseListener(polygonListener);
077                videoFrame.addVideoListener(this);
078                SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
079        }
080
081        @Override
082        public void afterUpdate(VideoDisplay<MBFImage> display) {
083
084        }
085
086        @Override
087        public synchronized void beforeUpdate(MBFImage frame) {
088                List<? extends DetectedFace> faces = null;
089                if (findKeypoints) {
090                        faces = engine
091                                        .detectFaces(Transforms.calculateIntensityNTSC(frame));
092                } else {
093                        faces = innerEngine.detectFaces(Transforms
094                                        .calculateIntensityNTSC(frame));
095                }
096
097                if (faces.size() > 0) {
098                        Rectangle r = faces.get(0).getBounds();
099                        ((HaarCascadeDetector) innerEngine)
100                                        .setMinSize((int) (r.width * 0.9));
101                } else {
102                        ((HaarCascadeDetector) innerEngine).setMinSize(1);
103                }
104
105                for (DetectedFace face : faces) {
106                        final Shape bounds = face.getBounds();
107
108                        MBFImageRenderer renderer = frame.createRenderer();
109                        renderer.drawPolygon(bounds.asPolygon(), RGBColour.RED);
110
111                        if (findKeypoints) {
112                                for (FacialKeypoint kp : ((KEDetectedFace) face).getKeypoints()) {
113                                        Point2d pt = kp.position.clone();
114                                        pt.translate((float) bounds.minX(), (float) bounds.minY());
115
116                                        renderer.drawPoint(pt, RGBColour.GREEN, 3);
117                                }
118                        }
119                }
120        }
121
122        @Override
123        public void keyTyped(KeyEvent e) {
124
125        }
126
127        @Override
128        public synchronized void keyPressed(KeyEvent e) {
129                if (e.getKeyChar() == 't') {
130                        findKeypoints = !findKeypoints;
131                }
132        }
133
134        @Override
135        public void keyReleased(KeyEvent e) {
136
137        }
138
139        public static void main(String[] args) throws Exception {
140                new VideoFace();
141        }
142}