#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

References

Leave a Reply

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 )

Google photo

You are commenting using your Google 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.