Goal
• Learn to read video files, display videos, save video files
• Learn to get and display video from the camera
• You will learn these functions: Cv2. Videocapture (), Cv2. Videowrite ()
Capturing video with the camera
- Use the camera to capture a video and convert it into grayscale video.
- You should first create a Videocapture object, which can be the index number of the device, or a video file.
- The device index number is the camera that you specify to use. Typical laptops have built-in cameras. So the parameter is 0. You can choose a different camera by setting it to 1 or something else.
- After that, you can capture the video in one frame at a frame. But finally, don't forget to stop capturing the video.
On the code:
1 #-*-coding:utf-8-*-2 3 ImportNumPy as NP4 ImportCv25 6Cap = Cv2. Videocapture (0)#Create a Videocapture object7 while(True):8 9RET, frame = Cap.read ()#read a video frame at one frameTenGray = Cv2.cvtcolor (frame, cv2. Color_bgr2gray)#processing each frame, set to grayscale OneCv2.imshow ('Frame', Gray)#Show Results A ifCv2.waitkey (1) & 0xFF = = Ord ('Q'):#Press Q to stop - Break - theCap.release ()#release cap, destroy window -Cv2.destroyallwindows ()
View Code
- Run to see the camera open, and display a grayscale image, press Q to exit
Python_opencv_ Read Video _ Webcam