Exemple #1
0
        public void PopulateBodies(List <Body> bodies, CoordinateMapper mapper, Mode mode)
        {
            //List<GestureDetector> gestureDetectorList = new List<GestureDetector>();

            // create gesture detector for each body
            int bodyCount = bodies.Count;

            //for (int i = 0; i < bodyCount; ++i)
            //{
            //    GestureResult result = new GestureResult(i, false, false, 0.0f);
            //    String appCurrentPath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            //    String database = Path.Combine(appCurrentPath,"Database\\WaveHand.gbd");
            //    //Console.WriteLine(database);
            //    GestureDetector detector = new GestureDetector(sensor, result, database, "Wave_Left");
            //    gestureDetectorList.Add(detector);
            //}
            jsonSkeletons.command = "bodyData";

            for (int i = 0; i < bodyCount; ++i)
            {
                JSONBody jsonBody = new JSONBody();
                if (bodies[i].IsTracked)
                {
                    ulong trackingId = bodies[i].TrackingId;

                    //if (trackingId != gestureDetectorList[i].TrackingId)
                    //{
                    //    gestureDetectorList[i].TrackingId = trackingId;
                    //    gestureDetectorList[i].IsPaused = (trackingId == 0);
                    //}

                    jsonBody.trackingID     = bodies[i].TrackingId.ToString();
                    jsonBody.Joints         = new List <JSONJoint>();
                    jsonBody.HandLeftState  = bodies[i].HandLeftState;
                    jsonBody.HandRightState = bodies[i].HandRightState;
                    jsonBody.Gestures       = new List <JSONGesture>();

                    //if (gestureDetectorList[i].GestureResult.Detected)
                    //{
                    //    jsonSkeleton.Gestures.Add(new JSONGesture
                    //    {
                    //        Name = gestureDetectorList[i].gestureName,
                    //        Confidence = gestureDetectorList[i].GestureResult.Confidence
                    //    });
                    //}

                    foreach (var joint in bodies[i].Joints)
                    {
                        Point point = new Point();
                        switch (mode)
                        {
                        case Mode.Color:
                            ColorSpacePoint colorPoint = mapper.MapCameraPointToColorSpace(joint.Value.Position);
                            point.X = colorPoint.X;
                            point.Y = colorPoint.Y;
                            break;

                        case Mode.Depth:
                            DepthSpacePoint depthPoint = mapper.MapCameraPointToDepthSpace(joint.Value.Position);
                            point.X = depthPoint.X;
                            point.Y = depthPoint.Y;
                            break;

                        default:
                            break;
                        }

                        jsonBody.Joints.Add(new JSONJoint
                        {
                            Name    = joint.Key.ToString().ToLower(),
                            X       = joint.Value.Position.X,
                            Y       = joint.Value.Position.Y,
                            mappedX = Double.IsInfinity(point.X) ? -1 : point.X,
                            mappedY = Double.IsInfinity(point.X) ? -1 : point.Y,
                            Z       = joint.Value.Position.Z
                        });
                    }
                    jsonSkeletons.Bodies.Add(jsonBody);
                }
            }
        }
        void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            bool hasGesture = false;

            var reference = e.FrameReference.AcquireFrame();

            bool dataReceived = false;

            // Body
            using (var frame = reference.BodyFrameReference.AcquireFrame())
            {
                if (frame != null)
                {
                    int bodyCount = frame.BodyFrameSource.BodyCount;
                    _skeletonCollection = new SkeletonsDataCollection();
                    _skeletonList       = new Skeleton[bodyCount];

                    _bodies = new Body[frame.BodyFrameSource.BodyCount];

                    frame.GetAndRefreshBodyData(_bodies);
                    dataReceived = true;

                    List <Body> _bodyList = _bodies.ToList <Body>();

                    JSONBodySerialize jsonBS = new JSONBodySerialize();

                    jsonBS.PopulateBodies(_bodyList, _sensor.CoordinateMapper, Mode.Color);

                    if (isReceiving)
                    {
                        lock (serverLock)
                        {
                            if (jsonBS.jsonSkeletons.Bodies.Count > 0)
                            {
                                for (int i = 0; i < bodyCount; i++)
                                {
                                    foreach (var gesture in gestureDetectorList[i].GestureResults)
                                    {
                                        if (gesture.Detected)
                                        {
                                            ulong trackingId = gestureDetectorList[i].TrackingId;
                                            hasGesture = true;
                                            JSONBody jsBD = jsonBS.jsonSkeletons.Bodies.FirstOrDefault(n => n.trackingID == trackingId.ToString());
                                            jsBD.Gestures.Add(new JSONGesture
                                            {
                                                Name       = gesture.Name,
                                                Confidence = gesture.Confidence
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }

                    string _bodiesJSON = jsonBS.Serialize();
                    //string _bodiesJSON = _bodyList.Serialize(_sensor, _sensor.CoordinateMapper, Mode.Color);

                    if (hasGesture)
                    {
                        File.WriteAllBytes("body_byte.txt", Encoding.UTF8.GetBytes(_bodiesJSON));
                    }

                    if (isSendBodyData)
                    {
                        _skeletonCollection.BodyCount    = bodyCount;
                        _skeletonCollection.SkeletonList = _skeletonList;

                        //byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(_skeletonCollection));
                        byte[] bytes     = Encoding.UTF8.GetBytes(_bodiesJSON);
                        byte[] nullBytes = new byte[] { 0, 0, 0, 0 };
                        byte[] bodyDataDirectiveBytes;

                        bodyDataDirectiveBytes = Encoding.UTF8.GetBytes("+BDD");
                        //bytes = ms.ToArray();

                        byte[] sentData = new byte[bodyDataDirectiveBytes.Length + bytes.Length + nullBytes.Length];
                        //bytesSize.CopyTo(sentData, 0);
                        bodyDataDirectiveBytes.CopyTo(sentData, 0);
                        bytes.CopyTo(sentData, 4);
                        nullBytes.CopyTo(sentData, sentData.Length - 4);

                        //File.WriteAllBytes("body_byte.txt", sentData);

                        handlerSocket = connections[0].socket;

                        if (handlerSocket.Connected)
                        {
                            try
                            {
                                lock (serverLock)
                                {
                                    WriteDebug("Send Body data begins");
                                    //Console.WriteLine(DEBUG_SYMBOL + "Send Body data begins");
                                    handlerSocket.BeginSend(sentData, 0, sentData.Length, 0,
                                                            new AsyncCallback(SendBodyDataCallback), handlerSocket);
                                }
                            }

                            catch (SocketException se)
                            {
                                Console.WriteLine(ERROR_SYMBOL + "Socket exception: " + se.SocketErrorCode);
                                Console.WriteLine(se);
                            }
                        }
                    }
                }
            }

            if (dataReceived)
            {
                if (hasDetector)
                {
                    // we may have lost/acquired bodies, so update the corresponding gesture detectors
                    if (_bodies != null)
                    {
                        // loop through all bodies to see if any of the gesture detectors need to be updated
                        int maxBodies = _sensor.BodyFrameSource.BodyCount;
                        for (int i = 0; i < maxBodies; ++i)
                        {
                            Body  body       = _bodies[i];
                            ulong trackingId = body.TrackingId;

                            //WriteDebug("BodyIndex = " + i + " | TrackingId = " + trackingId + gestureDetectorList[i]);
                            Console.WriteLine("BodyIndex = " + i + " | BodyTrackingId = " + trackingId + "\n GestureTrackingId = " + gestureDetectorList[i].TrackingId + gestureDetectorList[i]);
                            // if the current body TrackingId changed, update the corresponding gesture detector with the new value
                            if (trackingId != gestureDetectorList[i].TrackingId)
                            {
                                gestureDetectorList[i].TrackingId = trackingId;
                                // if the current body is tracked, unpause its detector to get VisualGestureBuilderFrameArrived events
                                // if the current body is not tracked, pause its detector so we don't waste resources trying to get invalid gesture results
                                gestureDetectorList[i].IsPaused = (trackingId == 0);
                            }
                        }
                    }

                    foreach (var gestureDetector in gestureDetectorList)
                    {
                        if (gestureDetector.TrackingId == 0)
                        {
                            foreach (var gesture in gestureDetector.GestureResults)
                            {
                                gesture.UpdateGestureResult(gesture.Name, false, false, 0.0f);
                            }
                        }
                    }
                }
            }
        }