Thursday 28 August 2014

Get images from a webcam and do some face recognition

Today I show you a simple way to get images of your webcam and do some fancy face recognition. To glue it together I use a small JavaFX application and display the video stream as well as the recognized faces. You can download or checkout the complete project from: https://github.com/tobiasdenzler/face-recognition.

Before we start you should have a quick look at the webcam-capture library we will use. It is a really cool project and offers lot more features than we are investigating now. If you are interested in image capture/recognition stuff you might also like to dive into OpenIMAJ, a quite extensive set of image analysis tools. The webcam-capture library partly depends on this toolset.

The webcam-capture library is available on maven-central. The classes from OpenIMAJ which we will need to recognize some faces you can get from http://maven.openimaj.org repository. Check the pom.xml for details.

<dependency>
    <groupId>com.github.sarxos</groupId>
    <artifactId>webcam-capture</artifactId>
    <version>0.3.9</version>
<dependency>
    <groupId>org.openimaj</groupId>
    <artifactId>faces</artifactId>
    <version>1.1</version>
</dependency>

It is quite easy to access the stream from your webcam and get an image from there. First, get your default camera, then set your view size and open the webcam. You will find some great examples on how to use the webcam-capture library on their Github site here. To get an image from the webcam you simply call getImage().

Webcam webcam = Webcam.getDefault();
webcam.setViewSize(new Dimension(640, 480));
webcam.open();

BufferedImage capture = webcam.getImage();

For face detection tasks you can use different detectors from the openimaj package. Here we use a very basic one which returns a list of the recognized face patches.

HaarCascadeDetector detector = new HaarCascadeDetector();
Lisz<DetectedFace> faces = detector.detectFaces(ImageUtilities.createFImage(capture));

That's all the magic. To use it inside a JavaFX application you start a new thread in which you read your webcam stream and do the recognition tasks. See my sample project on Github for more details.