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.sitestuff;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  
37  import org.openimaj.image.DisplayUtilities;
38  import org.openimaj.image.FImage;
39  import org.openimaj.image.ImageUtilities;
40  import org.openimaj.image.MBFImage;
41  import org.openimaj.image.colour.ColourSpace;
42  import org.openimaj.image.processing.face.detection.CLMDetectedFace;
43  import org.openimaj.image.processing.face.detection.CLMFaceDetector;
44  import org.openimaj.image.processing.face.detection.DetectedFace;
45  import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
46  import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector;
47  import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace;
48  import org.openimaj.image.processing.face.util.CLMDetectedFaceRenderer;
49  import org.openimaj.image.processing.face.util.KEDetectedFaceRenderer;
50  import org.openimaj.image.processing.face.util.SimpleDetectedFaceRenderer;
51  import org.openimaj.image.processing.resize.ResizeProcessor;
52  
53  public class FaceDetSiteDemo {
54  	public static void main(String[] args) throws MalformedURLException, IOException {
55  		// Load the image
56  		FImage img = ImageUtilities.readF(new URL("file:///Users/ss/Desktop/Barack-Obama-02.jpg"));
57  		img.processInplace(new ResizeProcessor(640, 480));
58  
59  		MBFImage mbfAll = new MBFImage(img.width*3, img.height, ColourSpace.RGB);
60  		MBFImage mbf;
61  
62  		// A simple Haar-Cascade face detector
63  		HaarCascadeDetector det1 = new HaarCascadeDetector();
64  		DetectedFace face1 = det1.detectFaces(img).get(0);
65  
66  		mbf = MBFImage.createRGB(img);
67  		new SimpleDetectedFaceRenderer().drawDetectedFace(mbf,10,face1);
68  		mbfAll.drawImage(mbf, 0, 0);
69  
70  
71  		// Get the facial keypoints
72  		FKEFaceDetector det2 = new FKEFaceDetector();
73  		KEDetectedFace face2 = det2.detectFaces(img).get(0);
74  
75  		mbf = MBFImage.createRGB(img);
76  		new KEDetectedFaceRenderer().drawDetectedFace(mbf,10,face2);
77  		mbfAll.drawImage(mbf, img.width, 0);
78  
79  
80  		// With the CLM Face Model
81  		CLMFaceDetector det3 = new CLMFaceDetector();
82  		CLMDetectedFace face3 = det3.detectFaces(img).get(0);
83  
84  		mbf = MBFImage.createRGB(img);
85  		new CLMDetectedFaceRenderer().drawDetectedFace(mbf,10,face3);
86  		mbfAll.drawImage(mbf, img.width*2, 0);
87  
88  		mbfAll.processInplace(new ResizeProcessor(320,240));
89  
90  		DisplayUtilities.display(mbfAll);
91  		ImageUtilities.write(mbfAll, new File("/Users/ss/Desktop/barack-detected.png"));
92  	}
93  }