Skip to content

Commit

Permalink
Add detect faces example showing how to use painter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jul 10, 2013
1 parent 7b70637 commit 4c6adbd
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 0 deletions.
1 change: 1 addition & 0 deletions webcam-capture-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<modules>
<module>webcam-capture-applet</module>
<module>webcam-capture-detect-face</module>
<module>webcam-capture-executable</module>
<module>webcam-capture-live-streaming</module>
<module>webcam-capture-manycams</module>
Expand Down
8 changes: 8 additions & 0 deletions webcam-capture-examples/webcam-capture-detect-face/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions webcam-capture-examples/webcam-capture-detect-face/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>webcam-capture-example-detect-face</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
32 changes: 32 additions & 0 deletions webcam-capture-examples/webcam-capture-detect-face/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Webcam Capture Detect Face Example

This simple example show how to use painter interface and default painter
to mark located face, which has been found using awesome [OpenIMAJ](http://www.openimaj.org/)
framework.

## What Is This

This example presents how to use custom painter with conjunction of default one to alter
image displayed in `WebcamPanel` component.

In this example I used Haar Cascade from [OpenIMAJ](http://www.openimaj.org/) to detect faces.

![OpenIMAJ](http://openimaj.org/images/OpenImaj-sq.png "OpenIMAJ")


### Screenshoots:

The Trolls:

![The Trolls](https://raw.github.com/sarxos/webcam-capture/master/webcam-capture-examples/webcam-capture-detect-face/src/etc/resources/faces.jpg "The Trolls")


## License

Copyright (C) 2013 Bartosz Firyn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 changes: 45 additions & 0 deletions webcam-capture-examples/webcam-capture-detect-face/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture-examples</artifactId>
<version>0.3.10-SNAPSHOT</version>
</parent>

<artifactId>webcam-capture-example-detect-face</artifactId>
<packaging>jar</packaging>

<name>Webcam Capture - Detect Face Example</name>
<description>Example of how to use painter interface to mark detected face</description>

<repositories>
<repository>
<id>sarxos-repo</id>
<url>http://repo.sarxos.pl/maven2</url>
</repository>
<repository>
<id>OpenIMAJ maven repository</id>
<url>http://maven.openimaj.org/</url>
</repository>
<repository>
<id>OpenIMAJ snapshots maven repository</id>
<url>http://snapshots.openimaj.org</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openimaj</groupId>
<artifactId>faces</artifactId>
<version>1.1</version>
</dependency>
</dependencies>

</project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.github.sarxos.webcam;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import org.openimaj.image.ImageUtilities;
import org.openimaj.image.processing.face.detection.DetectedFace;
import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
import org.openimaj.math.geometry.shape.Rectangle;


/**
* Paint troll smile on all detected faces.
*
* @author Bartosz Firyn (SarXos)
*/
public class FacePainterExample extends JFrame implements Runnable, WebcamPanel.Painter {

private static final long serialVersionUID = 1L;

private static final Executor EXECUTOR = Executors.newSingleThreadExecutor();
private static final HaarCascadeDetector detector = new HaarCascadeDetector();
private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f);

private Webcam webcam = null;
private WebcamPanel.Painter painter = null;
private List<DetectedFace> faces = null;
private BufferedImage troll = null;

public FacePainterExample() throws IOException {

super();

troll = ImageIO.read(getClass().getResourceAsStream("/troll-face.png"));

webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.open(true);

WebcamPanel panel = new WebcamPanel(webcam, false);
panel.setPreferredSize(WebcamResolution.VGA.getSize());
panel.setPainter(this);
panel.setFPSDisplayed(true);
panel.setFPSLimited(true);
panel.setFPS(20);
panel.setPainter(this);
panel.start();

painter = panel.getDefaultPainter();

add(panel);

setTitle("Face Detector Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);

EXECUTOR.execute(this);
}

@Override
public void run() {
while (true) {
if (!webcam.isOpen()) {
return;
}
faces = detector.detectFaces(ImageUtilities.createFImage(webcam.getImage()));
}
}

@Override
public void paintPanel(WebcamPanel panel, Graphics2D g2) {
if (painter != null) {
painter.paintPanel(panel, g2);
}
}

@Override
public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {

if (painter != null) {
painter.paintImage(panel, image, g2);
}

if (faces == null) {
return;
}

Iterator<DetectedFace> dfi = faces.iterator();
while (dfi.hasNext()) {

DetectedFace face = dfi.next();
Rectangle bounds = face.getBounds();

int dx = (int) (0.1 * bounds.width);
int dy = (int) (0.2 * bounds.height);
int x = (int) bounds.x - dx;
int y = (int) bounds.y - dy;
int w = (int) bounds.width + 2 * dx;
int h = (int) bounds.height + dy;

g2.drawImage(troll, x, y, w, h, null);
g2.setStroke(STROKE);
g2.setColor(Color.RED);
g2.drawRect(x, y, w, h);
}
}

public static void main(String[] args) throws IOException {
new FacePainterExample();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c6adbd

Please sign in to comment.