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
Hire Me – I am Available for Freelance Projects
I am always looking for opportunities to put my technical skills to work.
I am always looking to connect with like minded people.
Want to connect? – Drop me a message