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;
        }
 private void Time_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
 {
     CurrentTime = e.NewValue;
     if (_selectedCameraIndex > -1 && _selectedCameraIndex < WebcamManager.NumberConnectedCameras())
     {
         Image_Viewer.TimeSensitivity = (float)CurrentTime;
     }
 }
 private void Difference_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
 {
     CurrentDifference = e.NewValue;
     if (_selectedCameraIndex > -1 && _selectedCameraIndex < WebcamManager.NumberConnectedCameras())
     {
         Image_Viewer.MotionDiffSensitivity = (int)CurrentDifference;
     }
 }
        public void Zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
        {
            long value = Convert.ToInt64(e.NewValue);

            CurrentZoom = Convert.ToInt32(value);
            if (_selectedCameraIndex > -1 && _selectedCameraIndex < WebcamManager.NumberConnectedCameras())
            {
                WebcamManager.SetPropertyValueManual(_selectedCameraIndex, WebcamManager.CAMERA_PROPERTY.CAMERA_ZOOM, value);
            }
        }
Beispiel #5
0
        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);
            }
        }
 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;
 }
Beispiel #8
0
        public override void UpdateFrame(object sender, EventArgs e)
        {
            WebCam camera = sender as WebCam;

            SelectedCameraIndex = camera.WebCamID;
            if (camera.ImageBuffer == null)
            {
                return;
            }


            if (ImagerWidth != default(double) && ImagerHeight != default(double) && SelectedCameraIndex > -1 && SelectedCameraIndex < WebcamManager.NumberConnectedCameras())
            {
                try
                {
                    int scaledRoiX      = Math.Max(Math.Min(Convert.ToInt32(ImagerWidth * RoiX), Convert.ToInt32(ImagerWidth)), 0);
                    int scaledRoiY      = Math.Max(Math.Min(Convert.ToInt32(ImagerHeight * RoiY), Convert.ToInt32(ImagerHeight)), 0);
                    int scaledRoiWidth  = Math.Max(Math.Min(Convert.ToInt32(ImagerWidth * RoiWidth), Convert.ToInt32(ImagerWidth)), 0);
                    int scaledRoiHeight = Math.Max(Math.Min(Convert.ToInt32(ImagerWidth * RoiHeight), Convert.ToInt32(ImagerHeight)), 0);
                    if (Detector.DetectFace(camera.ImageBuffer, Convert.ToInt32(ImagerWidth), Convert.ToInt32(ImagerHeight), scaledRoiX, scaledRoiY, scaledRoiWidth, scaledRoiHeight))
                    {
                        OnFaceDetected(EventArgs.Empty); // trigger event
                    }

                    if (showDebug)
                    {
                        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (SendOrPostCallback) delegate
                        {
                            populateBitMap(Detector.cvOutImage.Bytes, (int)(ImagerWidth * ImagerHeight * 3));
                            BitmapSource.Invalidate();
                            GC.Collect(); //this is due to a bug in InteropBitmap which causes memory leaks for 24 bit bitmaps... MS: FIX IT!
                        }, null);
                    }
                }

                catch (Emgu.CV.Util.CvException ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ErrorMessage);
                }
            }
        }
        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;
        }