# Laser Gesture Recognition

, 9 Jan 2007
 Rate this:
A quick article on setting up a simple, real-time laser gesture recognition application and using it to control Windows Media Player.

## Introduction

Wouldn't it be great if we could somehow give visual commands to our computer without touching the keyboard or mouse? In this article, we will put together a simple laser gesture recognition application and use it to control Windows Media Player. This is far more comfortable than using a remote control because you don't have to look for the correct buttons in the dark. All you have to do is make a few simple gestures anywhere in the camera's field of view with a laser pointer, and that's it! This program recognizes simple gestures made on a wall with a laser pointer such as left, right, up, down, two downward and two upward diagonals. This program could be modified to recognize some more gestures; however, it cannot recognize complex gestures since I haven't taken a neural network approach for image recognition.

## Video

Our first step is to get a video feed into our application from a webcam. We can use DirectX's component DirectShow for accomplishing this. I have directly used Andrew Kirillov's Motion Detection code[^] (with permission) for image acquisition. I modified the code in MotionDetector1.cs to perform laser gesture recognition.

## Recognition

The program searches for the brightest pixel in its field of view (which is a laser dot, in our case) with luminance above a certain threshold value. Luminance of a pixel can be calculated using its RGB values, with a simple formula:

`Luminance = (299 * red + 587 * green + 114 * blue) / 1000`

After it finds the pixel, it analyzes how much that point moved along the x and y axes. Based on these parameters, the program tries to recognize the movement. For example, if the laser dot's movement along the x-axis is much more than its movement along the y-axis, the program will determine that it was more or less a horizontal movement. Then, based on the initial and final position of the laser dot, it will determine if the movement was towards the left or towards the right. It uses a similar technique to detect upward, downward, and diagonal movements.

## Controlling Windows Media Player

For controlling the Windows Media Player, the program simply simulates some of the keyboard shortcuts used by Media Player. This code can skip to the next/previous track, or play, pause, stop a track based on the gesture the program recognizes:

```// Get a handle to an application window.
[DllImport("USER32.DLL")]
private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);

// Activate an application window.
[DllImport("USER32.DLL")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private void ControlMediaPlayer(string gesture)
{
IntPtr mediaPlayerHandle =
FindWindow("WMPlayerApp", "Windows Media Player");

// Verify that WMP is a running process.
if (mediaPlayerHandle == IntPtr.Zero)
{
System.Windows.Forms.MessageBox.Show("WMP is not running.");
return;
}

switch (gesture)
{
case "LEFT":
SetForegroundWindow(mediaPlayerHandle);
SendKeys.SendWait("^b");
break;

case "RIGHT":
SetForegroundWindow(mediaPlayerHandle);
SendKeys.SendWait("^f");
break;

case "UP":
SetForegroundWindow(mediaPlayerHandle);
SendKeys.SendWait("^s");
break;

case "DOWN":
SetForegroundWindow(mediaPlayerHandle);
SendKeys.SendWait("^p");
break;
}
}```

## Using the program

Since the program searches for the brightest pixel in the camera's field of view, the lighting conditions of your room can affect its performance. So, adjust the brightness threshold and lighting conditions so that nothing (except the laser) exceeds the brightness threshold.

In the sample program I've provided with this article, the gestures for controlling Windows Media Player are:

• Up – Stop
• Down – Play/Pause
• Left – Previous Track
• Right – Next Track

You can also easily modify the code and use diagonal gestures for volume control.

## Conclusion

We have reached the end of this article. I might update this program to perform more complex gesture recognition later on. However, for now, have fun with it! You can find some videos of this application being used to control Media Player on my blog[^]. You can also easily modify the code to make this program do much more than just controlling Windows Media Player. Have fun!

## History

• [13-JAN-2007] - Minor corrections
• [09-JAN-2007] - Initial publication

A list of licenses authors might use can be found here

## Share

 First PrevNext
 To recognize script aabha10 7-Jan-13 22:36
 My vote of 4 alifshaikh 25-Nov-11 6:15
 Diagonal arrows Danialazri 22-Nov-11 21:36
 Re: Diagonal arrows daltonelvis 5-Feb-12 22:31
 report anups mon 16-Nov-11 8:23
 project pavan acharya 25-Apr-11 6:34
 problem in FPS Sameer Bhide 3-Jan-11 17:02
 project rashmi15091988 30-Apr-10 5:06
 Problem.. niels van putte 13-Feb-10 5:59
 A Suggestion, If I may... Minkie 7-May-09 20:25
 oH! josmoi 29-Apr-09 20:54
 Where did you these values for the Luminance formula? dudubravo 21-Nov-08 8:24
 Super Program! mikkojay 8-Mar-08 8:41
 Re: Super Program! Ashish Derhgawen 17-Mar-08 18:30
 Hrm dubbele onzin 6-Nov-07 3:46
 Good idea, shoddy non-portable implementation. It's a bit sad that most of the article is dedicated to interaction with WMP instead of the actual motion detection.
 WMP is not running Abu Syed Khan 25-May-07 5:55
 Re: WMP is not running Ashish Derhgawen 10-Jun-07 19:15
 Translate to VB? Corbet Peters 20-May-07 17:20
 report shirish_agarwal_85 14-May-07 18:42
 Great Work Giorgi Dalakishvili 3-May-07 2:45
 Re: Great Work Ashish Derhgawen 3-May-07 11:27
 output XY Carl-Johnson456 31-Mar-07 7:34
 Re: output XY Ashish Derhgawen 1-Apr-07 23:00
 hi sarithak548 12-Mar-07 0:53
 Great Job! pab564546 10-Mar-07 7:38
 Re: Great Job! Ashish Derhgawen 11-Mar-07 20:16
 Awesome Niiiissssshhhhhuuuuu 4-Mar-07 23:27
 Awesome!!!! Niiiissssshhhhhuuuuu 4-Mar-07 23:26
 Well done Sacha Barber 1-Mar-07 0:53
 Re: Well done Ashish Derhgawen 1-Mar-07 3:42
 Very neat. Captain See Sharp 16-Feb-07 16:10
 Re: Very neat. Ashish Derhgawen 16-Feb-07 18:01
 Monthly Competition Sacha Barber 13-Feb-07 0:16
 Re: Monthly Competition Ashish Derhgawen 14-Feb-07 8:05
 Re: Monthly Competition Dr.Luiji 16-Feb-07 9:55
 Thanks for sharing Polymorpher 17-Jan-07 15:17
 Re: Thanks for sharing Ashish Derhgawen 17-Jan-07 19:14
 Would be interesting... Fabio Zanetta 14-Jan-07 22:25
 Excelente proyecto Member #591859 12-Jan-07 14:37
 Re: Excelente proyecto Ashish Derhgawen 12-Jan-07 21:57
 Neural Network wheeler989 12-Jan-07 6:06
 Re: Neural Network LanUx 27-Feb-07 6:14
 This is cool Sacha Barber 12-Jan-07 5:07
 Re: This is cool Ashish Derhgawen 12-Jan-07 7:27
 Re: This is cool Sacha Barber 30-Jan-07 23:18
 Re: This is cool Ashish Derhgawen 31-Jan-07 19:04
 Re: This is cool Sacha Barber 13-Feb-07 2:18
 Wa-hey Ed.Poore 11-Jan-07 9:06
 Re: Wa-hey Ashish Derhgawen 12-Jan-07 7:24
 Interesting Concept. Stuart Konen 10-Jan-07 14:08
 Last Visit: 31-Dec-99 19:00     Last Update: 18-Apr-15 15:52 Refresh 12 Next »