Skip to content
Snippets Groups Projects
Commit 53321cb0 authored by Alexis Iakovenko's avatar Alexis Iakovenko
Browse files

Create basic comparing algorithm

parent bb446435
No related branches found
No related tags found
No related merge requests found
...@@ -2,15 +2,72 @@ ...@@ -2,15 +2,72 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class CompareGestureMean : MonoBehaviour { public class CompareGestureMean : MonoBehaviour
{
List<TemporaryGesture> gestures = new List<TemporaryGesture> ();
TemporaryGesture gestureToCompare;
// Use this for initialization void Start ()
void Start () { {
LoadGestures ();
TemporaryGesture meanGesture = ComputeMeanGesture ();
CompareGestureWithMeanGesture (gestureToCompare, meanGesture);
} }
// Update is called once per frame void LoadGestures ()
void Update () { {
//gesture to compare
gestureToCompare = new TemporaryGesture ("1");
//gestures from data
gestures.Add (new TemporaryGesture ("2"));
gestures.Add (new TemporaryGesture ("3"));
gestures.Add (new TemporaryGesture ("4"));
gestures.Add (new TemporaryGesture ("5"));
}
TemporaryGesture ComputeMeanGesture()
{
//we assume that all gestures have same length
TemporaryGesture meanGesture = new TemporaryGesture ();
for (int i = 0; i < gestures [0].states.Count; i++)
{
float meanX = 0;
float meanY = 0;
float meanZ = 0;
foreach (TemporaryGesture gesture in gestures)
{
meanX += gesture.states [i].x;
meanY += gesture.states [i].y;
meanZ += gesture.states [i].z;
}
meanX /= gestures.Count;
meanY /= gestures.Count;
meanZ /= gestures.Count;
TemporaryGestureState meanState = new TemporaryGestureState ();
meanState.timestamp = gestures [0].states [i].timestamp;
meanState.x = meanX;
meanState.y = meanY;
meanState.z = meanZ;
meanGesture.states.Add (meanState);
}
return meanGesture;
}
void CompareGestureWithMeanGesture(TemporaryGesture gesture, TemporaryGesture meanGesture)
{
float score = 0;
for (int i = 0; i < gesture.states.Count; i++)
{
float differenceX = Mathf.Abs(gesture.states[i].x - meanGesture.states[i].x);
float differenceY = Mathf.Abs(gesture.states[i].y - meanGesture.states[i].y);
float differenceZ = Mathf.Abs(gesture.states[i].z - meanGesture.states[i].z);
score -= differenceX + differenceY + differenceZ;
}
Debug.Log ("Comparing gesture " + gesture.ToString() + " with mean gesture. Score: " + score);
} }
} }
...@@ -3,39 +3,57 @@ using System.Collections.Generic; ...@@ -3,39 +3,57 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System.IO; using System.IO;
public class LoadTemporaryGestures : MonoBehaviour public class TemporaryGesture
{ {
List<TemporaryGestureState> states = new List<TemporaryGestureState>(); static string directoryPath = "Assets/Compare/TemporaryGestures/";
public List<TemporaryGestureState> states;
string name;
void Start() public TemporaryGesture()
{ {
Load ("Assets/Compare/TemporaryGestures/1.txt"); states = new List<TemporaryGestureState> ();
} }
public void Load (string path) public TemporaryGesture(string name)
{
this.name = name;
Load ();
}
public void Load ()
{ {
StreamReader reader = new StreamReader(path); StreamReader reader = new StreamReader(directoryPath + name + ".txt");
string[,] csvStrings = CSVReader.SplitCsvGrid (reader.ReadToEnd ()); string[,] csvStrings = CSVReader.SplitCsvGrid (reader.ReadToEnd ());
Debug.Log("csvStrings length:" + csvStrings.Length); //Debug.Log("csvStrings " + name + " length:" + csvStrings.Length);
for (int i = 0; i < 200; i++) states = new List<TemporaryGestureState>();
for (int i = 0; i < 100; i++)
{ {
if (i < 2) //dont take 2 first lines into account if (i < 2) //dont take 2 first lines into account
continue; continue;
if (csvStrings == null)
break;
//we need timestamp, x, y, z of palm //we need timestamp, x, y, z of palm
string[] line = csvStrings[0,i].Split (' '); string[] line = csvStrings[0,i].Split (' ');
TemporaryGestureState state = new TemporaryGestureState (); TemporaryGestureState state = new TemporaryGestureState ();
state.timestamp = int.Parse(line [2]); state.timestamp = int.Parse(line [2]);
state.x = double.Parse(line [13]); state.x = float.Parse(line [13]);
state.y = double.Parse(line [14]); state.y = float.Parse(line [14]);
state.z = double.Parse(line [15]); state.z = float.Parse(line [15]);
states.Add (state); states.Add (state);
Debug.Log(state); //Debug.Log(state);
} }
reader.Close(); reader.Close();
//Debug.Log("Gesture " + name + " loaded.");
}
public override string ToString ()
{
return name;
} }
} }
fileFormatVersion: 2 fileFormatVersion: 2
guid: ee829df398e2f994bb63d985c6e48c7b guid: ee5ae9629eb5b5f439afcd95ba6e0655
timeCreated: 1511955693 timeCreated: 1511960149
licenseType: Free licenseType: Free
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
......
...@@ -5,9 +5,9 @@ using UnityEngine; ...@@ -5,9 +5,9 @@ using UnityEngine;
public class TemporaryGestureState { public class TemporaryGestureState {
public int timestamp = 0; public int timestamp = 0;
public double x = 0; public float x = 0;
public double y = 0; public float y = 0;
public double z = 0; public float z = 0;
public override string ToString() public override string ToString()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment