public OpencvSource(string cam_or_url) { MAssert.Check(cam_or_url != string.Empty); // check if cam_or_url is number bool stream = false; for (int i = 0; i < cam_or_url.Length; ++i) { stream = stream || (cam_or_url[i] < '0') || (cam_or_url[i] > '9'); } if (stream) { // open stream Console.WriteLine("opening stream '{0}'", cam_or_url); capturer = new OpenCvSharp.VideoCapture(cam_or_url); } else { // convert to integer int cam_id = Convert.ToInt32(cam_or_url, 10); MAssert.Check(cam_id >= 0, "wrong webcam id"); // open vebcam Console.WriteLine("opening webcam {0}", cam_id); capturer = new OpenCvSharp.VideoCapture(cam_id); MAssert.Check(capturer.IsOpened(), "webcam not opened"); // set resolution capturer.Set(OpenCvSharp.CaptureProperty.FrameWidth, 1280); capturer.Set(OpenCvSharp.CaptureProperty.FrameHeight, 720); MAssert.Check(capturer.IsOpened(), "webcam not opened"); } // sometimes first few frames can be empty even if camera is good // so skip few frames OpenCvSharp.Mat frame; for (int i = 0; i < 10; ++i) { frame = capturer.RetrieveMat(); } // check first two frames OpenCvSharp.Mat image1 = new OpenCvSharp.Mat(), image2 = new OpenCvSharp.Mat(); capturer.Read(image1); capturer.Read(image2); Console.WriteLine("image1 size: {0}", image1.Size()); Console.WriteLine("image1 size: {0}", image2.Size()); MAssert.Check( !image1.Empty() && !image2.Empty() && image1.Size() == image2.Size() && image1.Type() == OpenCvSharp.MatType.CV_8UC3 && image2.Type() == OpenCvSharp.MatType.CV_8UC3, "error opening webcam or stream"); }
private async Task CaptureCamera(CancellationToken token) { if (capture == null) { capture = new cv.VideoCapture(0, cv.VideoCaptureAPIs.ANY); } capture.Open(0); if (capture.IsOpened()) { try { while (!token.IsCancellationRequested) { using MemoryStream memoryStream = capture.RetrieveMat().ToMemoryStream(); await Application.Current.Dispatcher.InvokeAsync(() => { try { var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.CacheOption = BitmapCacheOption.OnLoad; imageSource.StreamSource = memoryStream; imageSource.EndInit(); img_WebCam.Source = imageSource; }catch (Exception e) { } }); bitmapImage = new Bitmap(memoryStream); //await ParseWebCamFrame(bitmapImage, token); } capture.Release(); } catch (Exception e) { } } }
private async Task CaptureCamera(CancellationToken token) { if (capture == null) { capture = new OpenCvSharp.VideoCapture(CaptureDevice.DShow); } capture.Open(0); m_capture.Start(); if (capture.IsOpened()) // if(m_capture.IsOpened) { while (!token.IsCancellationRequested) { using MemoryStream memoryStream = capture.RetrieveMat().Flip(FlipMode.Y).ToMemoryStream(); // using MemoryStream memoryStream = m_capture.QueryFrame()..RetrieveMat().Flip(FlipMode.Y).ToMemoryStream(); await Application.Current.Dispatcher.InvokeAsync(() => { var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.CacheOption = BitmapCacheOption.OnLoad; imageSource.StreamSource = memoryStream; imageSource.EndInit(); OpenCVSharpImageSource.Source = imageSource; }); var bitmapImage = new Bitmap(memoryStream); await ParseWebCamFrame(bitmapImage, token); } capture.Release(); } }
static void Main(string[] args) { //var vc = new OpenCvSharp.VideoCapture("./test/india.mp4"); //var vc = new OpenCvSharp.VideoCapture("./test/Test.mov"); //var vc = new OpenCvSharp.VideoCapture("./test/singleTest.m4v"); //var vc = new OpenCvSharp.VideoCapture("./test/peopleTest.m4v"); var vc = new OpenCvSharp.VideoCapture(); vc.Open(1); ImageRecognizer imageRecognizer = new ImageRecognizer(); int key = int.MinValue; using (Window window = new Window("capture")) { while (key < 0) { vc.Grab(); var mat = vc.RetrieveMat(); if (mat.Empty()) { return; } var faces = imageRecognizer.DetectFaces(mat); if (faces != null) { foreach (var face in faces) { var faceCrop = new Mat(mat, face); faceCrop.SaveImage($"./results/{Guid.NewGuid()}.jpg"); } } window.ShowImage(mat); key = Cv2.WaitKey(10); } } }