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.File; 033import java.net.MalformedURLException; 034import java.net.URL; 035import java.util.List; 036 037import javax.swing.JFileChooser; 038import javax.swing.JFrame; 039 040import org.openimaj.demos.Demo; 041import org.openimaj.image.MBFImage; 042import org.openimaj.image.colour.RGBColour; 043import org.openimaj.image.processing.face.detection.DetectedFace; 044import org.openimaj.image.processing.face.tracking.KLTHaarFaceTracker; 045import org.openimaj.video.VideoDisplay; 046import org.openimaj.video.VideoDisplayListener; 047import org.openimaj.video.xuggle.XuggleVideo; 048 049/** 050 * A demo of the KLT/HaarCascade face tracker 051 * 052 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 053 * 054 * @created 13 Oct 2011 055 */ 056@Demo( 057 author = "David Dupplaw", 058 description = "Demonstrates the KLT Tracker on faces, with the" + 059 " OpenIMAJ FaceTracker component.", 060 keywords = { "face", "tracking", "video", "klt" }, 061 title = "Face Tracking" 062) 063public class KLTHaarFaceTrackerDemo 064{ 065 /** The face tracker */ 066 private KLTHaarFaceTracker faceTracker = new KLTHaarFaceTracker( 40 ); 067 068 /** The video with faces in to track */ 069 private XuggleVideo video =null; 070 071 int frameCounter = 0; 072 073 /** 074 * Default contructor 075 * @throws MalformedURLException 076 */ 077 public KLTHaarFaceTrackerDemo() throws MalformedURLException 078 { 079 // Load the video 080 URL url = KLTHaarFaceTrackerDemo.class.getResource("/org/openimaj/demos/video/guy_goma.mp4"); 081 if(url == null){ 082 url = getAFile().toURI().toURL(); 083 } 084 085 video = new XuggleVideo(url); 086 087 // Jump into the video to a place where there are faces. 088 video.setCurrentFrameIndex( 10 ); 089 090 VideoDisplay<MBFImage> vd = VideoDisplay.createVideoDisplay( video ); 091 vd.addVideoListener( new VideoDisplayListener<MBFImage>() 092 { 093 @Override 094 public void beforeUpdate( MBFImage frame ) 095 { 096 // Pass the image to our face tracker 097 List<DetectedFace> faces = faceTracker.trackFace( frame.flatten() ); 098 099 System.out.println( "Frame: "+(frameCounter++)+", "+faces.size()+" faces " ); 100 101 for( DetectedFace face: faces ) 102 { 103 frame.drawShape( face.getBounds(), RGBColour.RED ); 104 } 105 } 106 107 @Override 108 public void afterUpdate( VideoDisplay<MBFImage> display ) 109 { 110 } 111 }); 112 } 113 114 private File getAFile() { 115 JFileChooser c = new JFileChooser(); 116 // Demonstrate "Open" dialog: 117 int rVal = c.showOpenDialog(new JFrame()); 118 if (rVal == JFileChooser.APPROVE_OPTION) { 119 return c.getSelectedFile(); 120 } 121 return null; 122 } 123 124 /** 125 * Default main 126 * @param args Command-line arguments 127 * @throws MalformedURLException If the URL for the video isn't right 128 */ 129 public static void main( String[] args ) throws MalformedURLException 130 { 131 new KLTHaarFaceTrackerDemo(); 132 } 133}