Select Page

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 Project or Idea!?

I am Available for Freelance Projects

My skills are always primed and ready for new opportunities to be put to work, and I am ever on the lookout to connect with individuals who share a similar mindset.

If you’re intrigued and wish to collaborate, connect, or simply indulge in a stimulating conversation, don’t hesitate! Drop me an email and let’s begin our journey. I eagerly anticipate our interaction!

jamie@jamiestarling.com


Pin It on Pinterest

Share This