Face Tracking With Processing and OpenCV

20,806

63

12

About: I spend my time somewhere between engineering and art.

Computer vision is incredible. It allows computational processes the chance to see the world around them. It is necessary for a lot of robotic applications, machine learning, or interactive projects. OpenCV is an open source computer vision library that makes computer vision capabilities free and available for everyone. If you want to watch a really entertaining documentary about robotics that talks about computer vision, The Great Robot Race is fantastic. 

Using OpenCV for Processing is very powerful and a lot of fun to play around with. This Instructable will help you get it installed on your computer and go over some of the capabilities of the face tracking sketch. 

Step 1: Install OpenCV Library for Processing

If you don't have it already, you will need to download and install Processing. There are a few ways of using OpenCV with Processing, however, I'm lazy and I prefer to use whatever is easiest to get up and running. Therefore, I recommend starting out by installing this library.  It doesn't look like it has been actively developed for a few years, however, it still works (after a few tweaks). 

Follow the steps that are outlined on the website. After you have installed it and moved it into your Processing libraries folder, open up Processing. If you don't know where this is, go to

Processing => Preferences

and the sketchbook location is listed at the top.

Go to this folder location. In it you should see a folder called libraries. If it isn't there, don't worry, you just have to make a new folder in that location and name it libraries. Move the OpenCV folder into your libraries folder.

Step 2: Setting Up Face Tracking

Let's track some faces!

In Processing, go to:

File => Examples => Contributed Libraries => OpenCV => face_detection

When you try and run the program, it is going to give you an error saying Cannot find a class or type named "Rectangle". To overcome this error, paste this line at the top of your code:

import java.awt.Rectangle; 

Now when you run the program, it shouldn't have any errors. If your computer doesn't have a webcam built in, you'll need to use an external webcam. 

Step 3: Features of Face Detection


You should see yourself with a rectangle around your head. As you move your head around the screen, you'll see the box follow your face.  If you turn your head to the side, the box will disappear. It isn't perfect, occasionally it seems to think that random objects behind me is a face as you can see in the video. Also, the lighting of the room you are in affects the detection. If you click and drag on the sketch window, you can change the contrast and brightness settings to help with better face detection.

Some of the most useful features with this sketch are:

X & Y coordinate locations of your face can be found with faces[some integer].x and faces[some integer].y. The number in the bracket is for finding the coordinates of a specific face if there are more than one people that it is detecting. If there is only one person to detect, it would be faces[0].x and faces[0].y. If there are two people in the screen and you want to know the location of the second person, it would be faces[1].x and faces[1].y.

Similarly, you can also find out the width and height of the detected face. This is convenient to determine how close the person is to the camera. The larger the width, you can assume that they are closer to the camera or they have a humongous head. To find the width or height, you'll use faces[some integer].width and faces[some integer].height. The integer that you use in the brackets, is the same method that you need to follow for the X and Y coordinates.

Share

    Recommendations

    • PCB Contest

      PCB Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Toys Contest

      Toys Contest

    12 Discussions

    Would you happen to know how to do this in Java? My only issue is in the webcam input; I can't figure out how to get my code to asses the image stream.

    0
    None
    BallscrewBob

    4 years ago on Step 2

    Using the exact same versions as you under windows XP pro with the exception of java version which is the current version for 32 bit.

    Exception in thread "Animation Thread" java.lang.NoSuchMethodError:

    processing.core.PApplet.registerMethod(Ljava/lang/string;Ljava/lang/Object;)V

    and a few more errors

    0
    None
    cyberogre

    5 years ago on Introduction

    Windows 8 64bit
    I am working my way thru this instructable and I am stuck running the face detection example. I couldn't load the example thru the example path, but I was able to load it by driving down to the location via File => Open. I get the error code below:

    No library found for hypermedia.video
    Libraries must be installed in a folder named 'libraries' inside the 'sketchbook' folder.

    I have OpenCV in my 'libraries' folder in the default location for Processing.

    any suggestions?

    2 replies
    0
    None
    ElectricSlimcyberogre

    Reply 5 years ago on Introduction

    I just updated to Processing 2.1 and I'm having issues with the OpenCV library as well. I'll let you know if I figure out what the issue is.

    0
    None
    tlee46

    5 years ago on Introduction

    hi many thanks for your sharing however i cant get it work due to the error

    error message: C:\Users\Yong Lee\Documents\Processing\libraries\opencv\OpenCV\library\OpenCV.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform

    A library relies on native code that's not available.
    Or only works properly when the sketch is run as a 32-bit application.

    any idea to solve this problem? thank you

    4 replies
    0
    None
    dedge4tlee46

    Reply 4 years ago on Introduction

    The problem is that you have an AMD architecture processor and the OpenCV for processing was only built to use on Intel Architecture Processors hence the (IA). As of right now I know of no other way to fix this except for finding a computer that uses Intel.... Sorry.

    0
    None
    charge11595tlee46

    Reply 4 years ago on Introduction

    Not sure if you are still following this after two months, but I think that you may need to download and use the 32 bit version of Processing to run the sketch. I really don't know if this will work on Windows though since I do use a Mac, but you could try anyway. . .

    0
    None
    dedge4charge11595

    Reply 4 years ago on Introduction

    Mac uses Intel. This is why nobody has a problem using OpenCV with Processing on Macs. Windows users have to have an Intel Processor to get this to work.

    0
    None
    noik

    5 years ago on Introduction

    Nice instructable, only problem is I can't get it working. It keeps on saying that the library isn't installed correctly. I downloaded the .rar file from the website, placed it in the library folder from processing. But on the website they somehow install it, but there isn't a .exe file or something. (i think.. ) I also added the library in processing. I hope you know the answer.

    1 reply