We will use a webcam in this chapter to capture video data. Let's see how to capture the video from the webcam using OpenCV-Python.
import cv2
0
input argument specifies the ID of the webcam. If you connect a USB camera, then it will have a different ID:# Initialize video capture object cap = cv2.VideoCapture(0)
# Define the image size scaling factor scaling_factor = 0.5
# Loop until you hit the Esc key while True: # Capture the current frame ret, frame = cap.read()
# Resize the frame frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
# Display the image cv2.imshow('Webcam', frame)
# Detect if the Esc key has been pressed c = cv2.waitKey(1) if c == 27: break
# Release the video capture object cap.release()
# Close all active windows cv2.destroyAllWindows()
video_capture.py
file that's already provided to you for reference. If you run this code, you will see the video from the webcam, similar to the following screenshot:3.145.17.46