Пример #1
0
        private void AllFrameReady(object sender, AllFramesReadyEventArgs e)
        {
            //Console.Clear();
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(this.skeletons);
                    Skeleton skel = skeletons[0];
                    if (skel.TrackingState == SkeletonTrackingState.Tracked)
                    {
                        SkeletonPoint rightHand = skeletons[0].Joints[JointType.HandRight].Position;
                        SkeletonPoint head      = skeletons[0].Joints[JointType.Head].Position;
                        rightHandPosition = SkeletonPointToScreen(rightHand);
                        headPosition      = SkeletonPointToScreen(head);
                    }
                }
            }

            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.ColorWriteBitmap.WritePixels(
                        new System.Windows.Int32Rect(0, 0, this.ColorWriteBitmap.PixelWidth, this.ColorWriteBitmap.PixelHeight),
                        this.colorPixels,
                        this.ColorWriteBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }

            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    var sw = Stopwatch.StartNew();
                    // Copy the pixel data from the image to a temporary array
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                    // short[] depthData = new short[depthFrame.PixelDataLength];
                    // Get the min and max reliable depth for the current frame
                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;
                    int width    = depthFrame.Width;
                    int height   = depthFrame.Height;

                    if (headPosition.X == 0)
                    {
                        headDepth = 100;
                    }
                    else
                    {
                        headDepth = depthPixels[headPosition.X + headPosition.Y * 640].Depth;
                    }

                    sw.Restart();
                    //*********** Convert cull and transform*****************
                    colorizer.TransformCullAndConvertDepthFrame(
                        depthPixels, minDepth, maxDepth, colorPixels,
                        AngleRotateTan,
                        (short)(headDepth - (short)CullingThresh), headPosition);

                    Image <Bgra, byte> depthImg;
                    //Console.WriteLine("iteration:" + sw.ElapsedMilliseconds);
                    sw.Restart();


                    Image <Gray, Byte> rightFront = null;
                    Image <Gray, Byte> leftFront  = null;
                    depthImg = ImageConverter.Array2Image(colorPixels, width, height, width * 4);
                    PointF rightVector   = PointF.Empty;
                    PointF leftVector    = PointF.Empty;
                    bool   isSkip        = false;
                    bool   leftHandRaise = false;
                    if (skeletons != null && skeletons[0].TrackingState == SkeletonTrackingState.Tracked)
                    {
                        PointF hr  = SkeletonPointToScreen(skeletons[0].Joints[JointType.HandRight].Position);
                        PointF hl  = SkeletonPointToScreen(skeletons[0].Joints[JointType.HandLeft].Position);
                        PointF er  = SkeletonPointToScreen(skeletons[0].Joints[JointType.ElbowRight].Position);
                        PointF el  = SkeletonPointToScreen(skeletons[0].Joints[JointType.ElbowLeft].Position);
                        PointF hip = SkeletonPointToScreen(skeletons[0].Joints[JointType.HipCenter].Position);
                        // hand is lower than hip
                        //Console.WriteLine(skeletons[0].Joints[JointType.HandRight].Position.Y);
                        //Console.WriteLine(skeletons[0].Joints[JointType.HipCenter].Position.Y);
                        //Console.WriteLine("-------------");
                        if (skeletons[0].Joints[JointType.HandRight].Position.Y <
                            skeletons[0].Joints[JointType.HipCenter].Position.Y + 0.05)
                        {
                            isSkip = true;
                        }
                        if (skeletons[0].Joints[JointType.HandLeft].Position.Y >
                            skeletons[0].Joints[JointType.HipCenter].Position.Y)
                        {
                            leftHandRaise = true;
                        }

                        rightVector.X = (hr.X - er.X);
                        rightVector.Y = (hr.Y - er.Y);
                        leftVector.X  = (hl.X - el.X);
                        leftVector.Y  = (hl.Y - el.Y);
                    }
                    HandShapeModel handModel = null;
                    if (!isSkip)
                    {
                        //handModel = m_OpenCVController.FindHandPart(ref depthImg, out rightFront, out leftFront, headDepth - (int)CullingThresh, rightVector, leftVector,leftHandRaise);
                    }



                    // no hands detected
                    if (handModel == null)
                    {
                        handModel = new HandShapeModel(0, HandEnum.None);
                    }
                    //sw.Restart();

                    // database processing
                    DBManager db = DBManager.GetSingleton();
                    if (db != null)
                    {
                        if (skeletons != null)
                        {
                            handModel.SetSkeletonData(skeletons[0]);
                        }
                        db.AddFrameData(handModel);
                    }
                    // not recording show prob
                    else
                    {
                        //Image<Bgr, byte>[] result = HandShapeClassifier.GetSingleton()
                        //.RecognizeGesture(handModel.hogRight, 3);
                        ////Console.WriteLine(sw.ElapsedMilliseconds);
                        //if (result != null)
                        //{
                        //    ImageConverter.UpdateWriteBMP(WrtBMP_Candidate1, result[0].Convert<Gray, byte>().ToBitmap());
                        //    ImageConverter.UpdateWriteBMP(WrtBMP_Candidate2, result[1].Convert<Gray, byte>().ToBitmap());
                        //    ImageConverter.UpdateWriteBMP(WrtBMP_Candidate3, result[2].Convert<Gray, byte>().ToBitmap());
                        //}
                    }
                    string currentSign = db == null ? "0" : db.CurrentSign.ToString();
                    string path        = @"J:\Kinect data\Aaron 141-180\hands\" + currentSign + " " + handModel.frame.ToString();
                    // UI update
                    if (rightFront != null)
                    {
                        Bitmap right = rightFront.ToBitmap();
                        //right.Save(path + " r.jpg");
                        ImageConverter.UpdateWriteBMP(WrtBMP_RightHandFront, right);
                    }
                    if (leftFront != null)
                    {
                        Bitmap left = leftFront.ToBitmap();
                        //left.Save(path + " l.jpg");
                        ImageConverter.UpdateWriteBMP(WrtBMP_LeftHandFront, left);
                    }
                    if (sw.ElapsedMilliseconds > 15)
                    {
                        Console.WriteLine("Find hand:" + sw.ElapsedMilliseconds);
                    }
                    sw.Restart();

                    //**************************draw gray histogram
                    //Bitmap histo = m_OpenCVController.Histogram(depthImg);
                    //ImageConverter.UpdateWriteBMP(GrayWriteBitmap, histo);

                    //  draw hand position from kinect
                    // DrawHandPosition(depthBMP, rightHandPosition, System.Drawing.Brushes.Purple);

                    //*******************upadte UI
                    ImageConverter.UpdateWriteBMP(DepthWriteBitmap, depthImg.ToBitmap());
                    // Console.WriteLine("Update UI:" + sw.ElapsedMilliseconds);
                }
            }
        }
        private void MenuItem_Test_Click(object sender, RoutedEventArgs e)
        {
            #region mog txt to database
            StreamReader sr = new StreamReader(File.Open(@"J:\Kinect data\mog141-180.txt", FileMode.Open));
            string dataPath = @"J:\Kinect data\database141-181.db";
            m_DBmanager = DBManager.GetSingleton(dataPath);
            m_DBmanager.BeginTrans();

            string line = sr.ReadLine();
            int count = 1;
            while (line != null && line != "")
            {
                string[] cell = line.Split();
                int frame = Convert.ToInt32(cell[1]);
                bool isRight = cell[2] == "r";
                if (cell.Count() >= 27)
                {
                    float[] Mog = cell.Skip(3).Take(24).Select(x => Convert.ToSingle(x)).ToArray();
                    m_DBmanager.UpdateMogData(frame, Mog, isRight);
                }
                Console.WriteLine(count++);
                line = sr.ReadLine();
            }
            m_DBmanager.Commit();
            m_DBmanager.Close();
            sr.Close();
            #endregion

            #region kmeans

            #endregion
        }
        public static DBManager GetSingleton(string dataSource)
        {
            if (singleton == null || singleton.DataSource != dataSource)
            {
                singleton = new DBManager(dataSource);
                return singleton;

            }
            return singleton;
        }