private void Render(Bitmap bitmap, MyTrackedPerson myTrackedPerson, MyBlobs myBlobs) { Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { // Update the bitmap image BitmapImage bitmapImage = ConvertBitmap(bitmap); if (bitmapImage != null) { imgStream.Source = bitmapImage; } // Update the data labels lblFacesDetected.Content = string.Format("Faces Detected: {0}", myTrackedPerson.FacesDetected); lblFaceH.Content = string.Format("Face Rect H: {0}", myTrackedPerson.FaceH); lblFaceW.Content = string.Format("Face Rect W: {0}", myTrackedPerson.FaceW); lblFaceX.Content = string.Format("Face Coord X: {0}", myTrackedPerson.FaceX); lblFaceY.Content = string.Format("Face Coord Y: {0}", myTrackedPerson.FaceY); lblFaceDepth.Content = string.Format("Face Depth: {0}", myTrackedPerson.FaceDepth); lblNumberPersons.Content = string.Format("Persons Detected: {0}", myTrackedPerson.PersonsDetected); lblPersonH.Content = string.Format("Person Rect H: {0}", myTrackedPerson.H); lblPersonW.Content = string.Format("Person Rect W: {0}", myTrackedPerson.W); lblPersonX.Content = string.Format("Person Coord X: {0}", myTrackedPerson.X); lblPersonY.Content = string.Format("Person Coord Y: {0}", myTrackedPerson.Y); // Show or hide the markers if (chkShowMarkers.IsChecked == true) { if (myTrackedPerson.FacesDetected == 1) { rectFaceMarker.Height = myTrackedPerson.FaceH; rectFaceMarker.Width = myTrackedPerson.FaceW; Canvas.SetLeft(rectFaceMarker, myTrackedPerson.FaceX); Canvas.SetTop(rectFaceMarker, myTrackedPerson.FaceY); rectFaceMarker.Visibility = Visibility.Visible; } else { rectFaceMarker.Visibility = Visibility.Hidden; } if (myTrackedPerson.PersonsDetected == 1) { rectPersonMarker.Height = myTrackedPerson.H; rectPersonMarker.Width = myTrackedPerson.W; Canvas.SetLeft(rectPersonMarker, myTrackedPerson.X); Canvas.SetTop(rectPersonMarker, myTrackedPerson.Y); rectPersonMarker.Visibility = Visibility.Visible; } else { rectPersonMarker.Visibility = Visibility.Hidden; } // draw blobs' contours (outer only) // actually, don't bother drawing because it's stupid slow /* * if (myBlobs.numBlobs > 0) * { * for (int i = 0; i < myBlobs.numBlobs; i++) * { * List<PXCMPointI32> currentBlob = myBlobs.blobs[i]; * * for (int j = 0; j < currentBlob.Count; j++) * { * Ellipse dot = new Ellipse(); * dot.Width = 2; * dot.Height = 2; * * SolidColorBrush blueBrush = new SolidColorBrush(); * blueBrush.Color = Colors.Blue; * dot.Fill = blueBrush; * * Canvas.SetLeft(dot, currentBlob[0].x); * Canvas.SetTop(dot, currentBlob[0].y); * * //MainCanvas.Children.Add(dot); * } * } * } */ } else { rectFaceMarker.Visibility = Visibility.Hidden; rectPersonMarker.Visibility = Visibility.Hidden; } })); }
private void Update() { // Start AcquireFrame-ReleaseFrame loop while (sm.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR) { // Acquire color image data PXCMCapture.Sample sample = sm.QuerySample(); Bitmap colorBitmap; PXCMImage.ImageData colorData; sample.color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out colorData); colorBitmap = colorData.ToBitmap(0, sample.color.info.width, sample.color.info.height); // Create an instance of MyTrackedPerson MyTrackedPerson myTrackedPerson = new MyTrackedPerson(); MyBlobs myBlobs = new MyBlobs(); // Acquire person tracking data personData = personModule.QueryOutput(); myTrackedPerson.PersonsDetected = personData.QueryNumberOfPeople(); if (myTrackedPerson.PersonsDetected == 1) { PXCMPersonTrackingData.Person trackedPerson = personData.QueryPersonData(PXCMPersonTrackingData.AccessOrderType.ACCESS_ORDER_BY_ID, 0); PXCMPersonTrackingData.PersonTracking trackedPersonData = trackedPerson.QueryTracking(); PXCMPersonTrackingData.BoundingBox2D personBox = trackedPersonData.Query2DBoundingBox(); myTrackedPerson.X = personBox.rect.x; myTrackedPerson.Y = personBox.rect.y; myTrackedPerson.H = personBox.rect.h; myTrackedPerson.W = personBox.rect.w; /* * PXCMPersonTrackingData.PersonJoints personJoints = trackedPerson.QuerySkeletonJoints(); * PXCMPersonTrackingData.PersonJoints.SkeletonPoint[] skeletonPoints = new PXCMPersonTrackingData.PersonJoints.SkeletonPoint[personJoints.QueryNumJoints()]; * trackedPerson.QuerySkeletonJoints().QueryJoints(skeletonPoints); * if (skeletonPoints.Length > 0) * skeletonPoints[0].GetType(); */ } // Acquire face tracking data faceData.Update(); myTrackedPerson.FacesDetected = faceData.QueryNumberOfDetectedFaces(); if (myTrackedPerson.FacesDetected == 1) { PXCMFaceData.Face face = faceData.QueryFaceByIndex(0); PXCMFaceData.DetectionData faceDetectionData = face.QueryDetection(); PXCMRectI32 faceRectangle; faceDetectionData.QueryBoundingRect(out faceRectangle); myTrackedPerson.FaceH = faceRectangle.h; myTrackedPerson.FaceW = faceRectangle.w; myTrackedPerson.FaceX = faceRectangle.x; myTrackedPerson.FaceY = faceRectangle.y; float faceDepth; faceDetectionData.QueryFaceAverageDepth(out faceDepth); myTrackedPerson.FaceDepth = faceDepth; } blobData.Update(); int numBlobs = blobData.QueryNumberOfBlobs(); myBlobs.numBlobs = numBlobs; myBlobs.blobs = new List <List <PXCMPointI32> >(numBlobs); myBlobs.closestPoints = new List <PXCMPoint3DF32>(4); for (int i = 0; i < numBlobs; i++) { PXCMBlobData.IBlob blob; pxcmStatus result1 = blobData.QueryBlob(i, PXCMBlobData.SegmentationImageType.SEGMENTATION_IMAGE_DEPTH, PXCMBlobData.AccessOrderType.ACCESS_ORDER_NEAR_TO_FAR, out blob); if (result1 == pxcmStatus.PXCM_STATUS_NO_ERROR) { PXCMPoint3DF32 closestPoint = blob.QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_CLOSEST); myBlobs.closestPoints.Add(closestPoint); int numContours = blob.QueryNumberOfContours(); if (numContours > 0) { // only deal with outer contour for (int j = 0; j < numContours; j++) { PXCMBlobData.IContour contour; pxcmStatus result2 = blob.QueryContour(j, out contour); if (result2 == pxcmStatus.PXCM_STATUS_NO_ERROR) { if (contour.IsOuter()) { PXCMPointI32[] points; pxcmStatus result3 = contour.QueryPoints(out points); if (result3 == pxcmStatus.PXCM_STATUS_NO_ERROR) { int numPoints = points.Length; myBlobs.blobs.Add(points.ToList <PXCMPointI32>()); } } } } } } } // Update UI Render(colorBitmap, myTrackedPerson, myBlobs); // serialize to json and send all clients var personJson = JsonConvert.SerializeObject(myTrackedPerson); personSockets.ToList().ForEach(s => s.Send(personJson)); var blobJson = JsonConvert.SerializeObject(myBlobs); blobSockets.ToList().ForEach(s => s.Send(blobJson)); // deserialize json as follows //MyTrackedPerson deserializedProduct = JsonConvert.DeserializeObject<MyTrackedPerson>(json); // Release resources colorBitmap.Dispose(); sample.color.ReleaseAccess(colorData); sm.ReleaseFrame(); } }