The code has been uploaded to:/tqbx/python-opencv/tree/master/Getting_started_videos
goal
Learn to read video, play video, and save video.
Learn to capture frames from the camera and display them.
Learning about (), () usage
Capture video from the camera
Capture the video with its own camera and convert it to grayscale video for display.
The basic steps are as follows:
1. First create a VideoCapture object which contains two kinds of parameters:
- Device Index, which specifies the number of the camera.
- The name of the video file.
2. Frame-by-frame capture.
3. Release of traps.
import numpy as np import cv2 as cv cap = (0) if not (): print("Cannot open camera") exit() while True: # Capture frame-by-frame ret, frame = () # if frame is read correctly ret is True if not ret: print("Can't receive frame (stream end?). Exiting ...") break # Our operations on the frame come here gray = (frame, cv.COLOR_BGR2GRAY) # Display the resulting frame ('frame', gray) if (1) == ord('q'): break # When everything done, release the capture () ()
Other:
-
()
Returns a boolean value, True if frame is read correctly, which can be used to determine if the video has ended. - Sometimes, cap may fail to initialize a capture, which can be done with the
()
to check if it's initialized, if it's True then that's best, if not you can use the()
to try to open it. - Of course, you can use the
(propId)
The way to get some properties of the video, such as the width of the frame, the height of the frame, the speed of the frame, etc. propId is a number from 0 to 18, each number represents a property, the corresponding relationship is shown in the bottom appendix. - Since you can get it, you can of course also try to set it, assuming you want to set the width and height of the frame to 320 and 240:
(3,320), (4,240)
。
Playing a video from a file
Code and capture video from the camera is basically the same, the difference is that the parameter passed into VideoCapture, at this time, pass the name of the video file.
When displaying each frame, you can use the()
Set the proper time, if the value is small, the video will be fast. Normally, 25ms is ok.
import numpy as np import cv2 cap = ('') while(()): ret, frame = () gray = (frame, cv2.COLOR_BGR2GRAY) ('frame',gray) if (1) & 0xFF == ord('q'): break () ()
Save Video
1. Create a VideoWriter object with the following parameters:
- The filename of the output, e.g.
- FourCC code。
- Frames per second fps.
- The size of the frame.
Code is passed in two ways:
- fourcc = cv2.VideoWriter_fourcc(*'XVID')
- fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
is a 4-byte code for specifying a video codec.
- In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
- In Windows: DIVX (More to be tested and added)
- In OSX : (I don't have access to OSX. Can some one fill this?)
import numpy as np import cv2 cap = (0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = ('',fourcc, 20.0, (640,480)) while(()): ret, frame = () if ret==True: frame = (frame,0) # write the flipped frame (frame) ('frame',frame) if (1) & 0xFF == ord('q'): break else: break # Release everything if job is finished () () ()
appendice
- CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
- CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
- CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
- CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
- CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
- CV_CAP_PROP_FPS Frame rate.
- CV_CAP_PROP_FOURCC 4-character code of codec.
- CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
- CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
- CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
- CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
- CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
- CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
- CV_CAP_PROP_HUE Hue of the image (only for cameras).
- CV_CAP_PROP_GAIN Gain of the image (only for cameras).
- CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
- CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
- CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v backend currently)
- CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v backend currently)
- CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v backend currently)
- CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v backend currently)
- CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v backend currently)
bibliography
Getting Started with Videos
Author: tianqiao basha丶
Provenance:/summerday152/
This article has been included in Gitee:/tqbx/JavaBlog
If you are interested, you can come and visit my personal mini-site:
Above is the detailed content of python using opencv to save and play video, more information about python opencv please pay attention to my other related articles!