opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

Recording time at 30fps with usb webcam is correct but is halt time at 60fps

Open ivanjimenez opened this issue 1 year ago • 7 comments

Summary of your issue

No Issues related.

The problem is that my app with opencvcsharp record a video at 30fps during 30s it will obtain a file with 30fps and 30seconds of time. But if a video is recorded at 60fps during 30s, it will obtain a file with 15s of real time.

Environment

OS: MS Windows 10 Pro IDE: Visual Studio 2022 CE .NET: 7.0 Camera: Webcam USB 30fps/60fps/120fps

What did you do when you faced the problem?

I tried with other resolutions and code passing as configuration through opencvchsarp but results went wrong.

I tried to use other webcam options such as iVCAM (webcam across Android) with an Android with camera at 60fps. The recorded time is correct in this camera option.

Example code:

 //Initialization of camera
 public async void initializeCamara(int index, int fps, System.Drawing.Size resolution)
        {
            this.fps = fps;
            this.resolution = resolution;
            
            this.index = index;
            currentFrame = getBlackImage();
            imgViewport.Source = BitmapSourceConverter.ToBitmapSource(currentFrame);
            
            clearReplay();

            cancellationTokenSourceDisplay = new CancellationTokenSource();
            cancellationTokenDisplay = cancellationTokenSourceDisplay.Token;
            videoCapture = new VideoCapture(index, VideoCaptureAPIs.DSHOW);
            videoCapture.Set(VideoCaptureProperties.Fps, this.fps);
            videoCapture.Set(VideoCaptureProperties.FrameHeight, resolution.Height);
            videoCapture.Set(VideoCaptureProperties.FrameWidth, resolution.Width);
            cameraChanged?.Invoke(this, EventArgs.Empty);
            layoutAnchorable.Title = getTitle();
            await Task.Run(() => displayCameraCallback());
        }
        // Closing current cam window 
        private void onClose(object sender, RoutedEventArgs e)
        {
            endCameraTask();
        }
        private void endCameraTask()
        {
            if (videoCapture != null)
            {
                cancellationTokenSourceDisplay.Cancel();
            }
        }

        // Update the image
        private async Task displayCameraCallback()
        {
            while (true)
            {
                if (cancellationTokenDisplay.IsCancellationRequested)
                {
                    videoCapture.Release();
                    videoCapture = null;
                    currentFrame = getBlackImage();
                    await Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
                    {
                        imgViewport.Source = BitmapSourceConverter.ToBitmapSource(getBlackImage());
                    });
                    cameraChanged?.Invoke(this, EventArgs.Empty);
                    return;
                }
                //Mat frame = new Mat();
                videoCapture.Read(currentFrame);
                if (!currentFrame.Empty())
                {
                    //currentFrame = frame;
                    await Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
                    {
                        imgViewport.Source = BitmapSourceConverter.ToBitmapSource(currentFrame);
                    }
                    );
                }
                await Task.Delay(1000 / fps);
            }
        }

Output:

Output is correct. The code seems good. The problem is maybe configuration is not correct for webcam.

What did you intend to be?

  • Change the resolution and codec to opencvchsarp.
  • Record a video sample with Webcam app in Windows at 60fps and all is correct.

ivanjimenez avatar Jun 02 '23 15:06 ivanjimenez

I know it is not direct reply to your question, but here https://github.com/kashiash/VLCSimpleWebCamViewer/blob/master/WinFormsOpenCvRecorder/Recorder.cs you can find code where recording has right time

kashiash avatar Jun 02 '23 18:06 kashiash

I know it is not direct reply to your question, but here https://github.com/kashiash/VLCSimpleWebCamViewer/blob/master/WinFormsOpenCvRecorder/Recorder.cs you can find code where recording has right time

I cannot access to the repo, it seems is not available anymore. I have tried to find out some repo with other name in kashiash's repo without success. Could you provide me another option to get some tip to know the code to solve my issue.

I appreciate your help and support.

ivanjimenez avatar Jun 02 '23 21:06 ivanjimenez

sorry, I forget it is private repo, I just made it public, try again

kashiash avatar Jun 03 '23 20:06 kashiash

I am going to test this code. Sure it will be work. Thanks a bunch!!

ivanjimenez avatar Jun 05 '23 22:06 ivanjimenez

i observed there a big memory consumption, if you cfind solution please share it

kashiash avatar Jun 10 '23 06:06 kashiash

Sure, I have tested your code but I obtain a video file with no content. I have to test deeply.

I have an usb 3.0 Webcam that works to 30, 60 and 120 fps. But it is impossible to record to 60fps well.

Otherwise, I have tested with ivCAM (Wificam to Android) for 60 fps and all is correct. I have atttached a sample code (no github because is very simple) if you want to test it.

I will try to test your code properly and maybe follow another solutions. opencv60fps.zip

ivanjimenez avatar Jun 10 '23 13:06 ivanjimenez

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 15 '23 06:12 stale[bot]