Click here to Skip to main content
11,718,016 members (79,003 online)
Click here to Skip to main content

Laser Gesture Recognition

, 9 Jan 2007 240.2K 6.1K 176
Rate this:
Please Sign up or sign in to vote.
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

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. Smile | :)

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

Share

About the Author


You may also be interested in...

Comments and Discussions

 
QuestionTo recognize script Pin
aabha107-Jan-13 21:36
memberaabha107-Jan-13 21:36 
GeneralMy vote of 4 Pin
alifshaikh25-Nov-11 5:15
memberalifshaikh25-Nov-11 5:15 
QuestionDiagonal arrows Pin
Danialazri22-Nov-11 20:36
memberDanialazri22-Nov-11 20:36 
AnswerRe: Diagonal arrows Pin
daltonelvis5-Feb-12 21:31
memberdaltonelvis5-Feb-12 21:31 
AnswerRe: Diagonal arrows Pin
OmAr MouNir3-May-15 3:23
memberOmAr MouNir3-May-15 3:23 
Generalreport Pin
anups mon16-Nov-11 7:23
memberanups mon16-Nov-11 7:23 
Generalproject Pin
pavan acharya25-Apr-11 5:34
memberpavan acharya25-Apr-11 5:34 
Generalproblem in FPS Pin
Sameer Bhide3-Jan-11 16:02
memberSameer Bhide3-Jan-11 16:02 
Generalproject Pin
rashmi1509198830-Apr-10 4:06
grouprashmi1509198830-Apr-10 4:06 
QuestionProblem.. Pin
niels van putte13-Feb-10 4:59
memberniels van putte13-Feb-10 4:59 
GeneralA Suggestion, If I may... Pin
Minkie7-May-09 19:25
memberMinkie7-May-09 19:25 
GeneraloH! Pin
josmoi29-Apr-09 19:54
memberjosmoi29-Apr-09 19:54 
QuestionWhere did you these values for the Luminance formula? Pin
dudubravo21-Nov-08 7:24
memberdudubravo21-Nov-08 7:24 
GeneralSuper Program! Pin
mikkojay8-Mar-08 7:41
membermikkojay8-Mar-08 7:41 
GeneralRe: Super Program! Pin
Ashish Derhgawen17-Mar-08 17:30
memberAshish Derhgawen17-Mar-08 17:30 
GeneralHrm Pin
dubbele onzin6-Nov-07 2:46
memberdubbele onzin6-Nov-07 2:46 
QuestionWMP is not running Pin
Abu Syed Khan25-May-07 4:55
memberAbu Syed Khan25-May-07 4:55 
AnswerRe: WMP is not running Pin
Ashish Derhgawen10-Jun-07 18:15
memberAshish Derhgawen10-Jun-07 18:15 
QuestionTranslate to VB? Pin
Corbet Peters20-May-07 16:20
memberCorbet Peters20-May-07 16:20 
Generalreport Pin
shirish_agarwal_8514-May-07 17:42
membershirish_agarwal_8514-May-07 17:42 
GeneralGreat Work Pin
Giorgi Dalakishvili3-May-07 1:45
memberGiorgi Dalakishvili3-May-07 1:45 
Thanks for such a cool and great piece of code. I was impressed when I saw this.
GeneralRe: Great Work Pin
Ashish Derhgawen3-May-07 10:27
memberAshish Derhgawen3-May-07 10:27 
Generaloutput XY Pin
Carl-Johnson45631-Mar-07 6:34
memberCarl-Johnson45631-Mar-07 6:34 
GeneralRe: output XY Pin
Ashish Derhgawen1-Apr-07 22:00
memberAshish Derhgawen1-Apr-07 22:00 
Generalhi Pin
sarithak54811-Mar-07 23:53
membersarithak54811-Mar-07 23:53 
GeneralGreat Job! Pin
pab56454610-Mar-07 6:38
memberpab56454610-Mar-07 6:38 
GeneralRe: Great Job! Pin
Ashish Derhgawen11-Mar-07 19:16
memberAshish Derhgawen11-Mar-07 19:16 
GeneralAwesome Pin
Niiiissssshhhhhuuuuu4-Mar-07 22:27
memberNiiiissssshhhhhuuuuu4-Mar-07 22:27 
GeneralAwesome!!!! Pin
Niiiissssshhhhhuuuuu4-Mar-07 22:26
memberNiiiissssshhhhhuuuuu4-Mar-07 22:26 
GeneralWell done Pin
Sacha Barber28-Feb-07 23:53
memberSacha Barber28-Feb-07 23:53 
GeneralRe: Well done Pin
Ashish Derhgawen1-Mar-07 2:42
memberAshish Derhgawen1-Mar-07 2:42 
GeneralVery neat. Pin
Captain See Sharp16-Feb-07 15:10
memberCaptain See Sharp16-Feb-07 15:10 
GeneralRe: Very neat. Pin
Ashish Derhgawen16-Feb-07 17:01
memberAshish Derhgawen16-Feb-07 17:01 
GeneralMonthly Competition Pin
Sacha Barber12-Feb-07 23:16
memberSacha Barber12-Feb-07 23:16 
GeneralRe: Monthly Competition Pin
Ashish Derhgawen14-Feb-07 7:05
memberAshish Derhgawen14-Feb-07 7:05 
GeneralRe: Monthly Competition Pin
Dr.Luiji16-Feb-07 8:55
memberDr.Luiji16-Feb-07 8:55 
GeneralThanks for sharing Pin
Polymorpher17-Jan-07 14:17
memberPolymorpher17-Jan-07 14:17 
GeneralRe: Thanks for sharing Pin
Ashish Derhgawen17-Jan-07 18:14
memberAshish Derhgawen17-Jan-07 18:14 
GeneralWould be interesting... Pin
Fabio Zanetta14-Jan-07 21:25
memberFabio Zanetta14-Jan-07 21:25 
GeneralExcelente proyecto Pin
Member #59185912-Jan-07 13:37
memberMember #59185912-Jan-07 13:37 
GeneralRe: Excelente proyecto Pin
Ashish Derhgawen12-Jan-07 20:57
memberAshish Derhgawen12-Jan-07 20:57 
GeneralNeural Network Pin
wheeler98912-Jan-07 5:06
memberwheeler98912-Jan-07 5:06 
GeneralRe: Neural Network Pin
LanUx27-Feb-07 5:14
memberLanUx27-Feb-07 5:14 
GeneralThis is cool Pin
Sacha Barber12-Jan-07 4:07
memberSacha Barber12-Jan-07 4:07 
GeneralRe: This is cool Pin
Ashish Derhgawen12-Jan-07 6:27
memberAshish Derhgawen12-Jan-07 6:27 
GeneralRe: This is cool Pin
Sacha Barber30-Jan-07 22:18
memberSacha Barber30-Jan-07 22:18 
GeneralRe: This is cool Pin
Ashish Derhgawen31-Jan-07 18:04
memberAshish Derhgawen31-Jan-07 18:04 
GeneralRe: This is cool Pin
Sacha Barber13-Feb-07 1:18
memberSacha Barber13-Feb-07 1:18 
GeneralWa-hey Pin
Ed.Poore11-Jan-07 8:06
memberEd.Poore11-Jan-07 8:06 
GeneralRe: Wa-hey Pin
Ashish Derhgawen12-Jan-07 6:24
memberAshish Derhgawen12-Jan-07 6:24 

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

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

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