This is the 11th day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article shows how to use OpenCV in Python3 to read and display video files:

above

What this paper intends to achieve is to use the built-in camera to capture the video and display every frame of the video to achieve the video playback function.

  • Create the camera object with the path pointing to the video file.
  • Frame by frame display to achieve video playback.

Creating a Camera Object

Opencv’s VideoCapture() function defines the camera object. As follows:

video = cv2.VideoCapture('E:\\demo\\0603\\1.mp4')
Copy the code

Frame by frame display to achieve video playback

In the while loop, the video frames are read and displayed in turn using the read() function of the video object, and then wait for 1 unit of time. If the keyboard input L is detected during this time, exit, that is, close the window.

while(1):
    # get a frame
    ret, frame = video.read()
    # show a frame
    cv2.imshow("capture", frame)
    if cv2.waitKey(100) & 0xFF == ord('L') :Copy the code

Release the camera object and window

Call Release () to release the camera, and call destroyAllWindows() to close all image Windows.

video.release()
cv2.destroyAllWindows()
Copy the code

Complete program code

import cv2
import numpy as np

video = cv2.VideoCapture('E:\\demo\\0603\\1.mp4')
while(1):
    # get a frame
    ret, frame = video.read()
    # show a frame
    cv2.imshow("capture", frame)
    if cv2.waitKey(100) & 0xFF == ord('L') :break
video.release()
cv2.destroyAllWindows()
Copy the code

The output is:

A series of articles will follow this month,

Wonderful article, please pay attention.