Saturday, 5 May 2012

Open CV Python Face Detection making LED Glow via Arduino

Swami Gulagulaananda said:
"There was light... I blocked my face and *poof* Off it went!"

I had some spare time on me, and I thought "You know what? Let me do something" - I decided to have a look at Open CV and Python. I am just a beginner at this point of time and have done nothing very clever. But if you share my enthusiasm, you will find this little thing pretty nice.

In my previous post, you will see how to write a very small program to get Face Detection done. Notice that you don't need to know anything about Image Processing (It is an awesome subject if you are interested in) but you can still get some stuff up and running.

After I did that and it worked, I thought, let me take this one step higher. What is the use of Face Detection if you don't do anything after that? So in this post, I show you how to make an LED glow or turn off, depending on whether a face is detected or not. It is not at all as complicated as the title seems.

All you need to have is an Arduino kit (and an external LED, resistor and breadboard which are optional because you can use the built in LED if you don't have them)

Just connect the LED and resistor to pin 13 and ground if you have them - otherwise it's just fine.

int incoming = 0;

void setup() {
   pinMode(13, OUTPUT);

void loop() {
   incoming =;
   if (incoming != -1) {
      digitalWrite(13, LOW);
   } else {
      digitalWrite(13, HIGH);


In the Python program, just import serial. For this, you should get the pyserial libraries.

Just do a serial write to the serial port when the face is detected as soon as you are drawing the rectangle. That's it! You are done.

Have a look at the working here:

Get the code here -

No comments: