O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading a video

Reading a video using OpenCV is really simple. You do not have to worry about different formats (mp4, avi, and more) as OpenCV does all the heavy lifting for you. It provides different ways to read a video. You can read a live video using your webcam or use an external USB camera or read a saved video on your computer using an object of the VideoCapture module. The VideoCapture() constructor takes either an integer or the name of a file. The integer argument is the ID of the camera attached to the computer. Let's look at how to capture a video using a webcam. A webcam has the ID 0; therefore, to read a video from your webcam, pass 0 to VideoCapture(). All other USB cameras will have IDs starting from 1:

import cv2cam = cv2.VideoCapture(0) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required