コード例 #1
0
ファイル: WekaHelper.cs プロジェクト: caomw/Multiwave
        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());
        }
コード例 #2
0
        private GestureSample Load3DSample(string filename)
        {
            GestureSample sample = null;

            GestureType    gesture           = GestureType.unknown;
            float          duration          = float.NaN;
            bool           rightHanded       = false;
            List <Vector3> interpretedPoints = new List <Vector3>();
            List <Vector3> velocities        = new List <Vector3>();
            List <Vector3> inverseVelocities = new List <Vector3>();
            List <Vector3> strokePoints      = new List <Vector3>();
            List <float>   angles            = new List <float>();
            List <float>   elevations        = new List <float>();
            List <float[]> raw_data          = 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 "SpeakerElevations:":
                        int numEle = int.Parse(tokens[1]);
                        for (int i = 0; i < numEle; ++i)
                        {
                            string   angle = reader.ReadLine();
                            string[] ele   = angle.Split(" ,".ToCharArray());
                            elevations.Add(float.Parse(ele[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[] xyz   = point.Split(" ,".ToCharArray());
                            interpretedPoints.Add(new Vector3(float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])));
                        }

                        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[] xyz   = point.Split(" ,".ToCharArray());
                            strokePoints.Add(new Vector3(float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])));
                        }
                        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[] xyz   = point.Split(" ,".ToCharArray());
                            velocities.Add(new Vector3(float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])));
                        }
                        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[] xyz   = point.Split(" ,".ToCharArray());
                            inverseVelocities.Add(new Vector3(float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])));
                        }
                        break;

                    case "RawData:":
                        int numRawData = int.Parse(tokens[1]);

                        //read datapoints from succeeding lines.
                        for (int i = 0; i < numRawData; ++i)
                        {
                            string point = reader.ReadLine();
                            raw_data.Add(Array.ConvertAll(point.Split(','), float.Parse));
                        }
                        break;
                    }
                }
                if (strokePoints.Count == 0)
                {
                    strokePoints = Generate3DStroke(interpretedPoints);
                }
                reader.Close();
                sample = new GestureSample(gesture, rightHanded, duration, angles, elevations, interpretedPoints, strokePoints, velocities, inverseVelocities, raw_data);
                sample.ComputeFeatures(Config.FeaturesToUse);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }


            return(sample);
        }