Learn OpenCV (Python): Basic Video Operations

System information:

  • Ubuntu: 16.04
  • Opencv: 3.1.0

To work with video in opencv, you just only need to care about these two attributes:

  • cv2.VideoCapture(video_path)
  • cv2.VideoWriter(video_path, codec, fps, size, is_color)

About codec, depends on operating system you have, choose the appropriate one:

  • In Fedora (Linux): DIVX, XVID, MJPG, X264, WMV1, WMV2
  • In Windows: DIVX
  • In OS X: Dunno :V

Depends on what video codec you use, different file extension will be supported.
You can read more here .

  • Read Video
import numpy as np
import cv2 

ESCAPE_KEY = 27
# capture video
video_capture = cv2.VideoCapture('/home/loctv/Python/cv/data/pycv-master/first_edition/chapter2/miscellaneous/MyInputVid.avi')

# while video has not been released yet (or still opened)
while video_capture.isOpened():
    # read video frame by frame
    ret, frame = video_capture.read()
    # convert to gray image
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # display
    cv2.imshow("frame", gray)

    # wait to press 'ESC' key
    # 27 means every 27 ms, this if block will be called again
    # you can alter this to change FPS (Frame Per Second)
    if cv2.waitKey(27) == ESCAPE_KEY:
        break

# close video stream
video_capture.release()
# as it describes, destroy all windows has been opened by opencv
cv2.destroyAllWindows()
  • Write Video
import cv2

# 'capture' video
videoCapture = cv2.VideoCapture('MyInputVid.avi')
# set frame per second
fps = videoCapture.get(cv2.CAP_PROP_FPS)
# size of the copied video
size = (
    int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
    int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
)
# create video writer
# video codec = WMV1
# you can try others video codec
videoWriter = cv2.VideoWriter(
    'MyOutputVid.avi', cv2.VideoWriter_fourcc(*"WMV1"), fps, size
)

# read first frame
success, frame = videoCapture.read()
# loop until there is no more frame
while success:
    # write frame by frame
    videoWriter.write(frame)
    # read next frame
    success, frame = videoCapture.read()
  • Capture camera frames
import numpy as np
import cv2

ESCAPE_KEY = 27
# capture  frame from camera (webcam)
# 0 is the index to specify which camera you want to capture
# in this case, there only one camera
camera_capture = cv2.VideoCapture(0)

# # read the first frame
success, frame = camera_capture.read()
# while still more frame to read
while success:
    # display frame
    # if you want you can convert frame from BGR to gray-scale
    # for faster processing
    cv2.imshow("frame", frame)

    # 25 means this if block will be called every 25 ms
    # help the frames displayed slow enough to be processed
    # wait for 'ESC' key to be pressed
    # then program will be stoped
    if cv2.waitKey(25) == ESCAPE_KEY:
        break

    # read another frame
    success, frame = camera_capture.read()

# turn off webcam (by releasing capturer)
camera_capture.release()
cv2.destroyAllWindows()
  • Capture camera frames and writer to file
import numpy as np
import cv2

ESCAPE_KEY = 27
# capture  frame from camera (webcam)
# 0 is the index to specify which camera you want to capture
# in this case, there only one camera
camera_capture = cv2.VideoCapture(0)
# roughly estimate fps
fps = 25
# size of new video will be the same size with capture frames
size = (
    int(camera_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
    int(camera_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
)
# set video codec
# after trying several video codecs
# this one works for me
# if it does not work, let's try another one
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
# create video writer object
video_writer = cv2.VideoWriter(
    'CameraCapture.avi', fourcc, fps, size
)

# # read the first frame
success, frame = camera_capture.read()
# while still more frame to read
while success:
    # instead of displaying frame like we did before
    # write it to file
    video_writer.write(frame)
    cv2.imshow("frame", frame)

    # 25 means this if block will be called every 25 ms
    # help the frames displayed slow enough to be processed
    # wait for 'ESC' key to be pressed
    # then program will be stoped
    if cv2.waitKey(25) == ESCAPE_KEY:
        break

    # read another frame
    success, frame = camera_capture.read()

# turn off webcam (by releasing capturer)
camera_capture.release()
# and release video writer
# video_writer.release()
cv2.destroyAllWindows()

‘The number of camera and their order is system dependently, but opencv does not give us any way to query the number of cameras or their order. Fortunately, we can use VideoCapture‘s method isOpened() to check if we successfully connect to a real camera.

  • Work with multiple cameras:

In the examples above, you can use read() method of VideoCapture to read frames of one camera, but it cant be use for reading frame of multiple cameras. In that case, you have to use grab() and retrieve():
Suppose you already have two camera capture object for two camera

success_0 = camera_capture_0.grab()
success_1 = camera_capture_1.grab()
while success_0 and success_1:
    frame_0 = camera_capture_0.retrieve()
    frame_1 = camera_capture_1.retrieve()

Those are basic operations that opencv supports for video reading and writing.

 

Advertisements