using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using bbiwarg.Detectors.Fingers; using bbiwarg.Detectors.Palm; using bbiwarg.Detectors.Touch; using bbiwarg.Images; using bbiwarg.InputProviders; using Emgu.CV; using Emgu.CV.Structure; namespace bbiwarg { class VideoHandle { private IInputProvider inputProvider; private InputFrame inputFrame; public int Width { get; private set; } public int Height { get; private set; } private DepthImage depthImage; private EdgeImage edgeImage; private PalmImage palmImage; private TouchImage touchImage; private FingerImage fingerImage; private FingerDetector fingerDetector; private PalmDetector palmDetector; private TouchDetector touchDetector; private PalmTouchDetector palmTouchDetector; private FingerTracker fingerTracker; private TouchTracker touchTracker; public VideoHandle(IInputProvider inputProvider) { this.inputProvider = inputProvider; //initialize trackers touchTracker = new TouchTracker(); fingerTracker = new FingerTracker(); } public void start() { inputProvider.init(); inputProvider.start(); inputProvider.updateFrame(); processFrameUpdate(); } public void stop() { inputProvider.stop(); } public bool sourceIsMovie() { return inputProvider.sourceIsMovie(); } public void reversePlay() { inputProvider.reversePlay(); } public void pauseMovie() { inputProvider.pauseMovie(); } public void unpauseMovie() { inputProvider.unpauseMovie(); } public void nextFrame() { if (inputProvider.isActive()) { inputProvider.releaseFrame(); inputProvider.updateFrame(); processFrameUpdate(); } else { inputProvider.stop(); } } public Int16 getDepthAt(int x, int y) { return depthImage.getDepthAt(x, y); } public float getRelativeDepth(int x, int y) { return depthImage.getRelativeDepthAt(x, y); } public bool isEdgeAt(int x, int y) { return edgeImage.isEdgeAt(x, y); } public FingerImageState getFingerImageStateAt(int x, int y) { return fingerImage.getStateAt(x, y); } public PalmImageState getPalmImageStateAt(int x, int y) { return palmImage.getStateAt(x, y); } public TouchImageState getTouchImageStateAt(int x, int y) { return touchImage.getStateAt(x, y); } public List getTouchEvents() { return palmTouchDetector.PalmTouchEvents; } private void processFrameUpdate() { //read data from inputProvider inputFrame = inputProvider.getInputFrame(); Width = inputFrame.Width; Height = inputFrame.Height; //create depthImage Int16 minDepth = Int16.MaxValue; Image image = new Image(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { Int16 depth = inputFrame.getDepthAt(x, y); image.Data[y, x, 0] = depth; if (depth < minDepth) minDepth = depth; } } depthImage = new DepthImage(image, minDepth); //create images edgeImage = new EdgeImage(depthImage); touchImage = new TouchImage(Width, Height); fingerImage = new FingerImage(Width, Height); palmImage = new PalmImage(Width, Height); //detect+track fingers fingerDetector = new FingerDetector(depthImage, edgeImage, fingerImage); fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, fingerImage); //detect palm palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector.Fingers, palmImage); //detect+track touchEvents touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, touchImage); if (palmDetector.PalmQuad != null) palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad); touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, touchImage); } } }