Monday, July 21, 2025
HomeLanguagesPython OpenCV: Capture Video from Camera

Python OpenCV: Capture Video from Camera

Python provides various libraries for image and video processing. One of them is OpenCV. OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can capture a video from the camera. It lets you create a video capture object which is helpful to capture videos through webcam and then you may perform desired operations on that video.

Steps to capture a video:

  • Use cv2.VideoCapture() to get a video capture object for the camera.
  • Set up an infinite while loop and use the read() method to read the frames using the above created object.
  • Use cv2.imshow() method to show the frames in the video.
  • Breaks the loop when the user clicks a specific key.

Below is the implementation.




# import the opencv library
import cv2
  
  
# define a video capture object
vid = cv2.VideoCapture(0)
  
while(True):
      
    # Capture the video frame
    # by frame
    ret, frame = vid.read()
  
    # Display the resulting frame
    cv2.imshow('frame', frame)
      
    # the 'q' button is set as the
    # quitting button you may use any
    # desired button of your choice
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()


Output:

RELATED ARTICLES

Most Popular

Dominic
32157 POSTS0 COMMENTS
Milvus
67 POSTS0 COMMENTS
Nango Kala
6530 POSTS0 COMMENTS
Nicole Veronica
11678 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11740 POSTS0 COMMENTS
Shaida Kate Naidoo
6625 POSTS0 COMMENTS
Ted Musemwa
6907 POSTS0 COMMENTS
Thapelo Manthata
6592 POSTS0 COMMENTS
Umr Jansen
6587 POSTS0 COMMENTS