Over a million developers have joined DZone.

Controlling Games With Face Recognition in Java (OpenIMAJ)

DZone's Guide to

Controlling Games With Face Recognition in Java (OpenIMAJ)

This quick snippet and summary will start your journey to face recognition and motion controls in games and other solutions using OpenIMAJ.

· IoT Zone ·
Free Resource

I had a free evening, so I decided to make something that I've always wanted to try: a game controlled by body movement. You know, like in the Kinect console.

I thought that it would be hard, but it turns that nowadays, it's in fact really simple.

I decided to use Java. I wanted to have a working environment as fast as possible and I wanted to achieve that with Maven. The language determines the libraries, and this time, I chose OpenIMAJ. This library is really cool because it allows you to start with just a few lines of codes. Moreover, it takes care the GUI (windows, frames, etc), video capturing, and picture processing. It is a really simple with that library.

For example, here is the code that allows you to detect faces and mark them with a red rectangle on screen:

//first initialize the screen if HD
VideoCapture vc = new VideoCapture( 1240,720 );

//initialization of face detector
FaceDetector fd = new HaarCascadeDetector(40);

//this creeates the window showing the captured webcam video
VideoDisplay vd = VideoDisplay.createVideoDisplay( vc ); vd.addVideoListener( new VideoDisplayListener() {
  //this is the listener fired on every frame
  public void beforeUpdate( MBFImage frame ) {
    //this does the face detection and display the frame around the face on screen
    List faces = fd.detectFaces( Transforms.calculateIntensity(frame));
    for( DetectedFace face : faces ) {
      frame.drawShape(face.getBounds(), RGBColour.RED);

As you can see, it's really simple. This application, from the snippet, handles not only face detection but shows the effect in a separate JFrame window.

So the rest was pretty easy. I wrote a simple Arkanoid game and used the head of the player as a paddle. It works pretty well.

I won't write about the Arkanoid details, but if you are curious, here is the complete code, and here is the video showing the game in action:

I strongly encourage you to experiment with motion detection. It turns out that it can be super easy to implement. Of course, gaming is only one solution — I think that such face detection could be used in many different areas, like recognizing a laptop user (OpenIMAJ has the ability for face recognition also), or raising an alarm when your webcam notices a face in your room.

iot ,augmented reality ,game development ,motion control ,tutorial ,face recognition

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}