Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="context">compute context</param>
        /// <param name="useBinaryCache">use cache for binaries</param>
        public static ClooProgramCore Create(ClooContext context, bool useBinaryCache = false)
        {
            var dict = GetEmbeddedSourceLines(typeof(ClooProgramCore));
            string header = null;
            if (dict.TryGetValue("Header", out header)) dict.Remove("Header"); else header = "";
            List<string> lines = dict.Values.ToList();
            string source = header + "\r\n" + String.Join("\r\n\r\n", lines.ToArray());

            ClooProgramCore res = null;
            if (useBinaryCache)
            {
                // check if we already have binaries
                int hashCode = lines.GetArrayHashCode();
                foreach (ClooDevice device in context.Devices)
                    hashCode ^= device.GetHashCode();

                IList<byte[]> binaries;
                if (ClooProgram.BinariesCache.TryGetValue(hashCode, out binaries))
                {
                    // create from cached binaries
                    res = new ClooProgramCore(context, binaries, context.Devices);
                }
            }

            #if DEBUG
            try
            {
            #endif
                if (res == null)
                {
                    // ok not in cache, so rebuilt from source
                    res = new ClooProgramCore(context, lines.ToArray());
                    res.Build(context.Devices, null, useBinaryCache);
                }
            #if DEBUG
            }
            catch (Exception exception)
            {
                StringBuilder sb = new StringBuilder();
                foreach (ComputeDevice device in context.Devices)
                {
                    string info = "DEVICE: " + device.Name;
                    sb.AppendLine(info);

                    StringReader reader = new StringReader(res.GetBuildLog(device));
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        sb.AppendLine(line);
                        line = reader.ReadLine();
                    }

                    sb.AppendLine("");
                    sb.AppendLine(exception.Message);
                }
                throw new ApplicationException(sb.ToString());
            }
            #endif
            res.InitializeKernels(typeof(ClooProgramCore));
            return res;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="context">compute context</param>
 /// <param name="lines">lines of source code</param>
 protected ClooProgramCore(ClooContext context, IList<byte[]> binaries, IEnumerable<ClooDevice> devices)
     : base(context, binaries, devices)
 {
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="context">compute context</param>
 /// <param name="lines">lines of source code</param>
 protected ClooProgramCore(ClooContext context, string[] lines)
     : base(context, lines)
 {
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="context">compute context</param>
 /// <param name="lines">lines of source code</param>
 protected ClooProgramViolaJones(ClooContext context, string[] lines)
     : base(context, lines)
 {
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="context">Cloo compute context</param>
 /// <param name="normalizedCoords">are coordinates normalized? (between 0 and 1)</param>
 /// <param name="addressing">image addressing mode</param>
 /// <param name="filtering">image filtering mode</param>
 public ClooSampler(ClooContext context, bool normalizedCoords, ComputeImageAddressing addressing, ComputeImageFiltering filtering)
     : base(context, normalizedCoords, addressing, filtering)
 {
     _context = context;
 }