
- OpenCV Python Tutorial
- OpenCV Python - Home
- OpenCV Python - Overview
- OpenCV Python - Environment
- OpenCV Python - Reading Image
- OpenCV Python - Write Image
- OpenCV Python - Using Matplotlib
- OpenCV Python - Image Properties
- OpenCV Python - Bitwise Operations
- OpenCV Python - Shapes and Text
- OpenCV Python - Mouse Events
- OpenCV Python - Add Trackbar
- OpenCV Python - Resize and Rotate
- OpenCV Python - Image Threshold
- OpenCV Python - Image Filtering
- OpenCV Python - Edge Detection
- OpenCV Python - Histogram
- OpenCV Python - Color Spaces
- OpenCV Python - Transformations
- OpenCV Python - Image Contours
- OpenCV Python - Template Matching
- OpenCV Python - Image Pyramids
- OpenCV Python - Image Addition
- OpenCV Python - Image Blending
- OpenCV Python - Fourier Transform
- OpenCV Python - Capture Videos
- OpenCV Python - Play Videos
- OpenCV Python - Images From Video
- OpenCV Python - Video from Images
- OpenCV Python - Face Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Feature Detection
- OpenCV Python - Feature Matching
- OpenCV Python - Digit Recognition
- OpenCV Python Resources
- OpenCV Python - Quick Guide
- OpenCV Python - Resources
- OpenCV Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
OpenCV Python - Capture Video from Camera
By using the VideoCapture() function in OpenCV library, it is very easy to capture a live 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 live 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 finalize the creation of video.
Example
Following example reads live 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()