private void Button_Clicked(object sender, RoutedEventArgs e) { if (!_selectedState) { SelectedCameraIndex = _selectedCameraIndex = camera_selector.GetSelectedCameraIndex(); SelectedCameraName = WebcamManager.GetCamera(SelectedCameraIndex).WebCamName; SelectedCameraPath = WebcamManager.GetCamera(SelectedCameraIndex).WebCamPath; if (SelectedCameraIndex > -1 && SelectedCameraIndex < WebcamManager.NumberConnectedCameras()) { Image_Viewer.SetImageSource(WebcamManager.GetCamera(_selectedCameraIndex)); } SelectorPanel.Visibility = Visibility.Collapsed; ROIPanel.Visibility = Visibility.Visible; ButtonState.Content = "<< Select another camera"; } else { ROIPanel.Visibility = Visibility.Collapsed; if (_selectedCameraIndex > -1) { WebCam camera = WebcamManager.GetCamera(_selectedCameraIndex); Image_Viewer.RemoveImageSource(camera); Image_Viewer.Clear(); Image_Viewer_Orig.RemoveImageSource(camera); } SelectorPanel.Visibility = Visibility.Visible; ButtonState.Content = "Configure person detector >>"; } _selectedState = !_selectedState; }
void InitializeFaceDetection(int cameraindex) { Thread thread = new Thread(() => { try { ReleasePreviousDetectors(); WebcamManager.SetPropertyValueManual(cameraindex, WebcamManager.CAMERA_PROPERTY.CAMERA_FOCUS, camerafocus); WebcamManager.SetPropertyValueManual(cameraindex, WebcamManager.CAMERA_PROPERTY.CAMERA_ZOOM, camerazoom); faceDetector = new WebCamFaceDetector(); faceDetector.ToggleVisualization(); faceDetector.RegisterForImages(WebcamManager.GetCamera(cameraindex)); faceDetector.SelectedCameraIndex = cameraindex; faceDetector.RoiX = roiX; faceDetector.RoiY = roiY; faceDetector.RoiWidth = roiWidth; faceDetector.RoiHeight = roiHeight; faceDetector.FaceDetected += WebCamFaceDetected; } catch (Exception err) { MessageBox.Show(err.ToString()); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); }
void InitializeMotionDetection(int cameraindex) { Thread thread = new Thread(() => { try { ReleasePreviousDetectors(); WebcamManager.SetPropertyValueManual(cameraindex, WebcamManager.CAMERA_PROPERTY.CAMERA_FOCUS, camerafocus); WebcamManager.SetPropertyValueManual(cameraindex, WebcamManager.CAMERA_PROPERTY.CAMERA_ZOOM, camerazoom); motionDetector = new WebCamMotionDetector(); motionDetector.ToggleVisualization(); motionDetector.RegisterForImages(WebcamManager.GetCamera(cameraindex)); motionDetector.SelectedCameraIndex = cameraindex; motionDetector.MotionAreaPercentageSensitivity = percentageSensitivity; motionDetector.TimeSensitivity = timeThreshold; motionDetector.RoiX = roiX; motionDetector.RoiY = roiY; motionDetector.RoiWidth = roiWidth; motionDetector.RoiHeight = roiHeight; motionDetector.MotionDiffSensitivity = differenceThreshold; motionDetector.MotionDetected += WebCamMotionDetected; } catch (Exception err) { MessageBox.Show(err.ToString()); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); }
public void CleanUp() { if (_selectedCameraIndex > -1) { WebCam camera = WebcamManager.GetCamera(_selectedCameraIndex); Image_Viewer.RemoveImageSource(camera); Image_Viewer.Clear(); Image_Viewer_Orig.RemoveImageSource(camera); } }
protected int LookforSelectedCamera(bool force = false) { bool selectedCameraFound = false; selectedCameraConnected = false; if (!IsEnabled && !force) { return(-1); } int numberConnectedCameras = WebcamManager.NumberConnectedCameras(); if (numberConnectedCameras == 0) { selectedCameraConnected = false; Logger.WriteLine("No camera available"); ErrorLog.AddError(ErrorType.Failure, "Motion detection is disabled because no camera was detected"); return(-1); } int index = -1; if (selectedCameraPath == default(string)) { ErrorLog.AddError(ErrorType.Message, "No webcam configuration. Defaulting to first available webcam."); return(-1); } else { for (int i = 0; i < numberConnectedCameras; i++) { if (WebcamManager.GetCamera(i).WebCamPath == selectedCameraPath) { if (WebcamManager.GetCamera(i).IsActive) { index = i; selectedCameraConnected = true; selectedCameraFound = true; break; } else if (WebcamManager.StartCamera(i, captureWidth, captureHeight)) { index = i; selectedCameraConnected = true; selectedCameraFound = true; break; } } } if (!selectedCameraFound && numberConnectedCameras > 0) { ErrorLog.AddError(ErrorType.Failure, "The originally selected camera is not available."); } return(index); } }
public void SetImageSource(int index) { if (index > -1 && _selectedCameraIndex < WebcamManager.NumberConnectedCameras()) { _selectedCameraIndex = index; WebCam camera = WebcamManager.GetCamera(index); long mn = 0, mx = 0, step = 0; if (WebcamManager.GetMinPropertyValue(index, WebcamManager.CAMERA_PROPERTY.CAMERA_FOCUS, ref mn) && WebcamManager.GetMaxPropertyValue(index, WebcamManager.CAMERA_PROPERTY.CAMERA_FOCUS, ref mx)) { FocusMinimum = Convert.ToDouble(mn); FocusMaximum = Convert.ToDouble(mx); if (FocusMinimum != FocusMaximum) { Focus_Slider.IsEnabled = true; } else { FocusMinimum = 0; FocusMaximum = 1; Focus_Slider.IsEnabled = false; } } else { FocusMinimum = 0; FocusMaximum = 1; Focus_Slider.IsEnabled = false; } if (WebcamManager.GetMinPropertyValue(index, WebcamManager.CAMERA_PROPERTY.CAMERA_ZOOM, ref mn) && WebcamManager.GetMaxPropertyValue(index, WebcamManager.CAMERA_PROPERTY.CAMERA_ZOOM, ref mx)) { ZoomMinimum = Convert.ToDouble(mn); ZoomMaximum = Convert.ToDouble(mx); if (ZoomMinimum != ZoomMaximum) { Zoom_Slider.IsEnabled = true; } else { ZoomMinimum = 0; ZoomMaximum = 1; Zoom_Slider.IsEnabled = false; } } else { ZoomMinimum = 0; ZoomMaximum = 1; Zoom_Slider.IsEnabled = false; } Image_Viewer.SetImageSource(camera); Image_Viewer_Orig.SetImageSource(camera); } }
public string GetSelectedCameraName() { if (selectedCameraIndex != -1) { return(WebcamManager.GetCamera(cameraList[selectedCameraIndex]).WebCamName); } else { return(null); } }
protected override void OnDeleted() { if (webcambuffer != null && selectedCameraIndex != -1) { webcambuffer.UnregisterForImages(WebcamManager.GetCamera(selectedCameraIndex)); ReleasePreviousBuffers(); WebcamManager.ReleaseInactiveCameras(); if (callbacksRegistered) { WebcamManager.UnregisterWebcamConnectionEvent(OnCameraConnected); WebcamManager.UnregisterWebcamRemovalEvent(OnCameraDisconnected); callbacksRegistered = false; } } }
public void OnCameraConnected(object sender, USBEvent e) { if (!selectedCameraConnected) { Thread.Sleep(50); WebcamManager.RestartService(); selectedCameraIndex = LookforSelectedCamera(); if (selectedCameraIndex != -1) { Logger.WriteLine("Selected camera reconnected"); ErrorLog.AddError(ErrorType.Message, "Selected camera has been reconnected"); webcambuffer.RegisterForImages(WebcamManager.GetCamera(selectedCameraIndex)); WebcamManager.ReleaseInactiveCameras(); } } }
private void Viewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ImageViewer mouseViewer = sender as ImageViewer; int index = cameraList[cameraPreviews.IndexOf(mouseViewer)]; if (index != selectedCameraIndex && selectedCameraIndex != -1) { cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerBorderColor = "Transparent"; cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerWidth = previewWidth; cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerHeight = previewHeight; mouseViewer.ViewerBorderColor = "LightGreen"; mouseViewer.ViewerWidth = previewHeight + 16; mouseViewer.ViewerHeight = mouseViewer.ViewerWidth * 3 / 4; selectedCameraIndex = cameraList[cameraPreviews.IndexOf(mouseViewer)]; //mainView[0].TopTitle = WebcamManager.GetCamera(selectedCameraIndex).WebCamName; //mainView[0].SetImageSource(WebcamManager.GetCamera(selectedCameraIndex)); selectedCameraName = WebcamManager.GetCamera(selectedCameraIndex).WebCamName; } }
void InitializeSnapshot(int cameraindex) { Thread thread = new Thread(() => { try { ReleasePreviousBuffers(); if (webcambuffer == null) { webcambuffer = new WebCamBuffer(); } webcambuffer.RegisterForImages(WebcamManager.GetCamera(cameraindex)); } catch (Exception err) { MessageBox.Show(err.ToString()); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); }
//[MethodImpl(MethodImplOptions.Synchronized)] protected override void OnDisabled(DisabledEventArgs e) { if (!e.IsConfiguring) { if (motionDetector != null && selectedCameraIndex != -1) { motionDetector.MotionDetected -= WebCamMotionDetected; motionDetector.UnregisterForImages(WebcamManager.GetCamera(selectedCameraIndex)); ReleasePreviousDetectors(); WebcamManager.SetPropertyValueAuto(selectedCameraIndex, WebcamManager.CAMERA_PROPERTY.CAMERA_FOCUS); WebcamManager.SetPropertyValueAuto(selectedCameraIndex, WebcamManager.CAMERA_PROPERTY.CAMERA_ZOOM); WebcamManager.ReleaseInactiveCameras(); if (callbacksRegistered) { WebcamManager.UnregisterWebcamConnectionEvent(OnCameraConnected); WebcamManager.UnregisterWebcamRemovalEvent(OnCameraDisconnected); callbacksRegistered = false; } } } }
private void Button_Clicked(object sender, RoutedEventArgs e) { if (!_selectedState) { SelectedCameraIndex = camera_selector.GetSelectedCameraIndex(); SelectedCameraName = WebcamManager.GetCamera(SelectedCameraIndex).WebCamName; SelectedCameraPath = WebcamManager.GetCamera(SelectedCameraIndex).WebCamPath; if (SelectedCameraIndex > -1 && SelectedCameraIndex < WebcamManager.NumberConnectedCameras()) { camera_roi_selector.SetImageSource(SelectedCameraIndex); } SelectorPanel.Visibility = Visibility.Collapsed; ROIPanel.Visibility = Visibility.Visible; ButtonState.Content = "<< Select another camera"; } else { ROIPanel.Visibility = Visibility.Collapsed; camera_roi_selector.CleanUp(); SelectorPanel.Visibility = Visibility.Visible; ButtonState.Content = "Configure motion detector >>"; } _selectedState = !_selectedState; }
private void UpdatePanel() { if (WebcamManager.IsServiceRestartRequired()) { WebcamManager.RestartService(); } if (!callbacksRegistered) { WebcamManager.RegisterWebcamConnectionEvent(OnCameraConnected); WebcamManager.RegisterWebcamRemovalEvent(OnCameraDisconnected); callbacksRegistered = true; } numberConnectedCameras = WebcamManager.NumberConnectedCameras(); cameraList = new List <int>(); selectedCameraIndex = -1; int numAvailableCameras = 0; cameraPreviews.Clear(); //remove previous viewers //populate the camera list and find the one that is available and set it as current selection... if (numberConnectedCameras == 0) { ShowNoCamerasFoundPanel(); return; } for (int i = 0; i < numberConnectedCameras; i++) { //if (WebcamManager.StartCamera(i, captureWidth, captureHeight)) { ImageViewer viewer = new ImageViewer(); viewer.ViewerWidth = previewWidth; viewer.ViewerHeight = previewHeight; viewer.TopTitle = WebcamManager.GetCamera(i).WebCamName; if (WebcamManager.StartCamera(i, captureWidth, captureHeight)) { if (viewer.TopTitle == selectedCameraName || selectedCameraIndex == -1) { selectedCameraIndex = i; } viewer.SetImageSource(WebcamManager.GetCamera(i)); viewer.MouseEnter += Viewer_MouseEnter; viewer.MouseLeave += Viewer_MouseLeave; viewer.PreviewMouseLeftButtonUp += Viewer_MouseLeftButtonUp; ++numAvailableCameras; } else { viewer.ImageSource.Source = loadBitmap(Properties.Resources.CamBusyIcon); viewer.BottomTitle = "This webcam is already busy"; } cameraList.Add(i); cameraPreviews.Add(viewer); } } if (selectedCameraIndex != -1) { cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerBorderColor = "LightGreen"; cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerWidth = previewHeight + 16; cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerHeight = cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerWidth * 3 / 4; } //release inactive cameras so that they can be made available to other processes... WebcamManager.ReleaseInactiveCameras(); CameraReadiness = true; }