- OpenCV Python - Digit Recognition
- OpenCV Python - Feature Matching
- OpenCV Python - Feature Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Face Detection
- OpenCV Python - Video from Images
- OpenCV Python - Images From Video
- OpenCV Python - Play Videos
- OpenCV Python - Capture Videos
- OpenCV Python - Fourier Transform
- OpenCV Python - Image Blending
- OpenCV Python - Image Addition
- OpenCV Python - Image Pyramids
- OpenCV Python - Template Matching
- OpenCV Python - Image Contours
- OpenCV Python - Transformations
- OpenCV Python - Color Spaces
- OpenCV Python - Histogram
- OpenCV Python - Edge Detection
- OpenCV Python - Image Filtering
- OpenCV Python - Image Threshold
- OpenCV Python - Resize and Rotate
- OpenCV Python - Add Trackbar
- OpenCV Python - Mouse Events
- OpenCV Python - Shapes and Text
- OpenCV Python - Bitwise Operations
- OpenCV Python - Image Properties
- OpenCV Python - Using Matplotlib
- OpenCV Python - Write Image
- OpenCV Python - Reading Image
- OpenCV Python - Environment
- OpenCV Python - Overview
- OpenCV Python - Home
OpenCV Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
OpenCV Python - Capture Video from Camera
By using the VideoCapture() function in OpenCV pbrary, it is very easy to capture a pve stream from a camera on the OpenCV window.
This function needs a device index as the parameter. Your computer may have multiple cameras attached. They are enumerated by an index starting from 0 for built-in webcam. The function returns a VideoCapture object
cam = cv.VideoCapture(0)
After the camera is opened, we can read successive frames from it with the help of read() function
ret,frame = cam.read()
The read() function reads the next available frame and a return value (True/False). This frame is now rendered in desired color space with the cvtColor() function and displayed on the OpenCV window.
img = cv.cvtColor(frame, cv.COLOR_BGR2RGB) # Display the resulting frame cv.imshow( frame , img)
To capture the current frame to an image file, you can use imwrite() function.
cv2.imwrite(“capture.png”, img)
To save the pve stream from camera to a video file, OpenCV provides a VideoWriter() function.
cv.VideoWriter( filename, fourcc, fps, frameSize)
The fourcc parameter is a standardized code for video codecs. OpenCV supports various codecs such as DIVX, XVID, MJPG, X264 etc. The fps anf framesize parameters depend on the video capture device.
The VideoWriter() function returns a VideoWrite stream object, to which the grabbed frames are successively written in a loop. Finally, release the frame and VideoWriter objects to finapze the creation of video.
Example
Following example reads pve feed from built-in webcam and saves it to ouput.avi file.
import cv2 as cv cam = cv.VideoCapture(0) cc = cv.VideoWriter_fourcc(* XVID ) file = cv.VideoWriter( output.avi , cc, 15.0, (640, 480)) if not cam.isOpened(): print("error opening camera") exit() while True: # Capture frame-by-frame ret, frame = cam.read() # if frame is read correctly ret is True if not ret: print("error in retrieving frame") break img = cv.cvtColor(frame, cv.COLOR_BGR2RGB) cv.imshow( frame , img) file.write(img) if cv.waitKey(1) == ord( q ): break cam.release() file.release() cv.destroyAllWindows()Advertisements