In this tutorial, we will see how to read a video and start a webcam using python open-cv, which exists as cv2 (computer vision) library.
[toc]
You can use VideoCapture() method of cv2 library to read and start live streaming. To use cv2 library, you need to import cv2 library using import statement.
Now let’s see the syntax and return value of cv2 canny() method first, then we will move on to the examples.
Syntax
cv2.VideoCapture(video_path or device index )
Parameters
There is one argument which needs to pass in VideoCapture() class.
video_path: Locationvideo in your system in string form with their extensions like .mp4, .avi, etc.
OR
device index: It is just the number to specify the camera. Its possible values ie either 0 or -1.
Return value
Video capture object.
cv2 VideoCapture() method examples
Now Let’s see the Python code :
Example 1: Run a given video file.
# 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..")
Output :

Example 2: Start a webcam.
# 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..")
Output:

That’s all about Python cv2 VideoCapture() method,