![]() |
Multimedia »
DirectX »
General
Intermediate
Laser Gesture RecognitionBy Ashish DerhgawenA quick article on setting up a simple, real-time laser gesture recognition application and using it to control Windows Media Player. |
C# 1.0, C# 2.0.NET 2.0, Win2K, WinXPVS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
||||||||||||||||||

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.
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.

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.

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;
}
}
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:
You can also easily modify the code and use diagonal gestures for volume control. :)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Jan 2007 Editor: Smitha Vijayan |
Copyright 2007 by Ashish Derhgawen Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |