/// <summary>
        /// Creates a face detector for GPU processing
        /// </summary>
        /// <param name="fileName">XML file containing Haar cascade stages (in OpenCL format)</param>
        /// <param name="context">Cloo context</param>
        /// <param name="queue">Cloo command queue</param>
        /// <param name="imageWidth">image width</param>
        /// <param name="imageHeight">image height</param>
        /// <returns>HaarObjectDetector</returns>
        public static ClooHaarObjectDetector CreateDetectorFromFile(string fileName, ClooContext context, ClooCommandQueue queue, int imageWidth, int imageHeight)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (queue == null) throw new ArgumentNullException("queue");

            ClooHaarObjectDetector res = new ClooHaarObjectDetector();
            res._clooProgram = ClooProgramViolaJones.Create(context);
            res.Context = context;
            res.Queue = queue;

            res._integralImage = ClooImage2DUIntA.Create(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, imageWidth, imageHeight);
            res._integral2Image = ClooImage2DUIntA.Create(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, imageWidth, imageHeight);
            res._resultRectangles = new ClooBuffer<Rectangle>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, (long)MaxResultRectangles);

            var tuple = LoadFromXml(fileName);
            res.WindowWidth = tuple.WindowSize.Width;
            res.WindowHeight = tuple.WindowSize.Height;
            res._stageNodes = new ClooBuffer<HaarFeatureNode>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, tuple.StageNodes.ToArray());
            res._stageNodeCounts = new ClooBuffer<int>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, tuple.StageNodesCount.ToArray());
            res._stageThresholds = new ClooBuffer<float>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, tuple.StageThresholds.ToArray());
            res._totalNodesCount = tuple.StageNodes.Count;
            res._stageCount = tuple.StageNodesCount.Count;
            return res;
        }
Пример #2
0
        void initializeDevice()
        {
            try
            {
                _selectedDevice = comboBoxDevices.SelectedItem as ClooDevice;
                if (_context != null)
                {
                    _startProcessing = false;

                    // dispose previous context
                    _context.Dispose();
                    _kernels = null;
                    _context = null;
                    _sampler = null;
                    _queue = null;

                    image2.Source = null;
                    image3.Source = null;
                    image4.Source = null;
                }
                if (_selectedDevice != null)
                {
                    // create context
                    _context = _selectedDevice.CreateContext();
                    _queue = _context.CreateCommandQueue();
                    _sampler = new ClooSampler(_context, false, ComputeImageAddressing.ClampToEdge, ComputeImageFiltering.Linear);
                    _kernels = ClooProgramViolaJones.Create(_context);

                    _haarObjectDetector = ClooHaarObjectDetector.CreateFaceDetector(_context, _queue, 640, 480);
                    _haarObjectDetector.ScalingFactor = 1.25f;
                    _haarObjectDetector.ScalingMode = ScalingMode.SmallerToLarger;
                    _haarObjectDetector.MinSize = new System.Drawing.Size(30, 30);
                    _haarObjectDetector.MaxSize = new System.Drawing.Size(100, 100);

                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Reset();
                    stopwatch.Start();
                    _histogram = new ClooBuffer<uint>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 32 * 32 * 32);
                    _histogram2 = new ClooBuffer<uint>(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 32 * 32 * 32);
                    _clooImageByteOriginal = ClooImage2DByteRgbA.CreateFromBitmap(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1);
                    _clooImageByteOriginal.WriteToDevice(_queue);
                    _clooImageByteGrayOriginal = ClooImage2DByteA.CreateFromBitmap(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1);
                    _clooImageByteGrayOriginal.WriteToDevice(_queue);
                    _clooImageByteResult = ClooImage2DByteRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageByteResultA = ClooImage2DByteA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatOriginal = ClooImage2DFloatRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatGrayOriginal = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatTemp1 = ClooImage2DFloatRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatTemp2 = ClooImage2DFloatRgbA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatATemp1 = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatATemp2 = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width, _bitmapImage1.Height);
                    _clooImageFloatIntegral = ClooImage2DFloatA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width + 1, _bitmapImage1.Height + 1);
                    _clooImageUIntIntegral = ClooImage2DUIntA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width + 1, _bitmapImage1.Height + 1);
                    _clooImageUIntIntegralSquare = ClooImage2DUIntA.Create(_context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _bitmapImage1.Width + 1, _bitmapImage1.Height + 1);
                    _queue.Finish();
                    label1.Content = stopwatch.ElapsedMilliseconds + " ms - original " + _bitmapImage1.Width + "x" + _bitmapImage1.Height;

                    _startProcessing = true;

                    Update();
                    Update();
                }
            }
            catch (Exception ex)
            {
                // show exception
                MessageBox.Show(ex.Message, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
            }
        }
Пример #3
0
        /// <summary>
        /// Sets a value of all cells in a buffer
        /// </summary>
        /// <param name="queue">cloo command queue</param>
        /// <param name="source">image source</param>
        /// <param name="value">value to set</param>
        public void SetValue(ClooCommandQueue queue, ClooBuffer<uint> source, uint value)
        {
            if (queue == null) throw new ArgumentNullException("queue");
            if (source == null) throw new ArgumentNullException("source");

            KernelCoreUIntSetValue.SetArguments(source, value);
            queue.Execute(KernelCoreUIntSetValue, null, new long[] { source.HostBuffer.Length }, null, null);
            source.Modified = true;
        }
Пример #4
0
        public void ProcessViolaJonesFrame(ClooCommandQueue queue, ClooBuffer<HaarFeatureNode> nodes,
            int stagesCount, IBuffer<int> stageNodeCounts, IBuffer<float> stageThresholds,
            ClooImage2DUIntA integral, ClooImage2DUIntA integralSquare,
            IBuffer<Rectangle> resRectangles,
            float scale, int countX, int countY,
            int stepX, int stepY, int windowWidth, int windowHeight)
        {
            if (queue == null) throw new ArgumentNullException("queue");
            if (nodes == null) throw new ArgumentNullException("nodes");
            if (integral == null) throw new ArgumentNullException("integral");
            if (integralSquare == null) throw new ArgumentNullException("integralSquare");
            if (resRectangles == null) throw new ArgumentNullException("resRectangles");

            KernelImageViolaJonesProcess.SetArguments(nodes, stagesCount, stageNodeCounts, stageThresholds,
                integral, integralSquare, resRectangles, resRectangles.HostBuffer.Length, scale, stepX, stepY,
                windowWidth, windowHeight);
            queue.Execute(KernelImageViolaJonesProcess, null, new long[] { countX, countY }, null, null);
        }
Пример #5
0
        public void ClearViolaJonesRectangles(ClooCommandQueue queue, ClooBuffer<Rectangle> resRectangles)
        {
            if (queue == null) throw new ArgumentNullException("queue");
            if (resRectangles == null) throw new ArgumentNullException("resRectangles");

            KernelImageViolaJonesClear.SetArguments(resRectangles);
            queue.Execute(KernelImageViolaJonesClear, null, new long[] { resRectangles.HostBuffer.Length }, null, null);
        }