public MainWindow() { InitializeComponent(); network.init(); _sensor = KinectSensor.GetDefault(); if(_sensor != null) { _sensor.Open(); bodyCount = _sensor.BodyFrameSource.BodyCount; // Identify the bodies _bodies = new Body[bodyCount]; _colorReader = _sensor.ColorFrameSource.OpenReader(); _colorReader.FrameArrived += ColorReader_FrameArrived; _bodyReader = _sensor.BodyFrameSource.OpenReader(); _bodyReader.FrameArrived += BodyReader_FrameArrived; // Initialize the face source with the desired features. _faceSources = new FaceFrameSource[bodyCount]; _faceReaders = new FaceFrameReader[bodyCount]; for(int i = 0; i < bodyCount; i++) { // Create the face frame source with the required features and initial tracking id of 0 _faceSources[i] = new FaceFrameSource(_sensor, 0, FaceFrameFeatures.BoundingBoxInColorSpace); // open the corresponding reader _faceReaders[i] = _faceSources[i].OpenReader(); _faceReaders[i].FrameArrived += FaceReader_FrameArrived; } _faceResults = new FaceFrameResult[bodyCount]; // Set the arrays and values for person switches and timeouts personSize = 3; ims = new Image[3] {maskImage, maskImage2, maskImage3}; trackedInd = new bool[3] { false, false, false }; _persons = new Person[personSize]; for(int i = 0; i < personSize; i++) { _persons[i] = new Person(0, ims[i], -1); } paths = new String[3] { "pack://application:,,,/Images/tinfoil.png", "pack://application:,,,/Images/cowboy.png", "pack://application:,,,/Images/napolean.png"}; } }
/// <summary> /// Each result it drawn for each person attached to the result. /// </summary> /// <param name="p"></param> /// <param name="f"></param> void DrawFaceFrameResults(Person p, FaceFrameResult f) { var bb = f.FaceBoundingBoxInColorSpace; // Gather the width of the bounding box then the size difference double width = Math.Abs(bb.Right - bb.Left) * 1.8; double height = Math.Abs(bb.Bottom - bb.Top) * 1.8; double wDiff = Math.Abs(width - p.imageRef.Width); double hDiff = Math.Abs(height - p.imageRef.Height); // This will tell whether or not the image should be resized. if (wDiff / p.imageRef.Width > 0.25 || hDiff / p.imageRef.Height > 0.25 || Double.IsNaN(p.imageRef.Width)) { if (width > 0 && height > 0) { p.imageRef.Width = width; p.imageRef.Height = height; } } else { width = p.imageRef.Width; height = p.imageRef.Height; } // Gather the coordinates of the image and the location difference double left = bb.Left - width * 0.2; double top = bb.Top - height * 0.2 - height * 0.65; double lDiff = Math.Abs(Canvas.GetLeft(p.imageRef) - left); double tDiff = Math.Abs(Canvas.GetTop(p.imageRef) - top); // this will tell whether or not the image should be translated. if (lDiff / Canvas.GetLeft(p.imageRef) > 0.07 || tDiff / Canvas.GetTop(p.imageRef) > 0.07 || Double.IsNaN(Canvas.GetTop(p.imageRef))) { if (left > 0 && top > 0) { p.left = left; p.top = top; } Canvas.SetLeft(p.imageRef, p.left); Canvas.SetTop(p.imageRef, p.top); } p.imageRef.Visibility = Visibility.Visible; }