menx
Posts: 5
Joined: Wed Jan 31, 2024 10:21 am

Getting recording to work with multiple USB cameras using C# .NET

Wed Jan 31, 2024 11:06 am

Hi,
I've seen this post here: viewtopic.php?p=2167364&hilit=USB+Camera#p2167364 which is the same I'm trying to do but with C# code.
I follow this article https://neilsnotes.net/Software/Coding/dotnetPiCam.html and I simply don't understand why the recording is not working.
I ask to start recording, but the file is not populated. Also, I find the implementation using Windows much simpler. On Windows we just need to call the MediaCapture class to manage everything related with cameras.

Does anyone can light me up on how I can get this to work?
I have a Raspberry Pi 5 with 8GB of RAM.

The event handler, NewImageBufferReady does not get fired.
On VideoConnectionSettings class, I used Bus Id=0. I tried with 1, and the result is the same.

Running the lsusb command, the prompt is:
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0ac8:0346 Z-Star Microelectronics Corp. HBV HD CAMERA
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

menx
Posts: 5
Joined: Wed Jan 31, 2024 10:21 am

Re: Getting recording to work with multiple USB cameras using C# .NET

Wed Feb 14, 2024 11:34 pm

Anyone with the same issue as myself?

User avatar
HermannSW
Posts: 6326
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Getting recording to work with multiple USB cameras using C# .NET

Thu Feb 15, 2024 8:11 pm

First, it would help if you show what you have done sofar.

While this is a C# question, I think it is more a camera question.
In that case you might want to flag your initial post as wrong forum.
I would expect more help potential for your question in camera forum than here under other programming languages.
https://github.com/Hermann-SW/RSA_numbers_factored
https://stamm-wilbrandt.de/GS [304+402+536fps]
https://hermann-sw.github.io/planar_graph_playground
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de

menx
Posts: 5
Joined: Wed Jan 31, 2024 10:21 am

Re: Getting recording to work with multiple USB cameras using C# .NET

Sat Feb 24, 2024 12:01 am

HermannSW wrote:
Thu Feb 15, 2024 8:11 pm
First, it would help if you show what you have done sofar.

While this is a C# question, I think it is more a camera question.
In that case you might want to flag your initial post as wrong forum.
I would expect more help potential for your question in camera forum than here under other programming languages.
Thanks for replying, and sorry for the long delay.
In fact, I posted here because I thought this would be an issue with the implementation of the code regarding the Raspberry Pi .NET IoT stuff.
I can't get the NewImageBufferReady event handler to be fired. Strange...
So, here's the code I implemented:

Code: Select all

using System.Device.Gpio;
using Iot.Device.Graphics;
using Iot.Device.Media;

VideoConnectionSettings videoConnectionSettings = new VideoConnectionSettings(1, (1920, 1080), VideoPixelFormat.H264);
VideoDevice videoDevice = VideoDevice.Create(videoConnectionSettings);
FileStream fileStream = File.Create("/home/marco/CameraRecorderPOC/test.mp4");
videoDevice.NewImageBufferReady += VideoDevice_NewImageBufferReady;
videoDevice.StartCaptureContinuous();
Console.WriteLine("VideoDevice: {0}, Is open: {1} ", videoDevice.DevicePath, videoDevice.IsOpen);
CancellationTokenSource tokenSource = new CancellationTokenSource();


Console.WriteLine("Capturing video, press any key to stop");
int ledPin = 23;

GpioController controller = new GpioController();  
controller.OpenPin(ledPin, PinMode.Output);
controller.Write(ledPin, PinValue.High);

while (!Console.KeyAvailable)
{
    Thread.SpinWait(1);
}

tokenSource.Cancel();
videoDevice.StopCaptureContinuous();
fileStream.Close();
fileStream.Dispose();
videoDevice.Dispose();
controller.Write(ledPin, PinValue.Low);
controller.ClosePin(ledPin);
controller.Dispose();

async void VideoDevice_NewImageBufferReady(object sender, NewImageBufferReadyEventArgs e)
{
    try
    {
        Console.WriteLine("Writing image to disk");
        await fileStream.WriteAsync(e.ImageBuffer, 0, e.Length);
        Console.Write(".");
    }
    catch (ObjectDisposedException)
    {
        // ignore this as its thrown when the stream is stopped
    }
}

menx
Posts: 5
Joined: Wed Jan 31, 2024 10:21 am

Re: Getting recording to work with multiple USB cameras using C# .NET

Sun Mar 03, 2024 1:01 am

I figured out why "VideoDevice_NewImageBufferReady" was not firing. It was because, I wasn't launching a new thread with the CaptureContinuous method. Now it's recording and the file is being populated.
Now, I came across another roadblock. The video captured appears to be a still image. I grabbed the video to my dev PC, and open its metadata to know which encoding is being used. It says JPEG. How's that possible? I asked to record with H264.

The class I call is, VideoConnectionSettings videoConnectionSettings = new VideoConnectionSettings(0, (1920, 1080), VideoPixelFormat.H264);

I have these libraries installed on Rp5
v4l-utils libc6-dev libgdiplus libx11-dev
I'm using a Raspberry Pi 5 in headless mode, so I have always a remote connection on to grab the files.

Anyone interested to help me out?
Thanks.
Screenshot 2024-03-03 010048.png
Screenshot 2024-03-03 010048.png (362.77 KiB) Viewed 413 times


Return to “Other programming languages”