Esempio n. 1
0
        public virtual void AddTemplate(GestureTemplate gestureTemplate)
        {
            #region standard loading procedure
            if (gestureTemplate.ClassName == null)
            {
                //gestureTemplate.ClassName = templates.Count.ToString();
                throw new ArgumentException("gesture template doesn't contain class name");
            }
            if (!templates.ContainsKey(gestureTemplate.ClassName))
            {
                GestureClass gestureClass = new GestureClass();
                gestureClass.ClassName = gestureTemplate.ClassName;
                // gestureTemplate.TemplateNumber = 0;
                gestureClass.Add(gestureTemplate);
                templates.Add(gestureClass.ClassName, gestureClass);
            }
            else
            {
                //TODO: this avoids adding of templates under same class name as templates
                //that differ in token count -> remove in final version or parameterize
//                templates[gestureTemplate.ClassName][0].Count
                //&& templates[gestureTemplate.ClassName].Count<11
                //if (3 == gestureTemplate.Count )
                {
                    //   gestureTemplate.TemplateNumber = templates[gestureTemplate.ClassName].Count;
                    templates[gestureTemplate.ClassName].Add(gestureTemplate);
                }
                //else
                //{
                //    Console.WriteLine("Template contained wrong number of tokens");
                //    System.Diagnostics.Process.GetCurrentProcess().Kill();
                //}
            }
            #endregion
        }
    public double distanceAtBestAngle(List <Point2D> points, GestureTemplate aTemplate)
    {
        double startRange = -angleRange;
        double endRange   = angleRange;
        //Debug.LogWarning("anglerange:  "+angleRange);
        double x1 = goldenRatio * startRange + (1.0f - goldenRatio) * endRange;
        double f1 = distanceAtAngle(points, aTemplate, x1);
        double x2 = (1.0f - goldenRatio) * startRange + goldenRatio * endRange;
        double f2 = distanceAtAngle(points, aTemplate, x2);

        while (Mathf.Abs((float)(endRange - startRange)) > anglePrecision)
        {
            if (f1 < f2)
            {
                endRange = x2;
                x2       = x1;
                f2       = f1;
                x1       = goldenRatio * startRange + (1.0f - goldenRatio) * endRange;
                f1       = distanceAtAngle(points, aTemplate, x1);
            }
            else
            {
                startRange = x1;
                x1         = x2;
                f1         = f2;
                x2         = (1.0f - goldenRatio) * startRange + goldenRatio * endRange;
                f2         = distanceAtAngle(points, aTemplate, x2);
            }
        }
        return(Mathf.Min((float)f1, (float)f2));
    }
Esempio n. 3
0
        public virtual GestureTemplate GetGestureFromXmlTemplate(String xml)
        {
            XmlSerializer   xmlSer  = new XmlSerializer(typeof(GestureTemplate));
            StringReader    sr      = new StringReader(xml);
            GestureTemplate gesture = xmlSer.Deserialize(sr) as GestureTemplate;

            return(gesture);
        }
Esempio n. 4
0
 public virtual void AddTemplate(GestureTemplate template)
 {
     foreach (Object o in classifiers)
     {
         if (o is ILoadGestureTemplates)
         {
             (o as ILoadGestureTemplates).AddTemplate(template);
         }
     }
 }
Esempio n. 5
0
        public virtual String GetXmlTemplateFromGesture(GestureTemplate gesture)
        {
            XmlDocument   doc    = new XmlDocument();
            StringWriter  sw     = new StringWriter();
            XmlSerializer xmlSer = new XmlSerializer(typeof(GestureTemplate));

            xmlSer.Serialize(sw, gesture);
            System.Text.StringBuilder sb = sw.GetStringBuilder();

            return(sb.ToString());
        }
Esempio n. 6
0
        public virtual void StoreTemplate(string templatePath, GestureTemplate template)
        {
            int    i        = 0;
            String fileName = template.ClassName + "_" + i.ToString() + ".xml";
            String file     = templatePath + System.IO.Path.DirectorySeparatorChar + fileName;

            while (System.IO.File.Exists(file))
            {
                i++;
                fileName = template.ClassName + "_" + i.ToString() + ".xml";
                file     = templatePath + System.IO.Path.DirectorySeparatorChar + fileName;
            }
            StoreTemplate(templatePath, fileName, template);
        }
Esempio n. 7
0
 public void RecognizerFinished(GestureRecognizerResponse response)
 {
     LastNormalizedGesture = response.OptimizedPoints;
     if (response.IsMatch)
     {
         LastMatchedGesture = response.MatchedTemplate;
         SetDebugText(response.MatchedTemplate.Name);
         Debug.Log("Matched with " + response.MatchedTemplate.Name + ". Score: " + response.Score);
     }
     else
     {
         LastMatchedGesture = null;
         SetDebugText("No match");
         Debug.Log("No match");
     }
 }
    public void Save()
    {
        template = new GestureTemplate()
        {
            Points = points,
            Name   = input.text
        };

        allTemplates.Add(template);

        foreach (GestureTemplate g in allTemplates)
        {
            g.BeforeSerializing();
        }

        gestureRecognizer.RecordTemplate(points);
        templatesFileHandler.Save(allTemplates);
    }
    public int addTemplate(string name, List<Point2D> points)
    {
        points = normalizePath(points);

        GestureTemplate tmp = new GestureTemplate(name, points);

        templates.Add(tmp);

        //--- Let them know how many examples of this template we have now
        int numInstancesOfGesture = 0;
        // You know, i don't care so i'm just going to ignore this
        //for (var i = 0; i < templates.size(); i++)
        //{
        //	if (templates[i].Name == name)
        //		numInstancesOfGesture++;
        //}
        return numInstancesOfGesture;
    }
Esempio n. 10
0
        public virtual IClassificationResult RecognizeTemplate(GestureTemplate template)
        {
            var tokenDict = new Dictionary <int, IList <Gestures.Geometrie.Vertex.Sample> >();

            foreach (var t in template.Tokens)
            {
                tokenDict.Add(t.Id, t.Samples);
            }

            foreach (IClassify o in classifiers)
            {
                var r = o.Classify(null, tokenDict);
                if (r != null)
                {
                    return(r);
                }
            }
            return(null);
        }
    public int addTemplate(string name, List <Point2D> points)
    {
        points = normalizePath(points);

        GestureTemplate tmp = new GestureTemplate(name, points);

        templates.Add(tmp);

        //--- Let them know how many examples of this template we have now
        int numInstancesOfGesture = 0;

        // You know, i don't care so i'm just going to ignore this
        //for (var i = 0; i < templates.size(); i++)
        //{
        //	if (templates[i].Name == name)
        //		numInstancesOfGesture++;
        //}
        return(numInstancesOfGesture);
    }
        float CalculateTemplateDistance(IList <GestureStroke> strokes, GestureTemplate templateToMatch)
        {
            var distance = 0.0f;

            for (var i = 0; i < strokes.Count; i++)
            {
                var stroke        = strokes[i];
                var strokeToMatch = templateToMatch.Strokes[i];

                if (strokeToMatch.IsDot != stroke.IsDot)
                {
                    return(Mathf.Infinity);
                }

                if (strokeToMatch.IsDot && stroke.IsDot)
                {
                    distance += Vector2.Distance(strokeToMatch.DotPosition, stroke.DotPosition) * dotDistancePenalty;
                }
                else
                {
                    if (stroke.Points.Count != strokeToMatch.Points.Count)
                    {
                        Debug.LogError("Difference in list length when calculating distance is not supported");
                        return(Mathf.Infinity);
                    }
                    var tempdistance = 0.0f;
                    for (var j = 0; j < stroke.Points.Count; j++)
                    {
                        var strokePoint     = stroke.Points[j];
                        var pointToMatch    = strokeToMatch.Points[j];
                        var penaltyModifier = templateToMatch.PenaltyModifier(strokePoint, pointToMatch);
                        tempdistance += Vector2.Distance(strokePoint, pointToMatch) * penaltyModifier;
                    }

                    distance += (tempdistance / stroke.Points.Count);
                }
            }

            return(distance);
        }
Esempio n. 13
0
        public virtual string FinishTemplateRecording(String xmlTemplatePath, String gestureClassName)
        {
            string fileName = String.Empty;

            try
            {
                if (RecognitionMode)
                {
                    return(fileName);
                }
                RecordingMode = false;
                Gesture         gesture = blobTracker.TrackedBlobs;
                GestureTemplate gestureTemplate
                    = new GestureTemplate(gestureClassName, gesture);

                foreach (Object o in classifiers)
                {
                    if (o is ILoadGestureTemplates)
                    {
                        ILoadGestureTemplates tl = (o as ILoadGestureTemplates);
                        tl.AddTemplate(gestureTemplate);
                        var classes = tl.GetGestureClasses();
                        int index   = 0;
                        for (index = 0; index < classes.Length; index++)
                        {
                            if (classes[index].ClassName == gestureTemplate.ClassName)
                            {
                                break;
                            }
                        }
                        fileName = gestureTemplate.ClassName + "_" + classes[index].Count.ToString() + ".xml";
                        tl.StoreTemplate(xmlTemplatePath, fileName, gestureTemplate);
                    }
                }
            }
            catch
            {
            }
            return(fileName);
        }
Esempio n. 14
0
    private const int NUMBER_OF_POINTS    = 32; //The number of points to resample to.

    #region IGestureRecognizer methods

    public GestureTemplate Recognize(Gesture g, List <GestureTemplate> candidates, out float score)
    {
        var normalizedGesture = Normalize(g);

        var best_distance = 0f;
        var best_gesture  = new GestureTemplate("No Gesture");

        score = 0f;

        for (var i = 0; i < candidates.Count; i++)
        {
            var examples = candidates[i].Examples;

            for (var j = 0; j < examples.Count; j++)
            {
                var distance = DistanceAtBestAngle(normalizedGesture, examples[j], -SECTION_SIZE, SECTION_SIZE, SEARCH_DELTA);

                if (i == 0 && j == 0)
                {
                    best_distance = distance;
                    best_gesture  = candidates[i];
                    continue;
                }

                if (!(distance < best_distance))
                {
                    continue;
                }

                best_distance = distance;
                best_gesture  = candidates[i];
                score         = 1 - (best_distance / (0.5f * Mathf.Sqrt(Mathf.Pow(2f * BOUNDING_BOX_SIZE, 2))));
            }
        }

        return(best_gesture);
    }
 public double distanceAtAngle(List<Point2D> points, GestureTemplate aTemplate, double rotation)
 {
     List<Point2D> newPoints = rotateBy(points, rotation);
     return pathDistance(newPoints, aTemplate.points);
 }
Esempio n. 16
0
 public virtual void StoreTemplate(string templatePath, GestureTemplate template)
 {
     trw.StoreTemplate(templatePath, template);
 }
Esempio n. 17
0
        public virtual void LoadTemplates(string xmlTemplatePath)
        {
            templates.Clear();
            if (xmlTemplatePath == null || xmlTemplatePath == String.Empty)
            {
                xmlTemplatePath = StandardPaths.standardXmlTemplatePath;
            }
            if (System.IO.Directory.Exists(xmlTemplatePath))
            {
                var fileList = new List <String>();
                foreach (String file in System.IO.Directory.GetFiles(xmlTemplatePath, "*.xml",
                                                                     System.IO.SearchOption.AllDirectories))
                {
                    fileList.Add(file);
                }
                fileList.Sort(new Comparison <string>(delegate(string str1, string str2)
                {
                    if (str1 == null && str2 == null)
                    {
                        return(0);
                    }
                    else if (str1 == null)
                    {
                        return(-1);
                    }
                    else if (str2 == null)
                    {
                        return(1);
                    }
                    return(System.IO.File.GetCreationTime(str1) < System.IO.File.GetCreationTime(str2) ? -1 : 1);
                }));

                foreach (String file in fileList)
                {
                    GestureTemplate gestureTemplate =
                        templateReaderWriter.GetGestureFromXmlTemplate(
                            templateReaderWriter.LoadXmlTemplate(file));

                    if (gestureTemplate != null)
                    {
                        var  name        = Path.GetFileNameWithoutExtension(file);
                        var  num         = name.Substring(name.LastIndexOf("_"));
                        int  templateNum = 0;
                        bool store       = false;

                        if (String.IsNullOrWhiteSpace(gestureTemplate.ClassName))
                        {
                            var cname = name.Substring(0, name.LastIndexOf("_"));
                            gestureTemplate.ClassName = cname;
                            store = true;
                        }
                        if (!String.IsNullOrWhiteSpace(num))
                        {
                            bool succ = Int32.TryParse(num, out templateNum);
                            if (succ && templateNum != gestureTemplate.TemplateNumber)
                            {
                                gestureTemplate.TemplateNumber = templateNum;
                                store = true;
                            }
                        }
                        if (store)
                        {
                            try { this.StoreTemplate(Path.GetDirectoryName(file), Path.GetFileName(file), gestureTemplate); }
                            catch { }
                        }

                        AddTemplate(gestureTemplate);
                    }
                }
            }

            else
            {
                System.IO.Directory.CreateDirectory(xmlTemplatePath);
            }
        }
    public double distanceAtAngle(List <Point2D> points, GestureTemplate aTemplate, double rotation)
    {
        List <Point2D> newPoints = rotateBy(points, rotation);

        return(pathDistance(newPoints, aTemplate.points));
    }
Esempio n. 19
0
 public virtual void StoreTemplate(string templatePath, String templateFileName, GestureTemplate template)
 {
     if (templatePath == null || templatePath == String.Empty)
     {
         templatePath = StandardPaths.standardXmlTemplatePath;
     }
     if (!System.IO.Directory.Exists(templatePath))
     {
         System.IO.Directory.CreateDirectory(templatePath);
     }
     templateReaderWriter.StoreTemplate(
         templateReaderWriter.GetXmlTemplateFromGesture(template),
         templatePath + System.IO.Path.DirectorySeparatorChar + templateFileName);
 }
Esempio n. 20
0
    double distanceAtBestAngle(System.Collections.Generic.List<Point2D> points,
        GestureTemplate aTemplate)
    {
        double startRange = -angleRange;
		double endRange   =  angleRange;
		double x1 = goldenRatio * startRange + (1.0 - goldenRatio) * endRange;
		double f1 = distanceAtAngle(points, aTemplate, x1);
		double x2 = (1.0 - goldenRatio) * startRange + goldenRatio * endRange;
		double f2 = distanceAtAngle(points, aTemplate, x2);
		while (UnityEngine.Mathf.Abs((float)(endRange - startRange)) > anglePrecision)
		{
			if (f1 < f2)
			{
				endRange = x2;
				x2 = x1;
				f2 = f1;
				x1 = goldenRatio * startRange + (1.0 - goldenRatio) * endRange;
				f1 = distanceAtAngle(points, aTemplate, x1);
			}
			else
			{
				startRange = x1;
				x1 = x2;
				f1 = f2;
				x2 = (1.0 - goldenRatio) * startRange + goldenRatio * endRange;
				f2 = distanceAtAngle(points, aTemplate, x2);
			}
		}
		return UnityEngine.Mathf.Min((float)f1, (float)f2);
    }
Esempio n. 21
0
    double distanceAtAngle(System.Collections.Generic.List<Point2D> points,
        GestureTemplate aTemplate,double rotation)
    {
        System.Collections.Generic.List<Point2D> newPoints = rotateBy(points, rotation);
		return pathDistance(newPoints, aTemplate.points);
    }
 public double distanceAtBestAngle(List<Point2D> points, GestureTemplate aTemplate)
 {
     double startRange = -angleRange;
     double endRange   =  angleRange;
     //Debug.LogWarning("anglerange:  "+angleRange);
     double x1 = goldenRatio * startRange + (1.0f - goldenRatio) * endRange;
     double f1 = distanceAtAngle(points, aTemplate, x1);
     double x2 = (1.0f - goldenRatio) * startRange + goldenRatio * endRange;
     double f2 = distanceAtAngle(points, aTemplate, x2);
     while (Mathf.Abs((float)(endRange - startRange)) > anglePrecision)
     {
         if (f1 < f2)
         {
             endRange = x2;
             x2 = x1;
             f2 = f1;
             x1 = goldenRatio * startRange + (1.0f - goldenRatio) * endRange;
             f1 = distanceAtAngle(points, aTemplate, x1);
         }
         else
         {
             startRange = x1;
             x1 = x2;
             f1 = f2;
             x2 = (1.0f - goldenRatio) * startRange + goldenRatio * endRange;
             f2 = distanceAtAngle(points, aTemplate, x2);
         }
     }
     return Mathf.Min((float)f1, (float)f2);
 }