Click here to Skip to main content
15,887,214 members
Articles / Multimedia / GDI+

C# Application to Create and Recognize Mouse Gestures (.NET)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (39 votes)
17 Mar 2008CPOL5 min read 221.5K   8.1K   144  
This program can create and recognize mouse gestures.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using NeuralNetworks;
using NeuralNetworks.Utilities;
using NeuralNetworks.Training;
using NeuralNetworks.ActivationFunctions;
using MouseGestures;

namespace Test
{
	class Program
	{
		//Gesture test!
		static void Main(string[] args)
		{
			Gesture g1 = new Gesture(@"D:\Progs\GestureRecognizer\GestureRecognizer\bin\Release\Gestures\L\L_G.xml");
			Gesture g2 = new Gesture(@"D:\Progs\GestureRecognizer\GestureRecognizer\bin\Release\Gestures\ReverseL\ReverseL_G.xml");
			//TrainingSet ts1 = new TrainingSet(@"D:\Progs\GestureRecognizer\GestureRecognizer\bin\Debug\Gestures\ArrowDown\ArrowDown_TS.xml");
			//TrainingSet ts2 = new TrainingSet(@"D:\Progs\GestureRecognizer\GestureRecognizer\bin\Debug\Gestures\ArrowLeft\ArrowLeft_TS.xml");
			GestureSet ts1 = new GestureSet(g1, 1000);
			GestureSet ts2 = new GestureSet(g2, 100);

			NeuralNetwork net = new NeuralNetwork(
				15,
				new int[2] { 5, 1 },
				new double[2][]
		                {
		                    new double[5],
		                    new double[1]
		                },
				new IActivationFunction[2] { new SigmoidFunction(), new SigmoidFunction() });

			BackPropagationLearning learning = new BackPropagationLearning(net, 0.5, new double[2] { 0.5, 0.2 }, 0.0001);

			int cycles;
			double[][] data1 = new double[ts1.Gestures][];
			double[][] output1 = NeuralNetworks.Utilities.Arrays.CreateByValue(ts1.Gestures, 1, 1);
			for (int i = 0; i < ts1.Gestures; i++)
				data1[i] = ts1[i].ExtractFeatures(net.Inputs);

			double[][] data2 = new double[ts2.Gestures][];
			double[][] output2 = NeuralNetworks.Utilities.Arrays.CreateByValue(ts2.Gestures, 0, 1);
			for (int i = 0; i < ts2.Gestures; i++)
				data2[i] = ts2[i].ExtractFeatures(net.Inputs);


			double[][] data = NeuralNetworks.Utilities.Arrays.Merge(data1, data2);
			double[][] output = NeuralNetworks.Utilities.Arrays.Merge(output1, output2);

			double error = learning.Train(data, output, 1000, out cycles);
			Console.WriteLine("Cycles: " + cycles.ToString());
			Console.WriteLine("Error: " + error.ToString());

			double[] input2 = g2.ExtractFeatures(net.Inputs);
			double[] input1 = g1.ExtractFeatures(net.Inputs);

			double[] final_output = net.Compute(input1);
			Console.WriteLine("Output OK: " + Output.Format(final_output));
			//Console.WriteLine("Distance OK: " + MouseGestures.Utilities.Geometry.ComputeDistance(input1, output).ToString());

			final_output = net.Compute(input2);
			Console.WriteLine("Output NOK: " + Output.Format(final_output));
			//Console.WriteLine("Distance NOK: " + MouseGestures.Utilities.Geometry.ComputeDistance(input2, output).ToString());

			Console.ReadLine();
		}

		////Neural network test!
		//static void Main(string[] args)
		//{
		//    //double[] input = new double[] { 0.873, 0, 0, 0 };
		//    //double[] not_input = new double[] { 0.2, 0, 0, 0 };

		//    double[][] inputs = new double[][]{
		//        new double[]{ 0, 0},
		//        new double[]{ 0, 1},
		//        new double[]{ 1, 0},
		//        new double[]{ 1, 1}
		//    };

		//    double[][] outputs = new double[][]{
		//        new double[]{ 0 },
		//        new double[]{ 1 },
		//        new double[]{ 1 },
		//        new double[]{ 0 }
		//    };

		//    LinearFunction f0 = new LinearFunction();
		//    BipolarSigmoidFunction f1 = new BipolarSigmoidFunction();
		//    SigmoidFunction f2 = new SigmoidFunction();
		//    HyperbolicTangentFunction f3 = new HyperbolicTangentFunction();

		//    NeuralNetwork net = new NeuralNetwork(
		//        2,
		//        new int[] { 2, 3, 2 },
		//        new double[][] { 
		//            new double[2],
		//            new double[3],
		//            new double[2]
		//        },
		//        new IActivationFunction[] { f1, f3, f3 });

		//    //Console.WriteLine("Original network:\n" + net.ToString() + "\n");

		//    //BackPropagationLearning back_learning = new BackPropagationLearning(net);
		//    BackPropagationLearning back_learning = new BackPropagationLearning(
		//        net,
		//        0,
		//        new double[] { 0, 0.9, 0.2 },
		//        0.0001);

		//    //double[] user_input = new double[4];
		//    int cycles;
		//    do
		//    {
		//        //back_learning.Train(inputs, outputs, 10000);
		//        back_learning.Train(inputs, inputs, 100000, out cycles);

		//        Console.WriteLine("Modified network:\n" + net.ToString() + "\n");
		//        Console.WriteLine("Computed cycles: " + cycles.ToString());

		//        Console.WriteLine("Input: " + Output.Format(inputs[0]) + "\nOutput: " + Output.Format(net.Compute(inputs[0])));

		//        Console.WriteLine("Input: " + Output.Format(inputs[1]) + "\nOutput: " + Output.Format(net.Compute(inputs[1])));

		//        Console.WriteLine("Input: " + Output.Format(inputs[2]) + "\nOutput: " + Output.Format(net.Compute(inputs[2])));

		//        Console.WriteLine("Input: " + Output.Format(inputs[3]) + "\nOutput: " + Output.Format(net.Compute(inputs[3])));

		//        //Console.WriteLine("Input: " + Output.Format(input) + "\nOutput: " + Output.Format(net.Compute(input)));

		//        //Console.WriteLine("Input: " + Output.Format(not_input) + "\nOutput: " + Output.Format(net.Compute(not_input)));

		//        //try
		//        //{
		//        //    Console.Write("Insert input 1: ");
		//        //    user_input[0] = double.Parse(Console.ReadLine());
		//        //    Console.Write("Insert input 2: ");
		//        //    user_input[1] = double.Parse(Console.ReadLine());
		//        //}
		//        //catch
		//        //{
		//        //    user_input[0] = 0;
		//        //    user_input[1] = 1;
		//        //}
		//        //Console.WriteLine("Input: " + Output.Format(user_input) + "\nOutput: " + Output.Format(net.Compute(user_input)));

		//        Console.Write("\n\nPress 'e' and ENTER to exit... ");
		//    }
		//    while (Console.ReadLine().ToLower() != "e");
		//    //net[0][0].Save("c:/neuron.xml");
		//    //Console.WriteLine("Original neuron:\n" + net[0][0].ToString() + "\n");
		//    //Neuron n = new Neuron("c:/neuron.xml");
		//    //Console.WriteLine("Loaded neuron:\n" + n.ToString() + "\n");

		//    //net[0].Save("c:/layer.xml");
		//    //Console.WriteLine("Original layer:\n" + net[0].ToString() + "\n");
		//    //Layer l = new Layer("c:/layer.xml");
		//    //Console.WriteLine("Loaded layer:\n" + l.ToString() + "\n");

		//    //net.Save("c:/net.xml");
		//    //Console.WriteLine("\nOriginal net:\n" + net.ToString() + "\n");

		//    //NeuralNetwork net2 = new NeuralNetwork("c:/net.xml");
		//    //Console.WriteLine("Loaded net:\n" + net2.ToString());
		//    //Console.WriteLine("\n\n\n\n- Press ENTER to exit -");
		//    //Console.ReadLine();
		//}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Apex s.r.l.
Italy Italy
I got my Computer Science (Engineering) Master's Degree at the Siena University (Italy), but I'm from Rieti (a small town next to Rome).
My hobbies are RPG, MMORGP, programming and 3D graphics.
At the moment I'm employed at Apex s.r.l. (Modena, Italy) as a senior software developer, working for a WPF/WCF project in Rome.

Comments and Discussions