OpenCV has a functionality to work with standard USB webcams. Let's take a look at an example to capture an image from a webcam:
import cv2 # initialize the camera cam = cv2.VideoCapture(0) ret, image = cam.read() if ret: cv2.imshow('SnapshotTest',image) cv2.waitKey(0) cv2.destroyWindow('SnapshotTest') cv2.imwrite('/home/pi/book/output/SnapshotTest.jpg',image) cam.release()
In the preceding code, cv2.VideoCapture()
creates a video capture object. The argument for it can either be a video device or a file. In this case, we are passing a device index, which is 0. If we have more cameras, then we can pass the appropriate device index based on what camera to choose. If you have one camera, just pass 0.
You can find out the number of cameras and associated device indexes using the following command:
ls -l /dev/video*
Once cam.read()
returns a Boolean value ret
and the frame
which is the image it captured. If the image capture is successful, then ret
will be True; otherwise, it will be False. The previously listed code will capture an image with the camera device, /dev/video0
, display it, and then save it. cam.release()
will release the device.
This code can be used with slight modifications to display live video stream from the webcam:
import cv2 cam = cv2.VideoCapture(0) print 'Default Resolution is ' + str(int(cam.get(3))) + 'x' + str(int(cam.get(4))) w=1024 h=768 cam.set(3,w) cam.set(4,h) print 'Now resolution is set to ' + str(w) + 'x' + str(h) while(True): # Capture frame-by-frame ret, frame = cam.read() # Display the resulting frame cv2.imshow('Video Test',frame) # Wait for Escape Key if cv2.waitKey(1) == 27 : break # When everything done, release the capture cam.release() cv2.destroyAllWindows()
You can access the features of the video device with cam.get(propertyID)
. 3
stands for the width and 4
stands for the height. These properties can be set with cam.set(propertyID, value)
.
The preceding code first displays the default resolution and then sets it to 1024 x 768 and displays the live video stream till the Esc key is pressed. This is the basic skeleton logic for all the live video processing with OpenCV. We will make use of this in future.
We need to use the cv2.VideoWriter()
function to write a video to a file. Take a look at the following code:
import cv2 cam = cv2.VideoCapture(0) output = cv2.VideoWriter('VideoStream.avi', cv2.cv.CV_FOURCC(*'WMV2'),40.0,(640,480)) while (cam.isOpened()): ret, frame = cam.read() if ret == True: output.write(frame) cv2.imshow('VideoStream', frame ) if cv2.waitKey(1) == 27 : break else: break cam.release() output.release() cv2.destroyAllWindows()
In the preceding code, cv2.VideoWriter()
accepts the following parameters:
cv2.cv.CV_FOURCC()
function for this. This function accepts FourCC in the *'code' format. This means that for DIVX, we need to pass *'DIVX', and so on. Some supported formats are DIVX, XVID, H264, MJPG, WMV1, and WMV2.You can read more about FourCC at www.fourcc.org.
The preceding code records the video till the Esc key is pressed and saves it in the specified file.
The following code demonstrates the use of Picamera with OpenCV. It shows a preview for 3 seconds, captures an image, and displays it on screen using cv2.imshow()
:
import picamera import picamera.array import time import cv2 with picamera.PiCamera() as camera: rawCap=picamera.array.PiRGBArray(camera) camera.start_preview() time.sleep(3) camera.capture(rawCap,format="bgr") image=rawCap.array cv2.imshow("Test",image) cv2.waitKey(0) cv2.destroyAllWindows()