public static string Classify(bool useRubine, float duration, bool righthandedness, List<float> SpeakerAngles, PointCollection pointHist, StylusPointCollection S, List<List<int>> hist, List<List<int>> ihist) { // Convert all parameters to format used in GestureTests List<Vector2> InterpretedPoints = new List<Vector2>(); List<Vector2> StylusPoints = new List<Vector2>(); List<Vector2> VelocityHistory = new List<Vector2>(); List<Vector2> InverseVelocityHistory = new List<Vector2>(); foreach(Point P in pointHist) InterpretedPoints.Add(new Vector2((float)P.X,(float)P.Y)); foreach(StylusPoint P in S) StylusPoints.Add(new Vector2((float)P.X,(float)P.Y)); for (int i = 0; i < hist[0].Count; i++) { VelocityHistory.Add(new Vector2(hist[0][i], hist[1][i])); InverseVelocityHistory.Add(new Vector2(ihist[0][i], ihist[1][i])); } // Create a new Sample, compute the features, and classify GS = new GestureSample(GestureTests.Types.GestureType.unknown, righthandedness,duration,SpeakerAngles,InterpretedPoints,StylusPoints,VelocityHistory,InverseVelocityHistory); GS.ComputeFeatures(GestureFeatures.PointsStroke); if (useRubine) return EC.Recognizer.Classify(GS).ToString(); WriteARFF(); Instances test = new Instances(new java.io.FileReader("outfile.arff")); test.setClassIndex(0); double clsLabel = cls.classifyInstance(test.instance(0)); test.instance(0).setClassValue(clsLabel); // Return the appropriate label return ((GestureType2D)((int)clsLabel+1)).ToString(); }
/// <summary> /// Attempts to classify an unknown gesture sample. /// </summary> /// <param name="sample">the gesture sample to be classified</param> /// <returns>the best guess for the given sample</returns> public GestureType Classify(GestureSample sample) { float classificationScore = float.NegativeInfinity; GestureType classification = GestureType.unknown; //Compute the linear weighted function for each gesture class with the feature vector as input //the class whose function yields the maximum value is the classification foreach (GestureType gestureClass in Weights.Keys) { float classScore = ComputeScoreFor(gestureClass, sample.Features); //Console.WriteLine(gestureClass.ToString() + " " + classScore); if (classScore > classificationScore) { classificationScore = classScore; classification = gestureClass; } } return classification; }
/// <summary> /// Parses a given file and loads the gesture data from it. /// </summary> /// <param name="filename">path of file containing an instance of a gesture</param> /// <returns>gesture sample loaded from the file. Returns null if an I/O error occurs.</returns> private GestureSample LoadSample(string filename) { if (Config.Use3DMode == true) return Load3DSample(filename); GestureSample sample = null; GestureType gesture = GestureType.unknown; float duration = float.NaN; bool rightHanded = false; List<Vector2> interpretedPoints = new List<Vector2>(); List<Vector2> velocities = new List<Vector2>(); List<Vector2> inverseVelocities = new List<Vector2>(); List<Vector2> strokePoints = new List<Vector2>(); List<float> angles = new List<float>(); try { StreamReader reader = File.OpenText(filename); while (!reader.EndOfStream) { string line = reader.ReadLine(); //end of file if (line == null) continue; //skip lines starting with '#' are comments if (line.StartsWith("#")) continue; //ignore empty lines if (line == "") continue; //split the line by the 'space' character string[] tokens = line.Split(" ".ToCharArray()); //the first token provides information about the type of data to follow switch (tokens[0]) { case "GestureName:": gesture = ReadGestureType(tokens[1]); break; case "Duration(ms):": duration = float.Parse(tokens[1]); break; case "Handedness:": rightHanded = (tokens[1] == "right"); break; case "SpeakerAngles:": int numAngles = int.Parse(tokens[1]); for (int i = 0; i < numAngles; ++i) { string angle = reader.ReadLine(); string[] theta = angle.Split(" ,".ToCharArray()); angles.Add(float.Parse(theta[0])); } break; case "InterpretedPoints:": int numPoints = int.Parse(tokens[1]); //read the points from succeeding lines. for (int i = 0; i < numPoints; ++i) { string point = reader.ReadLine(); string[] xy = point.Split(" ,".ToCharArray()); interpretedPoints.Add(new Vector2(float.Parse(xy[0]), float.Parse(xy[1]))); } break; case "StrokePoints:": int numStrokePoints = int.Parse(tokens[1]); //read datapoints from succeeding lines. for (int i = 0; i < numStrokePoints; ++i) { string point = reader.ReadLine(); string[] xy = point.Split(" ,".ToCharArray()); //180f, 134.34f strokePoints.Add(new Vector2(float.Parse(xy[0]), float.Parse(xy[1]))); } break; case "Velocities:": int numVelocities = int.Parse(tokens[1]); //read datapoints from succeeding lines. for (int i = 0; i < numVelocities; ++i) { string point = reader.ReadLine(); string[] xy = point.Split(" ,".ToCharArray()); velocities.Add(new Vector2(float.Parse(xy[0]), float.Parse(xy[1]))); } break; case "InverseVelocities:": int numInverseVelocities = int.Parse(tokens[1]); //read datapoints from succeeding lines. for (int i = 0; i < numInverseVelocities; ++i) { string point = reader.ReadLine(); string[] xy = point.Split(" ,".ToCharArray()); inverseVelocities.Add(new Vector2(float.Parse(xy[0]), float.Parse(xy[1]))); } break; } } if (strokePoints.Count == 0) strokePoints = GenerateStroke(interpretedPoints); reader.Close(); sample = new GestureSample(gesture, rightHanded, duration, angles, interpretedPoints, strokePoints, velocities, inverseVelocities); sample.ComputeFeatures(Config.FeaturesToUse); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } return sample; }