Use AForge. NET for video collection and aforge.net for video collection
AForge. NET is designed based on C # And has powerful functions in computer vision and AI. Btw... it's an open source framework. Attached to the official website address: http://www.aforgenet.com/aforge/framework.
Today we will introduce the Video Acquisition Function in AForge. The video here includes inputs from cameras and other devices and inputs from video files.
First, let's take a look at the video source PLAYER: VideoSourcePlayer. Videos input from cameras and files are played through it and Bitmap data is output by Frame.
VideoSourcePlayer has the following important steps:
- Initialize it and set the VideoSource attribute. VideoSource accepts parameters of the IVideoSource type, which correspond to the camera input and file input. We will set them to VideoCaptureDevice and FileVideoSource respectively.
- Register the NewFrame event and start playing. Process Bitmap of each frame in the event registered with NewFrame.
- After processing, cancel the NewFrame event registration and stop it. Use SignalToStop (); and WaitForStop ();
The entire process is very simple. Let's take a look at the camera input and file input code:
1. Camera Input
The first is initialization and start:
// Get the video input device list FilterInfoCollection devices = new FilterInfoCollection (FilterCategory. videoInputDevice); // obtain the first video device (the sample code does not process the number of devices is 0) VideoCaptureDevice source = new VideoCaptureDevice (devices [0]. monikerString); // set the Frame size and ratesource. desiredFrameSize = new Size (640,360); source. desiredFrameRate = 1; // set VideoSourcePlayer's VideoSourceVideoSourcePlayer videoPlayer = new VideoSourcePlayer (); videoPlayer. videoSource = source; videoPlayer. newFrame + = videoPlayer_NewFrame; videoPlayer. start ();
Here is the NewFrame Event code:
private void videoPlayer_NewFrame(object sender, ref Bitmap image){ // do sth with image ...}
Code to stop after use:
videoPlayer.NewFrame -= videoPlayer_NewFrame;videoPlayer.SignalToStop();videoPlayer.WaitForStop();
2. file input
The first is initialization and start:
// The live video path file is used as the video source FileVideoSource videoSource = new FileVideoSource (videoFilePath); videoPlayer. VideoSource = videoSource; videoPlayer. NewFrame + = videoPlayer_NewFrame; videoPlayer. Start ();
The remaining two parts of the Code are the same as those entered by the camera.
For file input, note that codec on some machines is incomplete, which causes FileVideoSource to read some formats. For example, a read error occurs during mp4. In this case, you need to install a codec pack, you can.
Now, AForge. NET's video collection function is fully introduced. Next we will introduce some interesting functions in AForge.