View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos.video.videosift;
31  
32  import java.awt.event.KeyAdapter;
33  import java.awt.event.KeyEvent;
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.List;
37  
38  import javax.swing.JOptionPane;
39  import javax.swing.SwingUtilities;
40  
41  import org.openimaj.feature.FloatFV;
42  import org.openimaj.feature.FloatFVComparison;
43  import org.openimaj.image.FImage;
44  import org.openimaj.image.MBFImage;
45  import org.openimaj.image.colour.RGBColour;
46  import org.openimaj.image.processing.face.alignment.CLMAligner;
47  import org.openimaj.image.processing.face.detection.CLMDetectedFace;
48  import org.openimaj.image.processing.face.feature.LocalLBPHistogram;
49  import org.openimaj.image.processing.face.feature.comparison.FaceFVComparator;
50  import org.openimaj.image.processing.face.feature.comparison.FacialFeatureComparator;
51  import org.openimaj.image.processing.face.recognition.AnnotatorFaceRecogniser;
52  import org.openimaj.image.processing.face.tracking.clm.CLMFaceTracker;
53  import org.openimaj.image.typography.hershey.HersheyFont;
54  import org.openimaj.io.IOUtils;
55  import org.openimaj.math.geometry.point.Point2d;
56  import org.openimaj.ml.annotation.AnnotatedObject;
57  import org.openimaj.ml.annotation.ScoredAnnotation;
58  import org.openimaj.ml.annotation.basic.KNNAnnotator;
59  import org.openimaj.video.VideoDisplay;
60  import org.openimaj.video.VideoDisplayListener;
61  import org.openimaj.video.capture.VideoCapture;
62  
63  public class VideoFaceRecognition extends KeyAdapter implements VideoDisplayListener<MBFImage> {
64  	private VideoCapture capture;
65  	private VideoDisplay<MBFImage> videoFrame;
66  
67  	private AnnotatorFaceRecogniser<CLMDetectedFace, String> recogniser;
68  	private CLMFaceTracker engine;
69  	private FImage currentFrame;
70  
71  	public VideoFaceRecognition() throws Exception {
72  		capture = new VideoCapture(320, 240);
73  		engine = new CLMFaceTracker();
74  		engine.fpd = 120;
75  		// engine.fcheck = true;
76  
77  		videoFrame = VideoDisplay.createVideoDisplay(capture);
78  		videoFrame.addVideoListener(this);
79  		SwingUtilities.getRoot(videoFrame.getScreen()).addKeyListener(this);
80  
81  		final LocalLBPHistogram.Extractor<CLMDetectedFace> extractor = new
82  				LocalLBPHistogram.Extractor<CLMDetectedFace>(
83  						new CLMAligner(), 20, 20, 8, 1);
84  		final FacialFeatureComparator<LocalLBPHistogram> comparator = new
85  				FaceFVComparator<LocalLBPHistogram, FloatFV>(
86  						FloatFVComparison.EUCLIDEAN);
87  		final KNNAnnotator<CLMDetectedFace, String, LocalLBPHistogram> knn = KNNAnnotator.create(extractor, comparator,
88  				1, 5f);
89  		// final CLMShapeFeature.Extractor extractor = new
90  		// CLMShapeFeature.Extractor();
91  		// final FacialFeatureComparator<CLMShapeFeature> comparator = new
92  		// FaceFVComparator<CLMShapeFeature, DoubleFV>(
93  		// DoubleFVComparison.EUCLIDEAN);
94  		//
95  		// final KNNAnnotator<CLMDetectedFace, String,
96  		// CLMShapeFeature.Extractor, CLMShapeFeature> knn =
97  		// KNNAnnotator.create(extractor, comparator, 1, 5f);
98  
99  		recogniser = AnnotatorFaceRecogniser.create(knn);
100 	}
101 
102 	@Override
103 	public synchronized void keyPressed(KeyEvent key) {
104 		if (key.getKeyCode() == KeyEvent.VK_SPACE) {
105 			this.videoFrame.togglePause();
106 		} else if (key.getKeyChar() == 'c') {
107 			// if (!this.videoFrame.isPaused())
108 			// this.videoFrame.togglePause();
109 
110 			final String person = JOptionPane.showInputDialog(this.videoFrame.getScreen(), "", "",
111 					JOptionPane.QUESTION_MESSAGE);
112 
113 			final List<CLMDetectedFace> faces = detectFaces();
114 			if (faces.size() == 1) {
115 				recogniser.train(new AnnotatedObject<CLMDetectedFace, String>(faces.get(0), person));
116 			} else {
117 				System.out.println("Wrong number of faces found");
118 			}
119 
120 			// this.videoFrame.togglePause();
121 		} else if (key.getKeyChar() == 'd') {
122 			engine.reset();
123 		}
124 		// else if (key.getKeyChar() == 'q') {
125 		// if (!this.videoFrame.isPaused())
126 		// this.videoFrame.togglePause();
127 		//
128 		// final List<CLMDetectedFace> faces = detectFaces();
129 		// if (faces.size() == 1) {
130 		// System.out.println("Looks like: " +
131 		// recogniser.annotate(faces.get(0)));
132 		// } else {
133 		// System.out.println("Wrong number of faces found");
134 		// }
135 		//
136 		// this.videoFrame.togglePause();
137 		// }
138 		else if (key.getKeyChar() == 's') {
139 			try {
140 				final File f = new File("rec.bin");
141 				f.delete();
142 				IOUtils.writeBinaryFull(f, this.recogniser);
143 			} catch (final IOException e) {
144 				e.printStackTrace();
145 			}
146 		} else if (key.getKeyChar() == 'l') {
147 			try {
148 				final File f = new File("rec.bin");
149 				this.recogniser = IOUtils.read(f);
150 			} catch (final IOException e) {
151 				// TODO Auto-generated catch block
152 				e.printStackTrace();
153 			}
154 		}
155 	}
156 
157 	private List<CLMDetectedFace> detectFaces() {
158 		return CLMDetectedFace.convert(engine.model.trackedFaces, currentFrame);
159 	}
160 
161 	@Override
162 	public void afterUpdate(VideoDisplay<MBFImage> display) {
163 		// do nothing
164 	}
165 
166 	@Override
167 	public synchronized void beforeUpdate(MBFImage frame) {
168 		this.currentFrame = frame.flatten();
169 		engine.track(frame);
170 		engine.drawModel(frame, true, true, true, true, true);
171 
172 		if (recogniser != null && recogniser.listPeople().size() >= 1) {
173 			for (final CLMDetectedFace f : detectFaces()) {
174 				final List<ScoredAnnotation<String>> name = recogniser.annotate(f);
175 
176 				if (name.size() > 0) {
177 					final Point2d r = f.getBounds().getTopLeft();
178 					frame.drawText(name.get(0).annotation, r, HersheyFont.ROMAN_SIMPLEX, 15, RGBColour.GREEN);
179 				}
180 			}
181 		}
182 	}
183 
184 	public static void main(String[] args) throws Exception {
185 		new VideoFaceRecognition();
186 	}
187 }