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); } }
/// <summary> /// Constructor /// </summary> /// <param name="context">compute context</param> /// <param name="useBinaryCache">use cache for binaries</param> public static new ClooProgramViolaJones Create(ClooContext context, bool useBinaryCache = false) { var dict = GetEmbeddedSourceLines(typeof(ClooProgramViolaJones)); 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()); ClooProgramViolaJones 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 ClooProgramViolaJones(context, binaries, context.Devices); } } #if DEBUG try { #endif if (res == null) { // ok not in cache, so rebuilt from source res = new ClooProgramViolaJones(context, new string[] { source }); 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(ClooProgramViolaJones)); return res; }