Esempio n. 1
0
        private async Task RepaintAsync(TimeSpan dueTime, TimeSpan interval, CancellationToken token)
        {
            // Initial wait time before we begin the periodic loop.
            if (dueTime > TimeSpan.Zero)
            {
                await Task.Delay(dueTime, token);
            }

            int ColorW = Sensor.ColorW;
            int ColorH = Sensor.ColorH;

            // Repeat this loop until cancelled.
            while (!token.IsCancellationRequested)
            {
                // Timestamp data
                RepaintWatch.Again();

                // Do Job
                try {
                    // Draw Profile
                    DrawProfile();

                    // Draw Metrics
                    DrawMetrics();

                    if (!WSRConfig.GetInstance().SpeechOnly)
                    {
                        // Image color
                        Sensor.CopyColorData = true;
                        Bitmap            bitmap = WSRKinectSensor.ToBitmap(Sensor.ColorData, ColorW, ColorH, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                        Image <Bgr, Byte> image  = new Image <Bgr, Byte>(bitmap);

                        DrawFaces(image);
                        DrawJoints(image);
                        DrawFace(image);
                        DrawDepth(image);

                        this.Image.Source = ToBitmapSource(image);

                        // Prominent Color
                        DrawProminentColor();
                    }
                }
                catch (Exception ex) {
                    WSRConfig.GetInstance().logError("CAMERA", ex);
                }
                RepaintWatch.Stop();

                // Wait to repeat again.
                if (interval > TimeSpan.Zero)
                {
                    await Task.Delay(interval, token);
                }
            }
        }
Esempio n. 2
0
        public void Detect(byte[] pixels, int width, int height)
        {
            // Build Image
            Bitmap            bitmap = WSRKinectSensor.ToBitmap(pixels, width, height, PixelFormat.Format32bppRgb);
            Image <Bgr, Byte> color  = new Image <Bgr, Byte>(bitmap);

            // Convert it to Grayscale
            Gray = color.Convert <Gray, Byte>();

            // Detect faces
            Faces = Gray.DetectHaarCascade(haarCascade, 1.2, 2, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(80, 80))[0];

            // Train if needed
            Train();
        }
Esempio n. 3
0
        public Bitmap GreenScreen(String message)
        {
            // 1. Retrieve Sensor
            WSRKinectSensor Sensor = ((WSRKinect)WSRConfig.GetInstance().WSR).ActiveSensor();

            Sensor.CopyDepthPixels = true;
            Sensor.CopyColorData   = true;

            // 2. Allocate data once
            if (null == mask)
            {
                depthW = Sensor.DepthW;
                depthH = Sensor.DepthH;
                colorW = Sensor.ColorW;
                colorH = Sensor.ColorH;
                depthL = Sensor.DepthPixels.Length;

                // Compute divisor
                colorToDepthDivisor = colorW / depthW;

                // Allocate space to put color pixel data
                colorCoordinates = new ColorImagePoint[depthL];

                // Allocate space to put the green pixels
                mask = new Image <Gray, Byte>(depthW, depthH);
            }

            // 3. Smooth DepthPixels
            this.depthPixels = Sensor.DepthPixels;
            if (null != filter1)
            {
                this.depthPixels = this.filter1.CreateFilteredDepthArray(this.depthPixels, depthW, depthH);
            }
            if (null != filter2)
            {
                this.depthPixels = this.filter2.CreateAverageDepthArray(this.depthPixels, depthW, depthH);
            }

            // 4. Map Depth to Sensor
            Sensor.Sensor.CoordinateMapper.MapDepthFrameToColorFrame(
                Sensor.DepthFormat, depthPixels,
                Sensor.ColorFormat, colorCoordinates);

            // 5. CleanUp
            mask.SetZero();

            // 6. Loop over each row and column of the depth
            for (int y = 0; y < depthH; ++y)
            {
                for (int x = 0; x < depthW; ++x)
                {
                    // calculate index into depth array
                    int depthIndex = x + (y * depthW);

                    // if we're tracking a player for the current pixel, do green screen
                    DepthImagePixel depthPixel = depthPixels[depthIndex];
                    int             player     = depthPixel.PlayerIndex;
                    if (player <= 0)
                    {
                        continue;
                    }

                    // retrieve the depth to color mapping for the current depth pixel
                    ColorImagePoint colorImagePoint = colorCoordinates[depthIndex];

                    // scale color coordinates to depth resolution
                    int colorInDepthX = colorImagePoint.X / colorToDepthDivisor;
                    int colorInDepthY = colorImagePoint.Y / colorToDepthDivisor;

                    // make sure the depth pixel maps to a valid point in color space
                    // check y > 0 and y < depthHeight to make sure we don't write outside of the array
                    // check x > 0 instead of >= 0 since to fill gaps we set opaque current pixel plus the one to the left
                    // because of how the sensor works it is more correct to do it this way than to set to the right
                    if (colorInDepthX > 0 && colorInDepthX < depthW && colorInDepthY >= 0 && colorInDepthY < depthH)
                    {
                        // calculate index into the green screen pixel array
                        // int greenScreenIndex = colorInDepthX + (colorInDepthY * depthW);

                        // set opaque
                        mask.Data[colorInDepthY, colorInDepthX, 0] = 1;

                        // compensate for depth/color not corresponding exactly by setting the pixel
                        // to the left to opaque as well
                        mask.Data[colorInDepthY, colorInDepthX - 1, 0] = 1;
                    }
                }
            }

            Bitmap             bitmap = WSRKinectSensor.ToBitmap(Sensor.ColorData, colorW, colorH, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            Image <Bgra, Byte> color  = new Image <Bgra, Byte>(bitmap);

            color = color.Resize(depthW, depthH, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            color = color.Copy(mask);

            return(color.Bitmap);
        }