Thought leadership from the most innovative tech companies, all in one place.

Capture Video Footage from a Webcam using OpenCV Python

Python projects for beginners. Webcam access using python.

image

We all are using webcams, right? From the past 2 years, almost every geek comes across zoom meeting for once at least. Some of you might use a webcam to record videos or take photos. Somehow webcam is a part of daily life now for almost every student and corporate guy.

As a programming enthusiast, let's see how to capture and process a webcam video using python. Within a few lines of code, you can easily make that. So, let's start this project with a cup of coffee.

The video tutorial of this article is here:

What is OpenCV?

OpenCV is a python library. It stands for Open Source Computer Vision Library. It is designed to solve computer vision, machine learning, and image processing problems. Not only python, but it also supports Java, C++, and other programming languages. It is widely used to process images and videos to identify objects, faces, and even the handwriting of humans.

This was a brief introduction to the OpenCV library.

Capture webcam video using python

I recommend you to make this project in a virtual environment. It's a good habit to build a python based project.

Project Setup

  • Make a virtual environment. If you don't have then install it in your system. Open your terminal or command prompt window in any directory and type the command as shown below.
virtualenv -p python3.8 webcam
  • Navigate to the directory “webcam” and activate the virtual environment. The code for it is different for different OS. If you are using Linux then you can type the “source bin/activate” command in your terminal window to activate the virtual environment.
  • Install OpenCV.
pip install opencv-python

Code Part

  • Make a python file inside the virtual environment.
  • Create a Python file and import the required package.
  • OpenCV provides a video capture object that we can use to capture images from the webcam. The 0 input argument specifies the ID of the webcam. If you connect a USB camera, then it will have a different ID.
  • We have to define a scaling factor for frames that are captured using the webcam.
  • Start an infinite loop and keep capturing frames until you press the Esc key.
  • Resize the frame (Optional)
  • Display the frame
  • Wait for 1ms before capturing the next frame
  • Release the video captures
  • Close all active windows before exiting the code.

cam.py

import cv2#Initialize video capturecap = cv2.VideoCapture(0)#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()

Now, Run the python file.

image

Webcam access using Python

You will see something like this on-screen after you run the python file.

Well, that's it.

If this article sounds informative then make sure to follow and share it with your geek community.




Continue Learning