Opening Webcam using OpenCV

Shantam Sultania
2 min readMar 29, 2020

This is a quick tutorial on how to open a webcam using OpenCV.

What is OpenCV

OpenCV (Open source computer vision) is a library of programming functions aimed at real-time computer vision originally developed by intel. This is an Open source library and has a wide range of contributions to it.

So without wasting our precious time lets start

  1. first, we Import libraries OpenCV.
  2. Then we use VideoCapture(0) function to capture the feed of the webcam here 0indicates the default value of webcam.
  3. Now read the frame of the camera frame by frame here

ret is a boolean variable that checks if the camera is on or not

the frame is a variable in which all the camera feed is being saved

4. Here we close the loop and destroy all the windows and release the camera feed.

Source Code

import cv2

cam = cv2.VideoCapture(0)

while True:
check, frame = cam.read()

cv2.imshow('video', frame)

key = cv2.waitKey(1)
if key == 27:
break

cam.release()
cv2.destroyAllWindows()

--

--