Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Laser Gesture Recognition

Rate me:
Please Sign up or sign in to vote.
4.93/5 (84 votes)
9 Jan 20073 min read 320.7K   7.2K   177   85
A quick article on setting up a simple, real-time laser gesture recognition application and using it to control Windows Media Player.

Sample Image - lasergesture.jpg

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

Image 2

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.

Image 3

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:

C#
// 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India

Comments and Discussions

 
GeneralRe: Interesting Concept. Pin
Ashish Derhgawen10-Jan-07 15:32
Ashish Derhgawen10-Jan-07 15:32 
GeneralExcellent Pin
--=A J E E S H=--9-Jan-07 19:35
--=A J E E S H=--9-Jan-07 19:35 
GeneralRe: Excellent Pin
Ashish Derhgawen10-Jan-07 2:08
Ashish Derhgawen10-Jan-07 2:08 
GeneralWow again! Pin
Eytukan9-Jan-07 17:08
Eytukan9-Jan-07 17:08 
GeneralRe: Wow again! Pin
Ashish Derhgawen10-Jan-07 2:07
Ashish Derhgawen10-Jan-07 2:07 
GeneralThink patent... Pin
owen6543219-Jan-07 16:28
owen6543219-Jan-07 16:28 
GeneralRe: Think patent... Pin
Ashish Derhgawen10-Jan-07 15:50
Ashish Derhgawen10-Jan-07 15:50 
GeneralRe: Think patent... Pin
Ruben Jönsson19-Jan-07 0:20
Ruben Jönsson19-Jan-07 0:20 
GeneralVery Impressive Pin
Mark J. Miller9-Jan-07 8:39
Mark J. Miller9-Jan-07 8:39 
GeneralRe: Very Impressive Pin
Ashish Derhgawen9-Jan-07 13:54
Ashish Derhgawen9-Jan-07 13:54 
GeneralSomeone should take this sample to the next level and integrate a Neural Net Back Propagation routine to recognize more complex and robust patterns Pin
kroach9-Jan-07 5:19
kroach9-Jan-07 5:19 
GeneralRe: Someone should take this sample to the next level and integrate a Neural Net Back Propagation routine to recognize more complex and robust patterns Pin
Josh Smith9-Jan-07 5:38
Josh Smith9-Jan-07 5:38 
GeneralRe: Someone should take this sample to the next level and integrate a Neural Net Back Propagation routine to recognize more complex and robust patterns Pin
kroach9-Jan-07 5:44
kroach9-Jan-07 5:44 
GeneralRe: Someone should take this sample to the next level and integrate a Neural Net Back Propagation routine to recognize more complex and robust patterns Pin
kroach9-Jan-07 5:46
kroach9-Jan-07 5:46 
GeneralRe: Someone should take this sample to the next level and integrate a Neural Net Back Propagation routine to recognize more complex and robust patterns Pin
Martin Raynsford17-Jan-07 2:34
Martin Raynsford17-Jan-07 2:34 
GeneralVery Nice Work Pin
#1239-Jan-07 4:42
#1239-Jan-07 4:42 
GeneralRe: Very Nice Work Pin
NormDroid9-Jan-07 4:49
professionalNormDroid9-Jan-07 4:49 
GeneralRe: Very Nice Work Pin
#1239-Jan-07 6:08
#1239-Jan-07 6:08 
GeneralRe: Very Nice Work Pin
NormDroid9-Jan-07 8:11
professionalNormDroid9-Jan-07 8:11 
GeneralRe: Very Nice Work Pin
toxcct9-Jan-07 7:42
toxcct9-Jan-07 7:42 
QuestionRe: Very Nice Work Pin
NormDroid9-Jan-07 8:10
professionalNormDroid9-Jan-07 8:10 
AnswerRe: Very Nice Work Pin
toxcct9-Jan-07 8:14
toxcct9-Jan-07 8:14 
GeneralRe: Very Nice Work Pin
NormDroid9-Jan-07 9:46
professionalNormDroid9-Jan-07 9:46 
JokeRe: Very Nice Work Pin
toxcct9-Jan-07 9:57
toxcct9-Jan-07 9:57 
GeneralRe: Very Nice Work Pin
J4amieC9-Jan-07 22:17
J4amieC9-Jan-07 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.