BetaJedi
Posts: 6
Joined: Fri Dec 20, 2019 5:01 pm
Location: United Kingdom

Class/Library for C# Live Camera Feed/Preview?

Sun Dec 22, 2019 2:02 pm

Hi all,

I'm looking in to creating a mono C# program that does some very basic functions with a Raspberry Pi Camera V2, such as take still images and record videos of a specified length.

At the moment I'm using a work around to take photos/videos by constructing lxterminal commands for "raspistill" and "raspivid" and executing them using Process.Start, but ideally I'd also like to have a box in my form that has a "Live Preview/Feed" display, showing what the camera sees in realtime (or slight delay, just getting it to work is good enough for now)

I've seen a lot of threads talk about MMALSharp, using Python instead (which I'd like to avoid doing), and using services such as Motion, but the threads are either unsolved, ancient, or just so convoluted with tech support that it's hard to discern any actual information from them.

So, with that being said: Does anybody know of any libraries or classes that allow you to generate a live preview of the Raspberry Pi Camera? I'm quite new to GUI C#, as I've mainly dealt with Console applications, so any snippets of code/explanations of objects or methods which you feel like including would be appreciated.

Thanks very much

Edit: I should note that I'm using a Raspberry Pi 4 B
"To start press any key" ... Where's the any key?!

User avatar
HermannSW
Posts: 1782
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Class/Library for C# Live Camera Feed/Preview?

Sun Dec 22, 2019 3:04 pm

It is C code and not C#, but you might have a look at:
https://github.com/Hermann-SW2/userland ... i420toh264

It takes raspividyuv stream and processes on the fly, before finally using GPU to encode as h264.
i420toh264 allows to specify "-" as outputfile, and so you can use modified "hello_pi/hello_video" in my branch that allows to specify "-" as input file to directly display the video.
Yes, not perfect to h264 encode and h264 decode before viewing, but both is done by GPU.
I am still trying to figure out how to do hello_video like display of i420 (yuv) video stream.
If you just drop frames in between you can keep display stopped, and when you can pass frames again when you want display updates.

Access by that solution is very low level, but you don't need to learn complex frameworks, and it contains many documented examples.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

BetaJedi
Posts: 6
Joined: Fri Dec 20, 2019 5:01 pm
Location: United Kingdom

Re: Class/Library for C# Live Camera Feed/Preview?

Sun Dec 22, 2019 3:54 pm

HermannSW wrote:
Sun Dec 22, 2019 3:04 pm
It is C code and not C#, but you might have a look at:
https://github.com/Hermann-SW2/userland ... i420toh264
Thanks for sharing this HermannSW. I've had a look at the documentation and I think it could be quite useful to learn from if I ever need to use motion tracking and the likes! Unfortunately I would ideally like to isolate my solution to be entirely within my C# project and not have to run a C project alongside it, but I'm very impressed with the amount of detail and functionality your library has!

Perhaps I need to specifically describe what I'm trying to achieve so that it's more clear as to what I'm aiming for? I have created a Windows Form in Visual Studio 2019, which has an area that looks something like this:
Image

At this moment, the black box which says that the "Live Preview is not yet operational" is just an image with text on it which I have used for a placeholder. Ideally, I'd like a user to be able to click the "Activate" button and it will show a preview of what the camera sees within the form. It doesn't really need any more complications or extra features aside from showing a preview, as it's really just a box which will show what the camera actually sees so that I can view positioning etc as I work on it.

Any thoughts on how I could achieve this solely within C#?
"To start press any key" ... Where's the any key?!

BetaJedi
Posts: 6
Joined: Fri Dec 20, 2019 5:01 pm
Location: United Kingdom

Re: Class/Library for C# Live Camera Feed/Preview?

Tue Dec 24, 2019 3:16 pm

Sorry to bump this, but does anybody have any thoughts?
"To start press any key" ... Where's the any key?!

techyian
Posts: 65
Joined: Mon Jan 22, 2018 11:40 am

Re: Class/Library for C# Live Camera Feed/Preview?

Fri Jan 03, 2020 8:25 am

Hi,

I maintain the MMALSharp project so I'm happy to help you with anything specific to that project. I'm not sure whether there are any current offerings in the C#/.NET world which will provide you with a video GUI control. There are projects like Avalonia https://github.com/AvaloniaUI/Avalonia which brings you a cross platform GUI framework which will run on the Raspberry Pi, but I don't think they have a video control yet unfortunately due to the complexities involved. It's not something I've looked into for some time, so things may have changed.

People have had success in the past with using HTML5 to stream the camera in real time using technologies such as RTMP. Dave Jones has also created pistreaming https://github.com/waveform80/pistreaming which you could take inspiration from. You could replace the picamera and ws4py dependencies with MMALSharp and SignalR for instance.

Sorry I've only just picked up on this now. I don't regularly check the Pi forums. If you need direct help from me, it's better to raise a ticket on the MMALSharp github page, or reach out on our Gitter https://gitter.im/MMALSharp/Lobby?utm_s ... share-link.
MMALSharp - C# API for the Raspberry Pi camera module

https://github.com/techyian/MMALSharp

BetaJedi
Posts: 6
Joined: Fri Dec 20, 2019 5:01 pm
Location: United Kingdom

Re: Class/Library for C# Live Camera Feed/Preview?

Wed Jan 08, 2020 6:39 pm

techyian wrote:
Fri Jan 03, 2020 8:25 am
Hi,

I maintain the MMALSharp project so I'm happy to help you with anything specific to that project. I'm not sure whether there are any current offerings in the C#/.NET world which will provide you with a video GUI control. There are projects like Avalonia https://github.com/AvaloniaUI/Avalonia which brings you a cross platform GUI framework which will run on the Raspberry Pi, but I don't think they have a video control yet unfortunately due to the complexities involved. It's not something I've looked into for some time, so things may have changed.

People have had success in the past with using HTML5 to stream the camera in real time using technologies such as RTMP. Dave Jones has also created pistreaming https://github.com/waveform80/pistreaming which you could take inspiration from. You could replace the picamera and ws4py dependencies with MMALSharp and SignalR for instance.

Sorry I've only just picked up on this now. I don't regularly check the Pi forums. If you need direct help from me, it's better to raise a ticket on the MMALSharp github page, or reach out on our Gitter https://gitter.im/MMALSharp/Lobby?utm_s ... share-link.
Hey techyian, sorry for my late reply!

Thanks very much for your feedback, I've actually been using MMALSharp for the more "simple" tasks of taking videos and pictures and it works brilliantly!

I'll have a look in to the projects you recommend, as from reading more deeply in to MMAL it definitely doesn't seem like the easiest thing to work with, so I'm majorly impressed with what you have managed to accomplish with it!

If I need any specific help I'll be sure to use your GitHub tickets as recommended :-)
"To start press any key" ... Where's the any key?!

techyian
Posts: 65
Joined: Mon Jan 22, 2018 11:40 am

Re: Class/Library for C# Live Camera Feed/Preview?

Thu Jan 09, 2020 8:43 am

Thank you, your feedback is much appreciated! Good luck with your project.
MMALSharp - C# API for the Raspberry Pi camera module

https://github.com/techyian/MMALSharp

Return to “Other programming languages”