Using the OpenCV library, we can access, process and display video input
from webcams and files. For this tutorial you must first have the
installed.
Capturing webcam or video footage with OpenCV
With the OpenCV library, we can either capture footage from a camera feed or a video file, for each we can use the VideoCapture() function which will return an object which we can use to capture video.
If you want to use the camera feed you must provide an integer referencing the ID of the camera, if you have only 1 camera attached just give 0, this can also be used to select the default camera.
input = cv2.VideoCapture( 0 )
If you want to use a video file then you can provide the location instead.
input = cv2.VideoCapture( "vid.mp4" )
Once you've specified the video input, you can create the loop that will capture each frame, to acquire a frame we can use the read() function which will return True if successful as well as the requested frame.
while True:
ret, frame = input.read()
The
ret
variable should hold True if the capture was successful, the
frame
variable will then also hold the captured frame which we can then display or process depending on your needs.
Displaying webcam or video footage once captured
Once we've acquired the frame with the read() function, we can then create a window in which to display the frame with the imshow() function. This function takes 2 arguments, the title of the window to display it in (which you can set to whatever you like) and the frame to display within that window.
cv2.imshow( "video output", frame )
This function should be placed within the loop used to capture the frame as it will need to be reset every time a new frame is read.
Along with capturing and displaying the frame as shown above, we also need a way to stop the video, for this we can use a condition that checks whether a certain button is being pressed as well as the key that pauses the video.
Without this the video will not display correctly
.
if cv2.waitKey(1) & 0xFF == ord( 'x' ):
break
This will halt the loop once the "x" button has been pressed on the keyboard.
Putting it all together
Here we will combine the above code into a program that reads and displays frames received from a webcam input.
import cv2
input = cv2.VideoCapture( 0 )
while True:
ret, frame = input.read()
if ret:
cv2.imshow( "video output", frame )
if cv2.waitKey(1) & 0xFF == ord( 'x' ):
break
input.release()
cv2.destroyAllWindows()
In this example we first import the OpenCV package "cv2".
Then we capture the default camera device with the VideoCapture function with 0 as an argument.
We then create the loop which will capture each frame with the read() function.
Using the
ret
variable to ensure the frame was read successfully, we then display the frame in a new window using the imshow() function.
Lastly, we create the condition that allows for a way to stop the video loop.
As you can see in the last 2 lines, you can also include the functions release() and destroyAllWindows() in order to free up memory and ensure OpenCV can be used properly later in your code. If you don't need to include more after the loop has ended then these can be omitted.