This code creates displays a video feed in a window.
This code is based on this tutorial from the OpenCV documentation. This code was tested on Windows 10, with version 4.0.0 of OpenCV. The Python version used was 3.7.7. Python and OpenCV were installed as part of Anaconda.
The code
Start by importing the cv2 module, which will make available all the functionality we need to capture video from a camera.
1 | import cv2 |
Then, to obtain video from a camera, we need to create an object of class VideoCapture. As input, the constructor of this class receives the index of the device we want to use. If we just have a single camera connected to the computer, we can simply pass the value 0. I have a software virtual camera, for OBS Studio, at index 0, so to use the integrated webcam of my laptop I use an index of 1.
1 | capture = cv2.VideoCapture(1 ) |
This provides a handle to read the video from the camera frame by frame. This is achieved by calling the read method on or VideoCapture object.
This method takes no arguments and returns a tuple. The first returned value is a Boolean indicating if the frame was read correctly (True) or not (False).
We can use this value for error checking if we want to confirm no problem occurred while reading the frame. The second value returned by the read method is a numpy ndarray object representing the captured frame.
1 | ret, frame = capture.read() |
To display the captured frame call the imshow function of the cv2 module. This function will open a window and display the frame.
The first method argument is the window name, as a string. The second argument is the image to display.
1 | cv2.imshow( 'video' , frame) |
To continuously read and display the frames, we need to repeat the two method calls. Note that when imshow is called multiple times for the same window, the new image overrides the existing one.
To avoid an infinite loop the waitKey function from cv2 can be used to check if a given key was pressed and the loop exited.
waitKey takes a delay argument in milliseconds to wait until a key is pressed. Since this is a blocking function and the code is displaying video the delay needs to be short. A value of 1 millisecond works.
Note that the waitKey function returns the ASCII code of the pressed key. The code assumes the ESC key is used to exit the loop. That key corresponds to the number 27.
1 2 | if cv2.waitKey( 1 ) = = 27 : break |
The full loop can be seen below.
1 2 3 4 5 6 7 8 | while ( True ): ret, frame = capture.read() cv2.imshow( 'video' , frame) if cv2.waitKey( 1 ) = = 27 : break |
After the loop breaks, we no longer need to access the video, so we should release it with a call to the release method on the VideoCapture object.
1 | capture.release() |
To finalize, we will call the destroyAllWindows function of the cv2 module, to destroy the window we have created to show the frames of the camera.
1 | cv2.destroyAllWindows() |
The final code can be seen below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import cv2 capture = cv2.VideoCapture(1 ) while ( True ): ret, frame = capture.read() cv2.imshow( 'video' , frame) if cv2.waitKey( 1 ) = = 27 : break capture.release() cv2.destroyAllWindows() |
To test the code, run the script making sure you have a camera attached. You should see a result similar to figure 1, which shows the window where the frames of the camera are being displayed.
After you click the ESC key the window should close and the script complete execution.