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

Capture Image From Webcam – Using Python 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 Project or Idea!?

I am Available for Freelance Projects

I’m always ready to take on new opportunities and eager to connect with like-minded people.

If you’re interested in working together, connecting, or just having a good conversation, send me an email. I look forward to hearing from you!

jamie@jamiestarling.com


Pin It on Pinterest

Share This