private void NewLiveFrameHandler(byte[] frame, IntPtr frameGPU, int width, int height, int cameraID, System.Drawing.Imaging.PixelFormat pf, Exception exceptionState) { // OK it's in here // SOmehting in here causing it Stopwatch sw = new Stopwatch(); sw.Start(); CameraParams cp; if (vm.ActiveDictionary.TryGetValue(cameraID, out cp)) { if (cp.pSurface != IntPtr.Zero) { try { // if the size of the image has changed, need to resize the DirectX surface if (width != cp.Width || height != cp.Height) { ConfigImageD3DSurface(cameraID, (uint)width, (uint)height, false); cp.Width = width; cp.Height = height; } cp.d3dImage.Lock(); cp.d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, cp.pSurface); // copy GPU array (output of decoder) onto the IDirect3DSurface9 //CudaTools.DX_GpuCopyImageToSurface(m_cudaUtil, cameraID, frameGPU); cp.d3dImage.AddDirtyRect(new Int32Rect(0, 0, width, height)); cp.d3dImage.Unlock(); } catch (Exception e) { //Debug.Print("Error: " + e.Message); } } } sw.Stop(); long t = sw.ElapsedMilliseconds; UInt64 totalMem = 0; UInt64 freeMem = 0; int w = cp.Width; int h = cp.Height; CudaTools.Cuda_GetDeviceMemory64(m_cudaUtil, out totalMem, out freeMem); if (freeMem < 1000000) { Debug.Print("Out of GPU Memory"); } //Debug.Print("Display Time: " + t.ToString()); }
public void ConfigImageD3DSurface(int ID, uint pixelWidth, uint pixelHeight, bool useAlphaChannel) { // ID = unique id for this display panel in the surface array. It might be an ID for the camera, or an ID for an experiment indicator // pixelWidth, pixelHeight = the size of the display panel in pixels. This should match the size of the image to be displaye on it. // panelHeader = string that is displayed above the panel // useAlphaChannel = sets whether to use the alpha channel or not (usually false) bool success; try { CameraParams cp; if (ID == vm.TestCamera.ID) { cp = vm.TestCamera; } else if (ID == vm.RefCamera.ID) { cp = vm.RefCamera; } else { return; } // if there's a surface already at this position, remove it, since we're about to create a new one success = CudaTools.DX_GpuRemoveD3DSurface(m_cudaUtil, ID); // create new surface, and use the Invoke command to make sure it runs on UI thread since there's some UI-dependent code in here success = m_surfArray.AssignCameraToPosition(cp.DisplayRow, cp.DisplayCol, (uint)ID, pixelWidth, pixelHeight, cp.camera.CameraName, false); int surfaceIndex = m_surfArray.GetSurfaceIndex(cp.DisplayRow, cp.DisplayCol); // get pSurface and d3dImage IntPtr pSurface = IntPtr.Zero; uint uWidth; uint uHeight; bool useAlpha; m_surfArray.GetSurface_Params(surfaceIndex, out pSurface, out uWidth, out uHeight, out useAlpha); success = CudaTools.DX_GpuAddD3DSurface(m_cudaUtil, ID, pSurface, (int)uWidth, (int)uHeight); cp.Width = (int)uWidth; cp.Height = (int)uHeight; cp.d3dImage = m_surfArray.GetD3DImage(cp.DisplayRow, cp.DisplayCol); cp.pSurface = pSurface; cp.SurfaceIndex = surfaceIndex; } catch (Exception e) { Debug.Print(e.Message); } }
private void QuitPB_Click(object sender, RoutedEventArgs e) { m_serverController.Shutdown(); // this doesn't do anything yet, but needs to!! if (m_surfArray != null) { m_surfArray.Shutdown(); } CudaTools.Cuda_Free64(m_cudaUtil); Close(); }
public MainWindow1() { m_defaultHeight = 16; // TODO: these can't be zero for now, since if they're zero, no D3D9 surface will be created. Can fix this later. m_defaultWidth = 16; m_count = 0; m_captureRefImage = false; InitializeComponent(); vm = new ViewModel(); this.DataContext = vm; // Set default Server //vm.ServerIP = "www.demovi.com"; //vm.ServerDataPort = "4020"; vm.ServerIP = "10.0.0.219"; vm.ServerDataPort = "4010"; vm.Username = "******"; vm.Password = ""; bool success; m_cudaUtil = CudaTools.Cuda_Create64(); success = CudaTools.Cuda_GetContext64(m_cudaUtil, out m_cudaContext); byte[] deviceName = new byte[100]; CudaTools.Cuda_GetDeviceName64(m_cudaUtil, deviceName); ulong totMem = 0; ulong freeMem = 0; CudaTools.Cuda_GetDeviceMemory64(m_cudaUtil, out totMem, out freeMem); m_serverController = new VIServerController(m_cudaUtil); m_serverController.Connected += ServerController_Connected; VIClient.OnNewLiveFrame frameHandler = NewLiveFrameHandler; m_serverController.SetNewLiveFrameCallback(frameHandler); m_detector = new Detect_net.Detector(); m_detector.InitDetector(); }
void ServerController_Connected(VIServerController serverController, EventArgs e) { // run on UI thread Application.Current.Dispatcher.Invoke(() => { // Disable the Connect Button since, at this time, we can't disconnect/reconnect ConnectPB.IsEnabled = false; // get the list of cameras on this server ObservableCollection <Videoinsight.LIB.Camera> cameraList; m_serverController.GetCameraList(out cameraList); // Set the camera list to the View Model for this app vm.CameraList = cameraList; // open dialog that allows you to select the cameras that you want to stream CameraSelection dlg = new CameraSelection(cameraList); dlg.ShowDialog(); // After the dialog is closed, build ViewModel's ActiveDictionary, which is the dictionary that contains the list of the cameras that were marked to be streamed // in the dialog above (the cameras that were checked) vm.ResetActiveDictionary(); // clear the ActiveDictionary foreach (CheckedListItem <Videoinsight.LIB.Camera> cam in dlg.vm.CameraList) { if (cam.IsChecked) { vm.ActiveDictionary.Add(cam.Item.CameraID, new CameraParams(cam.Item, 0, 0, 0, 0, -1, IntPtr.Zero)); } } // Figure out the size of the array of display panels needed to show all the cameras in the Active Dictionary int cols = 0; int rows = 0; GetMatrixDimensions(vm.ActiveDictionary.Count, ref rows, ref cols); // this is a really dumb function that sets the number of rows and cols depending on the number of cameras to be displayed if (m_surfArray != null) { m_surfArray.Shutdown(); m_surfArray = null; } // build the SurfArray = this is the 2D "array" of D3D9 panels that are used to display the decoded images m_surfArray = new SurfArray(rows, cols, DisplayContainer); // configure the behavior and look of the Surface Array Color panelSelectedColor = Colors.Red; Color panelUnselectedColor = Colors.Gray; Color panelTitleTextColor = Colors.White; Color surfArrayBackgroundColor = Colors.Gray; double titleFontSize = 12.0; double panelMargin = 10.0; m_surfArray.SetDisplayParams(panelSelectedColor, panelUnselectedColor, panelTitleTextColor, titleFontSize, surfArrayBackgroundColor, panelMargin); m_surfArray.SetPanelsSelectable(true); // if true, the callback function is called when a panel is clicked // set the callback function that is called whenever a particular panel is clicked m_surfArray.SetCallback(CallbackFunction); // assign each camera to a position in the SurfArray int index = 0; foreach (KeyValuePair <int, CameraParams> entry in vm.ActiveDictionary) { int id = entry.Key; //CameraParams cp = entry.Value; if (index >= vm.ActiveDictionary.Count) { break; } int r = index / cols; int c = index - (r * cols); Videoinsight.LIB.Camera cam = vm.GetCamera(id); if (cam != null) { m_surfArray.AssignCameraToPosition(r, c, (uint)cam.CameraID, m_defaultWidth, m_defaultHeight, cam.CameraName, false); } entry.Value.DisplayRow = r; entry.Value.DisplayCol = c; entry.Value.SurfaceIndex = m_surfArray.GetSurfaceIndex(r, c); uint w = m_defaultWidth; uint h = m_defaultHeight; bool useAlpha = false; IntPtr pSurf = IntPtr.Zero; m_surfArray.GetSurface_Params(entry.Value.SurfaceIndex, out pSurf, out w, out h, out useAlpha); entry.Value.Width = (int)w; entry.Value.Height = (int)h; entry.Value.pSurface = pSurf; entry.Value.d3dImage = m_surfArray.GetD3DImage(entry.Value.DisplayRow, entry.Value.DisplayCol); bool success = CudaTools.DX_GpuAddD3DSurface(m_cudaUtil, id, entry.Value.pSurface, entry.Value.Width, entry.Value.Height); //if (vm.ActiveDictionary.ContainsKey(id)) // vm.ActiveDictionary[id] = cp; //else // Debug.Print("Shit"); index++; } // See how much memory is left on GPU ulong totMem = 0; ulong freeMem = 0; CudaTools.Cuda_GetDeviceMemory64(m_cudaUtil, out totMem, out freeMem); //MessageBox.Show("Total = " + totMem.ToString() + " Free = " + freeMem.ToString()); // command server to start streaming List <int> cameraIDList = new List <int>(); foreach (int id in vm.ActiveDictionary.Keys) { cameraIDList.Add(id); } m_serverController.GetLive(cameraIDList); }); }
void ServerController_Connected(VIServerController serverController, EventArgs e) { // run on UI thread Application.Current.Dispatcher.Invoke(() => { // Disable the Connect Button since, at this time, we can't disconnect/reconnect ConnectPB.IsEnabled = false; vm.ControlPanelVisibility = Visibility.Visible; // get the list of cameras on this server ObservableCollection <Videoinsight.LIB.Camera> cameraList; m_serverController.GetCameraList(out cameraList); // Set the camera list to the View Model for this app vm.CameraList = cameraList; // open dialog that allows you to select the cameras that you want to stream CameraSelection dlg = new CameraSelection(cameraList); dlg.ShowDialog(); int selectedCameraID = dlg.vm.SelectedCamera.ID; // After the dialog is closed, build ViewModel's ActiveDictionary, which is the dictionary that contains the list of the cameras that were marked to be streamed // in the dialog above (the cameras that were checked) foreach (Videoinsight.LIB.Camera cam in cameraList) { if (cam.CameraID == selectedCameraID) { vm.TestCamera = new CameraParams(cam, 0, 0, 0, 0, -1, IntPtr.Zero, null, -1); vm.RefCamera = new CameraParams(cam, 0, 0, 0, 0, -1, IntPtr.Zero, null, -1); break; } } if (m_surfArray != null) { m_surfArray.Shutdown(); m_surfArray = null; } // build the SurfArray = this is the 2D "array" of D3D9 panels that are used to display the decoded images m_surfArray = new SurfArray(1, 2, DisplayContainer); // configure the behavior and look of the Surface Array Color panelSelectedColor = Colors.Red; Color panelUnselectedColor = Colors.Gray; Color panelTitleTextColor = Colors.White; Color surfArrayBackgroundColor = Colors.Gray; double titleFontSize = 12.0; double panelMargin = 10.0; m_surfArray.SetDisplayParams(panelSelectedColor, panelUnselectedColor, panelTitleTextColor, titleFontSize, surfArrayBackgroundColor, panelMargin); m_surfArray.SetPanelsSelectable(false); // if true, the callback function is called when a panel is clicked // set the callback function that is called whenever a particular panel is clicked m_surfArray.SetCallback(CallbackFunction); // assign each camera to a position in the SurfArray uint w = m_defaultWidth; uint h = m_defaultHeight; bool useAlpha = false; IntPtr pSurf = IntPtr.Zero; bool success = false; // Reference Image m_surfArray.AssignCameraToPosition(0, 0, (uint)selectedCameraID + 1, m_defaultWidth, m_defaultHeight, "Reference Image", false); vm.RefCamera.SurfaceIndex = m_surfArray.GetSurfaceIndex(0, 0); pSurf = IntPtr.Zero; m_surfArray.GetSurface_Params(vm.RefCamera.SurfaceIndex, out pSurf, out w, out h, out useAlpha); vm.RefCamera.DisplayRow = 0; vm.RefCamera.DisplayCol = 0; vm.RefCamera.Width = (int)w; vm.RefCamera.Height = (int)h; vm.RefCamera.pSurface = pSurf; vm.RefCamera.d3dImage = m_surfArray.GetD3DImage(vm.RefCamera.DisplayRow, vm.RefCamera.DisplayCol); vm.RefCamera.ID = selectedCameraID + 1; success = CudaTools.DX_GpuAddD3DSurface(m_cudaUtil, vm.RefCamera.SurfaceIndex, vm.RefCamera.pSurface, vm.RefCamera.Width, vm.RefCamera.Height); // Test Image m_surfArray.AssignCameraToPosition(0, 1, (uint)selectedCameraID, m_defaultWidth, m_defaultHeight, "Test Image", false); vm.TestCamera.SurfaceIndex = m_surfArray.GetSurfaceIndex(0, 1); pSurf = IntPtr.Zero; m_surfArray.GetSurface_Params(vm.TestCamera.SurfaceIndex, out pSurf, out w, out h, out useAlpha); vm.TestCamera.DisplayRow = 0; vm.TestCamera.DisplayCol = 1; vm.TestCamera.Width = (int)w; vm.TestCamera.Height = (int)h; vm.TestCamera.pSurface = pSurf; vm.TestCamera.d3dImage = m_surfArray.GetD3DImage(vm.TestCamera.DisplayRow, vm.TestCamera.DisplayCol); vm.TestCamera.ID = selectedCameraID; success = CudaTools.DX_GpuAddD3DSurface(m_cudaUtil, vm.TestCamera.SurfaceIndex, vm.TestCamera.pSurface, vm.TestCamera.Width, vm.TestCamera.Height); m_refCanvas = m_surfArray.GetCanvas((uint)vm.RefCamera.ID); m_testCanvas = m_surfArray.GetCanvas((uint)vm.TestCamera.ID); InitRoiSelectionControls(); // See how much memory is left on GPU ulong totMem = 0; ulong freeMem = 0; CudaTools.Cuda_GetDeviceMemory64(m_cudaUtil, out totMem, out freeMem); //MessageBox.Show("Total = " + totMem.ToString() + " Free = " + freeMem.ToString()); // command server to start streaming List <int> cameraIDList = new List <int>(); cameraIDList.Add(vm.TestCamera.ID); m_serverController.GetLive(cameraIDList); }); }
private void NewLiveFrameHandler(byte[] frame, IntPtr frameGPU, int width, int height, int cameraID, System.Drawing.Imaging.PixelFormat pf, Exception exceptionState) { // OK it's in here // SOmehting in here causing it Stopwatch sw = new Stopwatch(); sw.Start(); if (cameraID == vm.TestCamera.ID) { if (vm.TestCamera.pSurface != IntPtr.Zero) { try { //if the size of the image has changed, need to resize the DirectX surface if (width != vm.TestCamera.Width || height != vm.TestCamera.Height) { ConfigImageD3DSurface(vm.TestCamera.ID, (uint)width, (uint)height, false); vm.TestCamera.Width = width; vm.TestCamera.Height = height; } vm.TestCamera.d3dImage.Lock(); vm.TestCamera.d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, vm.TestCamera.pSurface); // copy GPU array (output of decoder) onto the IDirect3DSurface9 //CudaTools.DX_GpuCopyImageToSurface(m_cudaUtil, cameraID, frameGPU); byte[] imageData; if (CudaTools.Cuda_CopyDataFromGpu(m_cudaUtil, out imageData, frameGPU, width * height * 4)) { // Calculate Detector Value if (m_detector != null && vm.ROIset) { double value = 0.0; int selectedDetector = vm.SelectedDetector; switch (selectedDetector) { case 0: value = m_detector.SurfCalcCorrelation(imageData); break; case 1: value = m_detector.AkazeCalcCorrelation(imageData); break; case 2: value = m_detector.CalcTemplateMatch(imageData); break; } //double value = m_detector.CalcTemplateMatch(imageData); vm.DetectorValue = value.ToString(); vm.AddPoint(value); } } vm.TestCamera.d3dImage.AddDirtyRect(new Int32Rect(0, 0, width, height)); vm.TestCamera.d3dImage.Unlock(); // if CapRefImage_PB has been clicked, display it in the Ref Image Frame and copy it to m_refImageData if (m_captureRefImage) { m_captureRefImage = false; // copy data to ref image if (CudaTools.Cuda_CopyDataFromGpu(m_cudaUtil, out m_refImageData, frameGPU, width * height * 4)) { //if the size of the image has changed, need to resize the DirectX surface if (width != vm.RefCamera.Width || height != vm.RefCamera.Height) { ConfigImageD3DSurface(vm.RefCamera.ID, (uint)width, (uint)height, false); vm.RefCamera.Width = width; vm.RefCamera.Height = height; } vm.RefCamera.d3dImage.Lock(); vm.RefCamera.d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, vm.RefCamera.pSurface); CudaTools.DX_GpuCopyImageToSurface(m_cudaUtil, vm.RefCamera.ID, frameGPU); vm.RefCamera.d3dImage.AddDirtyRect(new Int32Rect(0, 0, width, height)); vm.RefCamera.d3dImage.Unlock(); } else { m_refImageData = null; } } } catch (Exception e) { Debug.Print("Error: " + e.Message); } } } sw.Stop(); long t = sw.ElapsedMilliseconds; UInt64 totalMem = 0; UInt64 freeMem = 0; int w = vm.TestCamera.Width; int h = vm.TestCamera.Height; CudaTools.Cuda_GetDeviceMemory64(m_cudaUtil, out totalMem, out freeMem); if (freeMem < 1000000) { Debug.Print("Out of GPU Memory"); } //Debug.Print("Display Time: " + t.ToString()); }