#dotnet – less than 20 lines to display a 🎦 camera feed with #OpenCV and #net5

Buy Me A Coffee

Hi !

This post is the start of a series of posts around OpenCV and DotNet Core .Net 5. Most of my samples are going to be camera based, so I’ll start with the +20 13 lines needed to access a local camera and show the frames in a window.

opencvsharp camera sample

And the code using Net Core 5:

using OpenCvSharp;
var capture = new VideoCapture(0);
var window = new Window("El Bruno – OpenCVSharp demo");
var image = new Mat();
while (true)
{
capture.Read(image);
if (image.Empty())
break;
window.ShowImage(image);
if (Cv2.WaitKey(1) == 113) // Q
break;
}

A couple of notes on previous code

  • Pressing Q key will stop the program
  • Line 2 define the camera to open based on local indexes, in example
// 0 Logitech Brio
// 1 Msft Lifecam
// ... 
// 4 Logitech Capture
// 5 OBS VirtualCam
  • This is a standard .Net 5 Console App
  • I’m using the great OpenCVSharp nuget packages to have OpenCV capabilities in my app.
opencvsharp nuget packages

And because I’m using Net 5, here goes a before / after preview

That’s all for today!

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


References

3 comments

  1. Hello Bruno, I came across your blog for a project I am working as a senior project design. I am currently working with my team to make a WPF project using .NET Core for an animal shelter. We are implementing QR codes into our project and your blog helped a lot. We are able to display our camera, however, we were wondering if you could help us integrate ZXing.NET or some other library you may know of to decode the QR codes when the camera is on during runtime. Is this possible to do? We tried using ZBarcam, however, we are not able to run the exe file during runtime and cannot figure out the functions. They also do not have a documentation page with useful information. Any help is much appreciated. Muchas gracias. Hablo espaΓ±ol tambien si le es mas facil.

    Liked by 1 person

      1. Hello Bruno, your Twitter DM’s are closed. Please allow me to message you via Twitter. Thank you.

        Like

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: