
Hi !
A couple of days ago I wrote this post, and made the sample with Python. So today, same scenario, however with C# 9 and Net5.
This is a non-usual scenario, however I’m on a point where I need to extract all the frames from a video file. The reason: some of these frames will be used to train a Machine Learning model.
There are tools that can do this, however it’s a nice moment to do some OpenCV code. Let’s go for it. A couple of remarks
- Video file must be in the same folder as python file, and the name is defined in video_file, line 7
- There is a live preview of the video, comment line 24 to avoid this
- You can stop the process at any time pressing the Q letter
using System;
using OpenCvSharp;
var videoFile = "01.mp4";
System.IO.Directory.CreateDirectory("frames");
var capture = new VideoCapture(videoFile);
var window = new Window("El Bruno - OpenCVSharp Save Video Frame by Frame");
var image = new Mat();
var i = 0;
while (capture.IsOpened())
{
capture.Read(image);
if (image.Empty())
break;
i++;
var imgNumber = i.ToString().PadLeft(8, '0');
var frameImageFileName = $@"frames\image{imgNumber}.png";
Cv2.ImWrite(frameImageFileName, image);
window.ShowImage(image);
if (Cv2.WaitKey(1) == 113) // Q
break;
}
Console.WriteLine("Complete !");
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
Net 5 and OpenCV
- 13 lines to display a π¦ camera feed with OpenCV and Net5
- Detecting edges on the π¦ camera feed with Canny algorithm, OpenCV and Net5
- Detecting corners on the π¦ camera feed with FAST algorithm, OpenCV and Net 5
- Display the π¦ camera feed in a WinForm using OpenCV and Net5
- Face Detection using Face Cascades on the π¦ camera feed using OpenCV and Net 5
- Face Detection using DNN on the π¦ camera feed using OpenCV and Net 5
- Face Detection, DNN vs Haar Cascades on the π¦ camera feed using OpenCV and Net 5
- Age and Gender estimation on the π¦ camera feed using OpenCV and Net 5
- Caffe Model Zoo (GoogleNet) detection from the π¦ camera feed using OpenCV and Net5
- Pose detection from the π¦ camera feed using OpenCV and Net5
- Packaging a WinForm OpenCV and Net5 App in a one-self contained file
- Display a video file π₯ in Winform using OpenCV and Net5
- Open a video file π₯ and save each frame as a PNG πΌ file to a folderΒ π
- (Coming Soon) QR Codes detection on the π¦ camera feed using OpenCV and Net 5