Click here to Skip to main content
Click here to Skip to main content

Laser Gesture Recognition

By , 9 Jan 2007
 
Prize winner in Competition "C# Dec 2006"

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

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

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

About the Author

Ashish Derhgawen
India India
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionTo recognize scriptmemberaabha107 Jan '13 - 21:36 
Sir,
I am trying to take this concept of yours further and as a part of it I have make circle recognized by laser gesture recognition and now I need your help to get the script recognized by the same detector. Please help me with the logical part.
Thanking you.
GeneralMy vote of 4memberalifshaikh25 Nov '11 - 5:15 
Good One
QuestionDiagonal arrowsmemberDanialazri22 Nov '11 - 20:36 
Hey, Im Danial a student programmer. I've read that you said the diagonal arrow laser movements can be used. I've actually tried to do it but is finding troubles doing. I can't seem to know what to put to control the media plyer's volume using the diagonal arrows.
 
case "DIAGONAL1A":
SetForegroundWindow(mediaPlayerHandle);
SendKeys.SendWait("^b");
break;
 
I need help to know what to put as under the SendKeys.Sendwait("^b"); because this code is for the left arrow. thank you
AnswerRe: Diagonal arrowsmemberdaltonelvis5 Feb '12 - 21:31 
there add the shortcut key for volume increase for windows media player.go to wmp n check the shortcut key.
 
Regards,
Dalton
Generalreportmemberanups mon16 Nov '11 - 7:23 
Well done man... Smile | :) awesome project...can u kindly mail the project report to this id...ianupmondal@gmail.com.... thanks in advance... Smile | :) Smile | :) Smile | :)
Generalprojectmemberpavan acharya25 Apr '11 - 5:34 
i am doing this project with my friends but without using DirectX. But i may need ur report to refer once. kindly reply to pavan_acharya_b@yahoo.co.in Thanks.
Generalproblem in FPSmemberSameer Bhide3 Jan '11 - 16:02 
hey friend, i am getting frames per second ratio very low (below 1 fps) because of which the program is unable to detect gestures...
can u suggest me the solution.
i m using a TECH-COM webcam.
thanks.
Generalprojectgrouprashmi1509198830 Apr '10 - 4:06 
hey..
can i see your project report once . mail at rashmi.l.kanth@gmail.com if possible.
thanks.. Smile | :)
QuestionProblem..memberniels van putte13 Feb '10 - 4:59 
The program won't open any file or capturing divice.. Does anybody know how to solve this problem? Smile | :)
GeneralA Suggestion, If I may...memberMinkie7 May '09 - 19:25 
I have to say, I tryed this program and I think its great! Now here is a way that this program would become standard in my house... Media Center compatibility. I have Windows 7 and use MCE for T.V. on my partners computer. I could now change the channel and volume and stuff without bugging him. If you could do THAT, I would be in heaven! I am sure some people might find it usefull like me! Thanks for the great work.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 9 Jan 2007
Article Copyright 2007 by Ashish Derhgawen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid