Python | CV2视频捕获 VideoCapture()方法

时间:2020-02-23 14:42:15  来源:igfitidea点击:

在本教程中,我们将看到如何读取视频并使用Python Open-CV启动网络摄像头,该CV存在于CV2(计算机Vision)库。
我们可以使用 VideoCapture()的方法 cv2库读取并开始直播。
要使用CV2库,我们需要使用CV2库使用 import statement

现在让我们看看语法和返回值 cv2 canny()方法首先,我们将继续前进到示例。

语法

cv2.VideoCapture(video_path or device index )

参数

有一个参数需要通过videocapture()类。 video_path: Location在系统中的视频以字符串形式,它们的扩展如.mp4,.avi等。

或者 device index:它只是指定相机的数字。
其可能的值,即0或者-1.

返回值

视频捕获对象。

CV2视频捕获()方法示例

现在让我们看看Python代码:

示例1:运行给定的视频文件。

# import computer vision library(cv2) in this code
import cv2
 
# main code
if __name__ == "__main__" :
 
    # mentioning absolute path of the video
    video_path = "C:\Users\user\Desktop\test_video.mp4"
 
    # creating a video capture object
    video_object = cv2.VideoCapture(video_path)
 
    # handling errors
    # if any erros is occured during the running of the program
    # then it handles
    try :
        # run a infinite loop 
        while(True) :
 
            # read a video frame by frame
            # read() returns tuple in which 1st item is boolean value 
            # either True or False and 2nd item is frame of the video.
            # read() returns False when video is ended so 
            # no frame is readed and error will be generated.
            ret,frame = video_object.read()
            
            # show the frame on the newly created image window
            cv2.imshow('Frames',frame)
 
            # this condition is used to run a frames at the interval of 10 mili sec
            # and if in b/w the frame running , any one want to stop the execution .
            if cv2.waitKey(10) & 0xFF == ord('q') :
 
                # break out of the while loop
                break
 
    except :
        # if any error occurs then this block of code will run
        print("Video has ended..")

示例2:启动网络摄像头。

# import computer vision library(cv2) in this code
import cv2
 
# main code
if __name__ == "__main__" :
 
    # creating a video capture object
    # argument 0 , webcam start
    video_object = cv2.VideoCapture(0)
 
    # handling errors
    # if any erros is occured during the running of the program
    # then it handles
    try :
        # run a infinite loop 
        while(True) :
 
            # read a video frame by frame
            # read() returns tuple in which 1st item is boolean value 
            # either True or False and 2nd item is frame of the video.
            # read() returns False when live video is ended so 
            # no frame is readed and error will be generated.
            ret,frame = video_object.read()
            
            # show the frame on the newly created image window
            cv2.imshow('Frames',frame)
 
            # this condition is used to run a frames at the interval of 10 mili sec
            # and if in b/w the frame running , any one want to stop the execution .
            if cv2.waitKey(10) & 0xFF == ord('q') :
 
                # break out of the while loop
                break
 
    except :
        # if error occur then this block of code is run
        print("Video has ended..")