Example #1
0
        public void GetSum(double[] x, double[] y, int sakoeChibaBand, double expected)
        {
            var dtw    = new DTW(x, y, sakoeChibaBand);
            var result = dtw.GetSum();

            Assert.Equal(expected, result);
        }
Example #2
0
        /// <summary>
        /// This function is called to get normalised result before passing to DTW algorithm.
        /// The input from the Kinect and the text files are normalised and passed into DTW.
        /// If the gesture name matches with the input, the name of gesture is received in the parameter finalgesture.
        /// </summary>
        ///  <param name="gesturename">It is used to describe the name of gesture in dataset with which the Body coordinates are going to be compared with</param>
        /// <returns>returns the gesture matched from DTW</returns>
        private string passToDTW(string gesturename)
        {
            try
            {
                Recorder        recorder          = new Recorder();
                ArrayList       gesturefromkinect = recorder.readFiles(KINECT_COORDINATES);// getting normalised co ordinates here.....
                string          storedtextLines   = System.IO.File.ReadAllText(gesturename);
                List <double[]> normalisedresult  = new List <double[]>();

                string[] result = storedtextLines.Split('@');
                for (int i = 0; i < result.Length; i++)
                {
                    normalisedresult = recorder.explode_array(result, ',');
                }
                string txtFiles = Path.GetFileName(gesturename);

                int dimension      = 12;
                int threshold      = 3;
                int firstThreshold = 5;
                int maxSlope       = 2;
                int minimumLength  = 10;
                _dtw = new DTW(dimension, threshold, firstThreshold, maxSlope, minimumLength);
                ArrayList       arrlstTemp = ArrayList.Adapter(normalisedresult);
                Regex           regex      = new Regex(@"_(.+?)_");
                MatchCollection mc2        = regex.Matches(txtFiles);
                _dtw.Add(arrlstTemp, "The gesture is " + mc2[0]);
                finalgesture       = _dtw.recognize(gesturefromkinect);
                KINECT_COORDINATES = new string[] { };
            }
            catch (Exception ex)
            { }
            return(finalgesture);
        }
Example #3
0
        public void GetPath(double[] x, double[] y, int sakoeChibaBand, Tuple <int, int>[] expected)
        {
            var dtw    = new DTW(x, y, sakoeChibaBand);
            var result = dtw.GetPath();

            Assert.Equal(expected, result);
        }
Example #4
0
        public void Refresh(int i, int j, int minI, int minJ)
        {
            List <DTW.Pair> path = DTW.OptimalWarpingPath(totalCostMatrix, minI, minJ);

            int iOffset = i - writeableBitmap.PixelWidth;
            int jOffset = j - writeableBitmap.PixelHeight;

            if (iOffset < 0)
            {
                iOffset = 0;
            }
            if (jOffset < 0)
            {
                jOffset = 0;
            }

            // draw cost matrix
            int color;

            for (int x = 0; x < writeableBitmap.PixelWidth; x++)
            {
                for (int y = 0; y < writeableBitmap.PixelHeight; y++)
                {
                    double val = cellCostMatrix[iOffset + x, jOffset + y];
                    if (totalCostMatrix[iOffset + x, jOffset + y] == double.PositiveInfinity)
                    {
                        color = undefColor;
                    }
                    else if (val < 0)
                    {
                        color = minColor;
                    }
                    else if (val >= colorPalette.Length)
                    {
                        color = maxColor;
                    }
                    else
                    {
                        color = colorPalette[(int)val];
                    }
                    pixels[writeableBitmap.PixelWidth * y + x] = color;
                }
            }

            // draw path
            foreach (DTW.Pair p in path)
            {
                if (p.i1 <= iOffset || p.i2 <= jOffset)
                {
                    break;
                }
                pixels[writeableBitmap.PixelWidth * (p.i2 - jOffset - 1) + (p.i1 - iOffset - 1)] = pathColor;
            }

            // paint
            writeableBitmap.WritePixels(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight),
                                        pixels, writeableBitmap.PixelWidth * 4, 0);
            InvalidateVisual();
        }
Example #5
0
    public RecognitionThread(ActionInstance seq, ActionData subseq, mIndex[] stepConstraints)
    {
        _seq    = seq;
        _subseq = subseq;
        dtw     = new DTW(stepConstraints);

        _isDone = false;
    }
Example #6
0
        /// <summary>
        /// This function is called to get normalised result before passing to DTW algorithm.
        /// The input from the Kinect and the text files are normalised and passed into DTW.
        /// If the gesture name matches with the input, the name of gesture is received in the parameter finalgesture.
        /// </summary>
        /// <returns>returns the gesture matched from DTW with least cost</returns>
        private string passToDTW()
        {
            string finalgesture = String.Empty;

            try
            {   //initialization of Dictionary where key=gesture and value is the cost from DTW
                var       mySortedDictionary = new SortedDictionary <string, double>();
                Recorder  rd = new Recorder();
                ArrayList gesturefromkinect = rd.readfiles(KINECT_COORDINATES);// getting normalised co ordinates here.....
                string    sourceDirectory   = @"d:\DTW_files\2D\Hristo";
                var       txtFiles          = Directory.EnumerateFiles(sourceDirectory, "*.txt");
                foreach (string currentFile in txtFiles)
                {
                    System.Diagnostics.Debug.WriteLine("\nComparing with " + currentFile);
                    string          storedtextLines  = System.IO.File.ReadAllText(currentFile);
                    List <double[]> normalisedresult = new List <double[]>();

                    string[] result = storedtextLines.Split('@');
                    for (int i = 0; i < result.Length; i++)
                    {
                        normalisedresult = rd.explode_array(result, ',');
                    }
                    int dimension      = 12;
                    int threshold      = 2;
                    int firstThreshold = 4;
                    int maxSlope       = 2;
                    int minimumLength  = 10;
                    _dtw = new DTW(dimension, threshold, firstThreshold, maxSlope, minimumLength);
                    ArrayList arrlstTemp = ArrayList.Adapter(normalisedresult);
                    _dtw.Add(arrlstTemp, Path.GetFileNameWithoutExtension(currentFile));
                    finalgesture = _dtw.recognize(gesturefromkinect);
                    if ((!finalgesture.Contains("__UNKNOWN")))
                    {
                        string[] words = finalgesture.Split('@');
                        double   Num;
                        bool     isNum = double.TryParse(words[1], out Num);
                        if (isNum)
                        {
                            mySortedDictionary.Add(words[0], Num);
                        }
                    }
                    KINECT_COORDINATES = new string[] { };
                }
                //find the gesture with the least cost from the set of the gestures
                if (mySortedDictionary.Count >= 1)
                {
                    double[]     indexableValues = mySortedDictionary.Values.ToArray();
                    double       min             = indexableValues.Min();
                    string       key             = (from d in mySortedDictionary where d.Value == min select d).First().Key;
                    GestureStore ig = new GestureStore("Recoginised as:" + key);
                }
            }
            catch (Exception ex)
            {
            }

            return(finalgesture);
        }
Example #7
0
        static void Main(string[] args)
        {
            List <Double> seriesA = (new double[] { 4, 5, 6, 7, 8 }).ToList();
            List <Double> seriesB = (new double[] { 4, 5, 6, 7, 8 }).ToList();

            var distance = new EuclidianDistance();
            var cost     = DTW <Double> .getDistance(seriesA, seriesB, distance, 4);

            Console.WriteLine("Cost: " + cost);
            Console.ReadLine();
        }
Example #8
0
 public void DeterminePerformance(List <Skeleton> skeletons)
 {
     try
     {
         this.Performance = DTW <Skeleton> .Distance(this.Skeletons, skeletons, new SkeletonDistance());
     }
     catch (Exception)
     {
         this.Performance = 100;
     }
 }
Example #9
0
    void OnRecognitionDone(ActionData matchedinst, ActionInstance insttomatch, DTW dtw)
    {
        ActionInstance warpedinst = new ActionInstance();

        foreach (mIndex idx in dtw.WarpPath)
        {
            warpedinst.poses.Add(insttomatch.poses[idx.x]);
        }

        print("-- cost " + matchedinst.actionId + " " + dtw.getWarpPathCost());

        onRecognitionFinished(matchedinst.actionId, warpedinst);
    }
Example #10
0
        static void Main(string[] args)
        {
            DTW       ucrDtw;
            Query     query;
            DTWResult result;

            int   dimensions         = Int16.Parse(args[2]);
            int   queryStretchLength = Int16.Parse(args[3]);
            float warpingWindow      = (float)Double.Parse(args[4]);

            ucrDtw = new DTW(dimensions, warpingWindow);
            query  = ucrDtw.Query();

            using (TextReader reader = new StreamReader(args[0]))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] strs = line.Split(' ');

                    double[] a = new double[strs.Length];

                    for (int itt = 0; itt < strs.Length; itt++)
                    {
                        if (String.IsNullOrEmpty(strs[itt]))
                        {
                            continue;
                        }

                        a[itt] = double.Parse(strs[itt]);
                    }

                    ucrDtw.addDataItem(dimensions, a);
                }
            }

            List <double[]> source = new List <double[]>();
            List <double[]> stretched;

            using (TextReader reader = new StreamReader(args[1]))
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    if (String.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    string[] strs = line.Split(' ');

                    double[] a = new double[dimensions];

                    for (int itt = 0; itt < strs.Length; itt++)
                    {
                        if (String.IsNullOrEmpty(strs[itt]))
                        {
                            continue;
                        }

                        a[itt] = double.Parse(strs[itt]);
                    }

                    source.Add(a);
                }
            }

            stretched = Utilities.stretchData(source, dimensions, queryStretchLength);

            //writeCsvFile("source.csv", source, dimensions);
            //writeCsvFile("stretched.csv", stretched, dimensions);

            foreach (double[] row in stretched)
            {
                query.addQueryItem(row);
            }

            result = ucrDtw.warp(query);

            Console.WriteLine("Distance: " + result.Distance);
            Console.WriteLine("Location: " + result.Location);
        }
Example #11
0
        private void TimeWarp(TimeWarpType type, AudioTrack t1, TimeSpan t1From, TimeSpan t1To, AudioTrack t2, TimeSpan t2From, TimeSpan t2To, bool calculateSimilarity, bool normalizeSimilarity, bool cueIn, bool cueOut)
        {
            IAudioStream s1 = t1.CreateAudioStream();
            IAudioStream s2 = t2.CreateAudioStream();

            s1 = new CropStream(s1, TimeUtil.TimeSpanToBytes(t1From, s1.Properties), TimeUtil.TimeSpanToBytes(t1To, s1.Properties));
            s2 = new CropStream(s2, TimeUtil.TimeSpanToBytes(t2From, s2.Properties), TimeUtil.TimeSpanToBytes(t2To, s2.Properties));

            List <Tuple <TimeSpan, TimeSpan> > path = null;
            DTW dtw = null;

            // execute time warping
            if (type == TimeWarpType.DTW)
            {
                dtw = new DTW(TimeWarpSearchWidth, progressMonitor);
            }
            else if (type == TimeWarpType.OLTW)
            {
                dtw = new OLTW2(TimeWarpSearchWidth, progressMonitor);
            }

            if (TimeWarpDisplay)
            {
                this.Dispatcher.BeginInvoke((Action) delegate {
                    dtwPathViewer = new DtwPathViewer();
                    dtwPathViewer.Show();
                });

                dtw.OltwInit += new DTW.OltwInitDelegate(delegate(int windowSize, IMatrix <double> cellCostMatrix, IMatrix <double> totalCostMatrix) {
                    dtwPathViewer.Dispatcher.BeginInvoke((Action) delegate {
                        dtwPathViewer.DtwPath.Init(windowSize, cellCostMatrix, totalCostMatrix);
                    });
                });
                bool drawing = false;
                dtw.OltwProgress += new DTW.OltwProgressDelegate(delegate(int i, int j, int minI, int minJ, bool force) {
                    if (!drawing || force)
                    {
                        dtwPathViewer.Dispatcher.BeginInvoke((Action) delegate {
                            drawing = true;
                            dtwPathViewer.DtwPath.Refresh(i, j, minI, minJ);
                            drawing = false;
                        });
                    }
                });
            }

            path = dtw.Execute(s1, s2);

            if (path == null)
            {
                return;
            }

            // convert resulting path to matches and filter them
            int               filterSize       = TimeWarpFilterSize; // take every n-th match and drop the rest
            bool              smoothing        = TimeWarpSmoothing;
            int               smoothingWidth   = Math.Max(1, Math.Min(filterSize / 10, filterSize));
            bool              inOutCue         = TimeWarpInOutCue;
            TimeSpan          inOutCueSpan     = TimeWarpSearchWidth;
            List <Match>      matches          = new List <Match>();
            float             maxSimilarity    = 0; // needed for normalization
            IProgressReporter progressReporter = progressMonitor.BeginTask("post-process resulting path...", true);
            double            totalProgress    = path.Count;
            double            progress         = 0;

            /* Leave out matches in the in/out cue areas...
             * The matches in the interval at the beginning and end of the calculated time warping path with a width
             * equal to the search width should be left out because they might not be correct - since the time warp
             * path needs to start at (0,0) in the matrix and end at (m,n), they would only be correct if the path gets
             * calculated between two synchronization points. Paths calculated from the start of a track to the first
             * sync point, or from the last sync point to end of the track are probably wrong in this interval since
             * the start and end points don't match if there is time drift so it is better to leave them out in those
             * areas... in those short a few second long intervals the drict actually will never be that extreme that
             * someone would notice it anyway. */
            if (inOutCue)
            {
                int startIndex = 0;
                int endIndex   = path.Count;

                // this needs a temporally ordered mapping path (no matter if ascending or descending)
                foreach (Tuple <TimeSpan, TimeSpan> mapping in path)
                {
                    if (cueIn && (mapping.Item1 < inOutCueSpan || mapping.Item2 < inOutCueSpan))
                    {
                        startIndex++;
                    }
                    if (cueOut && (mapping.Item1 > (t1To - t1From - inOutCueSpan) || mapping.Item2 > (t2To - t2From - inOutCueSpan)))
                    {
                        endIndex--;
                    }
                }
                path = path.GetRange(startIndex, endIndex - startIndex);
            }

            for (int i = 0; i < path.Count; i += filterSize)
            {
                //List<Tuple<TimeSpan, TimeSpan>> section = path.GetRange(i, Math.Min(path.Count - i, filterSize));
                List <Tuple <TimeSpan, TimeSpan> > smoothingSection = path.GetRange(
                    Math.Max(0, i - smoothingWidth / 2), Math.Min(path.Count - i, smoothingWidth));
                Tuple <TimeSpan, TimeSpan> match = path[i];

                if (smoothingSection.Count == 0)
                {
                    throw new InvalidOperationException("must not happen");
                }
                else if (smoothingSection.Count == 1 || !smoothing || i == 0)
                {
                    // do nothing, match doesn't need any processing
                    // the first and last match must not be smoothed since they must sit at the bounds
                }
                else
                {
                    List <TimeSpan> offsets = new List <TimeSpan>(smoothingSection.Select(t => t.Item2 - t.Item1).OrderBy(t => t));
                    int             middle  = offsets.Count / 2;

                    // calculate median
                    // http://en.wikiversity.org/wiki/Primary_mathematics/Average,_median,_and_mode#Median
                    TimeSpan smoothedDriftTime = new TimeSpan((offsets[middle - 1] + offsets[middle]).Ticks / 2);
                    match = new Tuple <TimeSpan, TimeSpan>(match.Item1, match.Item1 + smoothedDriftTime);
                }

                float similarity = calculateSimilarity ? (float)Math.Abs(CrossCorrelation.Correlate(
                                                                             s1, new Interval(match.Item1.Ticks, match.Item1.Ticks + TimeUtil.SECS_TO_TICKS),
                                                                             s2, new Interval(match.Item2.Ticks, match.Item2.Ticks + TimeUtil.SECS_TO_TICKS))) : 1;

                if (similarity > maxSimilarity)
                {
                    maxSimilarity = similarity;
                }

                matches.Add(new Match()
                {
                    Track1     = t1,
                    Track1Time = match.Item1 + t1From,
                    Track2     = t2,
                    Track2Time = match.Item2 + t2From,
                    Similarity = similarity,
                    Source     = type.ToString()
                });

                progressReporter.ReportProgress(progress / totalProgress * 100);
                progress += filterSize;
            }

            // add last match if it hasn't been added
            if (path.Count > 0 && path.Count % filterSize != 1)
            {
                Tuple <TimeSpan, TimeSpan> lastMatch = path[path.Count - 1];
                matches.Add(new Match()
                {
                    Track1     = t1,
                    Track1Time = lastMatch.Item1 + t1From,
                    Track2     = t2,
                    Track2Time = lastMatch.Item2 + t2From,
                    Similarity = 1,
                    Source     = type.ToString()
                });
            }
            progressReporter.Finish();

            multiTrackViewer.Dispatcher.BeginInvoke((Action) delegate {
                foreach (Match match in matches)
                {
                    if (normalizeSimilarity)
                    {
                        match.Similarity /= maxSimilarity; // normalize to 1
                    }
                    multiTrackViewer.Matches.Add(match);
                }
            });

            s1.Close();
            s2.Close();
        }
    void Start()
    {
        Random.seed = System.DateTime.Now.Millisecond;
        Pose[] vec1 = new Pose[] {
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random()
        };
        Pose[] svec = new Pose[] {
            vec1[4], vec1[6], vec1[8], vec1[10],
            vec1[12], vec1[13], vec1[14], vec1[15],
            vec1[16], vec1[18], vec1[20], vec1[22],
            vec1[24], vec1[27], vec1[30], vec1[31],
            vec1[32], vec1[33], vec1[34], vec1[35],
            vec1[37], vec1[38], vec1[40], vec1[42]
        };
        //		Float[] vec1 = new Float[] {
        //			new Float(0), new Float(0.75f), new Float(1.5f), new Float(0.75f), new Float(0), new Float(-0.75f), new Float(-1.5f), new Float(-0.75f), new Float(0)
        //		};
        //		Float[] svec = new Float[] {
        //			new Float(0), new Float(-0.5f), new Float(-1), new Float(-1.5f)
        //		};
        //
        //		float[] vec1 = new float[] { 0, 0.75f, 1.5f, 0.75f, 0, -0.75f, -1.5f, -0.75f, 0};
        //		float[] svec = new float[] { 0, -0.5f, -1, -1.5f};

        int start = System.DateTime.Now.Millisecond;
        DTW dtw = new DTW(new mIndex[] {new mIndex(1,1), new mIndex(1,2), new mIndex(2,1)});
        List<mIndex> wres = dtw.warpSubsequenceDTW(vec1, svec);
        Debug.Log("-- Time " + (System.DateTime.Now.Millisecond-start));

        mDebug.printMatrix(dtw.CostMatrix);
        mDebug.printMatrix(dtw.AccumCostMatrix);
        foreach(mIndex step in wres)
        {
            print(step + " " + dtw.AccumCostMatrix[step.y][step.x]);
        }

        //		Texture2D tmpT = mDebug.drawMatrix(dtw.CostMatrix);
        Texture2D tmpT = mDebug.drawMatrix(dtw.AccumCostMatrix);
        tmpT = mDebug.drawPoints(tmpT, wres.ToArray(), Color.red);

        costMatrixTexture.texture = tmpT;
        costMatrixTexture.transform.localScale = new Vector3(0.3f, 0.3f*((float)svec.Length/(float)vec1.Length), 1);
        costMatrixTexture.transform.position = new Vector3(0.5f*costMatrixTexture.transform.localScale.x, costMatrixTexture.transform.localScale.y, 0);
    }
Example #13
0
    void Start()
    {
        Random.seed = System.DateTime.Now.Millisecond;
        Pose[] vec1 = new Pose[] {
            Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(),
               Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random(), Pose.random()
        };
        Pose[] svec = new Pose[] {
            vec1[4], vec1[6], vec1[8], vec1[10],
            vec1[12], vec1[13], vec1[14], vec1[15],
            vec1[16], vec1[18], vec1[20], vec1[22],
            vec1[24], vec1[27], vec1[30], vec1[31],
            vec1[32], vec1[33], vec1[34], vec1[35],
            vec1[37], vec1[38], vec1[40], vec1[42]
        };
//		Float[] vec1 = new Float[] {
//			new Float(0), new Float(0.75f), new Float(1.5f), new Float(0.75f), new Float(0), new Float(-0.75f), new Float(-1.5f), new Float(-0.75f), new Float(0)
//		};
//		Float[] svec = new Float[] {
//			new Float(0), new Float(-0.5f), new Float(-1), new Float(-1.5f)
//		};
//
//		float[] vec1 = new float[] { 0, 0.75f, 1.5f, 0.75f, 0, -0.75f, -1.5f, -0.75f, 0};
//		float[] svec = new float[] { 0, -0.5f, -1, -1.5f};

        int           start = System.DateTime.Now.Millisecond;
        DTW           dtw   = new DTW(new mIndex[] { new mIndex(1, 1), new mIndex(1, 2), new mIndex(2, 1) });
        List <mIndex> wres  = dtw.warpSubsequenceDTW(vec1, svec);

        Debug.Log("-- Time " + (System.DateTime.Now.Millisecond - start));

        mDebug.printMatrix(dtw.CostMatrix);
        mDebug.printMatrix(dtw.AccumCostMatrix);
        foreach (mIndex step in wres)
        {
            print(step + " " + dtw.AccumCostMatrix[step.y][step.x]);
        }

//		Texture2D tmpT = mDebug.drawMatrix(dtw.CostMatrix);
        Texture2D tmpT = mDebug.drawMatrix(dtw.AccumCostMatrix);

        tmpT = mDebug.drawPoints(tmpT, wres.ToArray(), Color.red);

        costMatrixTexture.texture = tmpT;
        costMatrixTexture.transform.localScale = new Vector3(0.3f, 0.3f * ((float)svec.Length / (float)vec1.Length), 1);
        costMatrixTexture.transform.position   = new Vector3(0.5f * costMatrixTexture.transform.localScale.x, costMatrixTexture.transform.localScale.y, 0);
    }
    public RecognitionThread(ActionInstance seq, ActionData subseq, mIndex[] stepConstraints)
    {
        _seq = seq;
        _subseq = subseq;
         	dtw = new DTW(stepConstraints);

        _isDone = false;
    }
    void OnRecognitionDone(ActionData matchedinst, ActionInstance insttomatch, DTW dtw)
    {
        ActionInstance warpedinst = new ActionInstance();
        foreach(mIndex idx in dtw.WarpPath)
        {
            warpedinst.poses.Add(insttomatch.poses[idx.x]);
        }

        print("-- cost " + matchedinst.actionId + " " + dtw.getWarpPathCost());

        onRecognitionFinished(matchedinst.actionId, warpedinst);
    }
    static void Main()
    {
        var server = new WebSocketServer("ws://127.0.0.1:8181");

        server.Start(socket =>
        {
            socket.OnOpen  = () => Console.WriteLine("Open!");
            socket.OnClose = () => Console.WriteLine("Close!");

            socket.OnMessage = message =>
            {
                JObject j           = JObject.Parse(message);
                int k               = j.GetValue("k").ToObject <int>();
                JArray searchseries = (JArray)j["searchseries"];
                int dimensions      = searchseries[0].ToObject <double[]>().Length;

                var sw     = Stopwatch.StartNew();
                DTW ucrDtw = new DTW(dimensions, k);
                foreach (JArray datapoint in searchseries)
                {
                    ucrDtw.addDataItem(datapoint.ToObject <double[]>());
                }
                JArray snippets = (JArray)j["querysnippets"];
                DTWResult localresults;

                JArray matches   = new JArray();
                JArray distances = new JArray();
                JArray queryids  = new JArray();
                int quid         = 0;

                foreach (JArray snippet in snippets)
                {
                    Query query = ucrDtw.Query();
                    foreach (JArray datapoint in snippet)
                    {
                        query.addQueryItem(datapoint.ToObject <double[]>());
                    }

                    localresults = ucrDtw.warp(query);
                    matches.Add(localresults.Locations);
                    distances.Add(localresults.Distances);
                    foreach (int thatloc in localresults.Locations)
                    {
                        queryids.Add(quid);
                    }
                    Console.WriteLine("Query " + quid + " finished.");
                    quid = quid + 1;
                }
                sw.Stop();

                JObject response = new JObject();
                response.Add("querytime", Math.Round(sw.Elapsed.TotalMilliseconds) / 1000.0);
                response.Add("matchlocations", matches);
                response.Add("queryids", queryids);
                response.Add("distances", distances);
                socket.Send(response.ToString());
            };
        });
        Console.WriteLine("Waiting for requests...");
        var name = Console.ReadLine();
    }