Introduction: Face Tracking With Processing and OpenCV
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:
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.x and faces.y. If there are two people in the screen and you want to know the location of the second person, it would be faces.x and faces.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.