Browse Source

-added Timer class
-enabled timer-output
-disabled touch-output

Alexander Hendrich 11 years ago
parent
commit
2d8656d260
4 changed files with 91 additions and 4 deletions
  1. 11 2
      bbiwarg/Graphics/OutputWindow.cs
  2. 52 0
      bbiwarg/Utility/Timer.cs
  3. 27 2
      bbiwarg/VideoHandle.cs
  4. 1 0
      bbiwarg/bbiwarg.csproj

+ 11 - 2
bbiwarg/Graphics/OutputWindow.cs

@@ -9,6 +9,7 @@ using OpenTK;
 using OpenTK.Graphics.OpenGL;
 using bbiwarg.Images;
 using bbiwarg.Detectors.Touch;
+using bbiwarg.Utility;
 
 using Emgu.CV;
 
@@ -81,6 +82,7 @@ namespace bbiwarg.Graphics
 
         protected override void OnRenderFrame(FrameEventArgs e)
         {
+            Timer.start("onRenderFrame");
 
             base.OnRenderFrame(e);
             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
@@ -140,11 +142,11 @@ namespace bbiwarg.Graphics
 
             if (changedFrame || !videoHandle.sourceIsMovie())
             {
-                foreach (PalmTouchEvent ev in videoHandle.getTouchEvents())
+                /*foreach (PalmTouchEvent ev in videoHandle.getTouchEvents())
                 {
                     Console.WriteLine("touch at " + ev.Position + " -> " + ev.RelativePalmPosition);
                     //touchVisualizer.addTouchEvent(ev);
-                }
+                }*/
             }
 
             // kalman demo
@@ -156,6 +158,8 @@ namespace bbiwarg.Graphics
             if (OpenTK.Input.Mouse.GetState().IsButtonDown(OpenTK.Input.MouseButton.Right))
                 kalmanDemo.reset();
 
+
+            Timer.start("outputTextures");
             //draw textures
             Int16[] depthTextureData = new Int16[3 * videoHandle.Width * videoHandle.Height];
             Int16[] edgeTextureData = new Int16[3 * videoHandle.Width * videoHandle.Height];
@@ -289,6 +293,11 @@ namespace bbiwarg.Graphics
             GL.End();
 
             SwapBuffers();
+            Timer.stop("outputTextures");
+
+            Timer.stop("onRenderFrame");
+
+            Timer.outputAll();
         }
 
     }

+ 52 - 0
bbiwarg/Utility/Timer.cs

@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg.Utility
+{
+    class Timer
+    {
+        private static Dictionary<String, Stopwatch> stopwatches = new Dictionary<string, Stopwatch>();
+        private static Dictionary<String, long> minTimes = new Dictionary<string, long>();
+        private static Dictionary<String, long> maxTimes = new Dictionary<string, long>();
+        private static Dictionary<String, long> sumTimes = new Dictionary<string, long>();
+        private static Dictionary<String, int> numTimes = new Dictionary<string, int>();
+
+        public static void start(String name) {
+            if (!stopwatches.ContainsKey(name))
+            {
+                stopwatches.Add(name, new Stopwatch());
+                minTimes.Add(name, int.MaxValue);
+                maxTimes.Add(name, 0);
+                sumTimes.Add(name, 0);
+                numTimes.Add(name, 0);
+            }
+            stopwatches[name].Restart();
+        }
+
+        public static void stop(String name) {
+            stopwatches[name].Stop();
+            long time = stopwatches[name].ElapsedMilliseconds;
+            if (time < minTimes[name]) minTimes[name] = time;
+            if (time > maxTimes[name]) maxTimes[name] = time;
+            sumTimes[name] += time;
+            numTimes[name]++;
+        }
+
+        public static void output(String name) {
+            Console.WriteLine("name:" + name + "\t" + "mean:" + (sumTimes[name] / Math.Max(numTimes[name],1)) + "\t" + "min:" + minTimes[name] + "\t" + "max:" + maxTimes[name]);
+        }
+
+        public static void outputAll() {
+            Console.WriteLine("---TIMERS-START---");
+            foreach (String name in stopwatches.Keys) {
+                output(name);
+            }
+            Console.WriteLine("---TIMERS-END---");
+        }
+
+    }
+}

+ 27 - 2
bbiwarg/VideoHandle.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Diagnostics;
+using bbiwarg.Utility;
 using bbiwarg.Detectors.Fingers;
 using bbiwarg.Detectors.Palm;
 using bbiwarg.Detectors.Touch;
@@ -128,12 +129,17 @@ namespace bbiwarg
 
         private void processFrameUpdate()
         {
+            Timer.start("processFrameUpdate");
+
             //read data from inputProvider
+            Timer.start("readInputData");
             inputFrame = inputProvider.getInputFrame();
             Width = inputFrame.Width;
             Height = inputFrame.Height;
+            Timer.stop("readInputData");
 
             //create depthImage
+            Timer.start("createDepthImage");
             Int16 minDepth = Int16.MaxValue;
             Image<Gray, Int16> image = new Image<Gray, Int16>(Width, Height);
             for (int x = 0; x < Width; x++)
@@ -147,25 +153,44 @@ namespace bbiwarg
                 }
             }
             depthImage = new DepthImage(image, minDepth);
+            Timer.stop("createDepthImage");
 
             //create images
+            Timer.start("createOtherImages");
             edgeImage = new EdgeImage(depthImage);
             touchImage = new TouchImage(Width, Height);
             fingerImage = new FingerImage(Width, Height);
             palmImage = new PalmImage(Width, Height);
+            Timer.stop("createOtherImages");
 
-            //detect+track fingers
+            //detect fingers
+            Timer.start("fingerDetection");
             fingerDetector = new FingerDetector(depthImage, edgeImage, fingerImage);
+            Timer.stop("fingerDetection");
+
+            //track fingers
+            Timer.start("fingerTracking");
             fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, fingerImage);
+            Timer.stop("fingerTracking");
 
             //detect palm
+            Timer.start("palmDetection");
             palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector.Fingers, palmImage);
+            Timer.stop("palmDetection");
 
-            //detect+track touchEvents
+            //detect touchEvents
+            Timer.start("touchDetection");
             touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, touchImage);
             if (palmDetector.PalmQuad != null)
                 palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad);
+            Timer.stop("touchDetection");
+
+            //track touchEvents
+            Timer.start("touchTracking");
             touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, touchImage);
+            Timer.stop("touchTracking");
+
+            Timer.stop("processFrameUpdate");
         }
     }
 }

+ 1 - 0
bbiwarg/bbiwarg.csproj

@@ -94,6 +94,7 @@
     <Compile Include="Utility\Line2D.cs" />
     <Compile Include="Utility\LineSegment2D.cs" />
     <Compile Include="Utility\Quadrangle.cs" />
+    <Compile Include="Utility\Timer.cs" />
     <Compile Include="Utility\Vector2D.cs" />
     <Compile Include="VideoHandle.cs" />
   </ItemGroup>