Home 9 The Art of Technology 9 Image Processing : Python and OpenCV : Capture Image From Webcam

Capture From Webcam – Using and OpenCV.

Let’s take a quick look on how we can use OpenCV2 to capture a single frame from a webcam.

The code below uses a web cam to capture a single frame, that stores the results in the image variable. We are using direct show to communicate with the cam and adjust the captured image size. Once an image is capture it will be shown, to close the window press any key.

#%%
import cv2

#define which USB Camera
cam_port = 0

# define a video capture object with direct show
cam = cv2.VideoCapture(cam_port,cv2.CAP_DSHOW)

# set new dimensions to cam object
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)


# reading the input using the camera
result, image = cam.read()

# Result will be true if a image is successfully captured.
if result:
    # showing image

    cv2.imshow("YourImage", image)

    # If keyboard interrupt occurs, destroy image window
    cv2.waitKey(0)
    cv2.destroyWindow("YourImage")

See the Image Processing Table of Contents



Have a Creative or Technical Project in Mind?

Looking for guidance, insights, or a fresh perspective on your technical or creative journey? Or just somebody to chat with?


LET’S KEEP IN TOUCH!

Get notified when I post something new!

We don’t spam! Read our privacy policy for more info.


Pin It on Pinterest

Share This