opencv learning note Day02: read video stream

Keywords: encoding Attribute codec

Article directory

Read video stream

cv2.getcapture(), use the function cap.get(propId) to get some parameter information of the video. Here the propId can be any integer between 0 and 18. Each number represents an attribute of the video:

  • cv2.VideoCapture.get(0) the current location (playback) of the video file in milliseconds
  • cv2.VideoCapture.get(1) based on the captured or decoded frame index starting with 0
  • cv2.VideoCapture.get(2) relative position of video file (play): 0 = beginning of movie, 1 = end of movie.
  • cv2.VideoCapture.get(3) the width of the frame in the video stream
  • cv2.VideoCapture.get(4) the height of the frame in the video stream
  • cv2.VideoCapture.get(5) frame rate
  • cv2.VideoCapture.get(6) codec 4-character code
  • cv2.VideoCapture.get(7) frames in video file
  • cv2.VideoCapture.get(8) returns the format of the object
  • cv2.VideoCapture.get(9) returns a backend specific value indicating the current capture mode
  • cv2.VideoCapture.get(10) brightness of image (camera only)
  • cv2.VideoCapture.get(11) image contrast (camera only)
  • cv2.VideoCapture.get(12) image saturation (camera only)
  • cv2.VideoCapture.get(13) tone image (camera only)
  • cv2.VideoCapture.get(14) image Gain (camera only) (Gain indicates white balance improvement in photography)
  • cv2.VideoCapture.get(15) exposure (camera only)
  • cv2.VideoCapture.get(16) indicates whether an image should be converted to an RGB boolean flag
  • cv2.VideoCapture.get(17) × not supported temporarily
  • cv2.VideoCapture.get(18) stereo camera correction annotation (currently only DC1394 v.2.x back-end supports this function)
"""capture video from camera"""
import numpy as np
import cv2

# create a VideoCapture object

# create a video object
# FourCC is a 4-byte code that determines the encoding format of the video

# create a videowriter object 
# element:1>filename 2>specify FourCC encoding 3,4>play frequency and frame 5>isColor label,True is color map  

    # Capture frame by frame
    if ret==True:
        # flip image
        # write next video frame

        # display the resulting frame
        if cv2.waitKey(1) & 0xFF == ord('q'):
    #    break
# release the capture

run, you can see your appearance in the screen. It's a gray image. You can only finish the current program after you click video with the mouse and type q with the key.

Screen insert text

"""read video"""
# encoding: utf-8
import cv2

# Declare a VideoCapture object. Its parameters can be the index number of the device, or a video file.
# The device index number is used to specify the camera to be used.
# Most laptops have built-in cameras. So the parameter is 0. You can choose another camera by setting it to 1 or something else.
# After that, you can capture the video frame by frame.
# Sometimes cap may fail to initialize the camera device successfully. In this case, the above code will report an error. You can use cap.isOpened(),
# To check if the initialization was successful. If the return value is True, there is no problem. Otherwise, use the function
cap = cv2.VideoCapture(0)

# cap.get(propId)
# You can use the function cap.get(propId) to get some parameter information of the video. Here the propId can be any integer between 0 and 18. Each number represents an attribute of the video
# cap.set(propId,value)
# Some of these values can be modified by cap.set(propId,value), which is the new value you want to set.

# Note: when your program reports an error, check whether the initialization is successful
# cap.isOpened()
# call the open method if initialization is not successful
while (cap.isOpened()):
# while (True):   # Capture frame by frame
    # # Returns two values, one Boolean (True/False) and one ndarray.
    # True if the frame reads correctly.
    # So finally you can check whether the video file has reached the end by checking its return value.

    # ndarray is to grab, decode and return to the next video frame
    # < class' numpy. Ndarray '> unable to grab and return to None
    ret,frame =
    # Test output

    # Here we operate on each frame
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #  Display the resulting frame

    # Font settings
    img=cv2.putText(gray, 'video', (10, 180), font, 5, (255, 255, 255), 20)
    if cv2.waitKey(1) & 0xFF == ord('q'):

#  When everything done,release the capture

The function of opening the camera requires the reader to try it by himself. Here is not the picture above... Most of the methods and parameters of the function have been annotated. You can take a look at them first..

Published 29 original articles, won praise 4, visited 868
Private letter follow

Posted by werkkrew on Sat, 07 Mar 2020 07:13:13 -0800