In this OpenCV Tutorial, you’ll learn how to capture video from a camera in OpenCV Python. If you want to capture video from your laptop camera then use this cv2.VideoCapture(0). I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn computer vision.
# import OpenCV module
import cv2
# Create a VideoCapture object
cap = cv2.VideoCapture(0) # 0 is used for primary camera and 1 is used for external camera
# Check Webcam is opened correctly
if not cap.isOpened():
raise IOError("webcam error")
# Read the frame
while True:
ret, frame = cap.read()
cv2.imshow('Captured Video', frame)
# Press esc to exit
c = cv2.waitKey(1)
if c == 27:
break
# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()

