Python opencv capture read

Reading and Writing Videos using OpenCV

Reading and writing videos in OpenCV is very similar to reading and writing images. A video is nothing but a series of images that are often referred to as frames. So, all you need to do is loop over all the frames in a video sequence, and then process one frame at a time. In this post, we will demonstrate how to read, display and write videos from a file, an image sequence and a webcam. We will also look into some of the errors that might occur in the process, and help understand how to resolve them.

Let’s go through the code example for reading a video file first.This essentially contains the functions for reading a video from disk and displaying it. As you proceed further, we will discuss the functions in detail used in this implementation.

Python

import cv2 # Create a video capture object, in this case we are reading the video from a file vid_capture = cv2.VideoCapture('Resources/Cars.mp4') if (vid_capture.isOpened() == False): print("Error opening the video file") # Read fps and frame count else: # Get frame rate information # You can replace 5 with CAP_PROP_FPS as well, they are enumerations fps = vid_capture.get(5) print('Frames per second : ', fps,'FPS') # Get frame count # You can replace 7 with CAP_PROP_FRAME_COUNT as well, they are enumerations frame_count = vid_capture.get(7) print('Frame count : ', frame_count) while(vid_capture.isOpened()): # vid_capture.read() methods returns a tuple, first element is a bool # and the second is frame ret, frame = vid_capture.read() if ret == True: cv2.imshow('Frame',frame) # 20 is in milliseconds, try to increase the value, say 50 and observe key = cv2.waitKey(20) if key == ord('q'): break else: break # Release the video capture object vid_capture.release() cv2.destroyAllWindows()

C++

// Include Libraries #include #include // Namespace to nullify use of cv::function(); syntax using namespace std; using namespace cv; int main() < // initialize a video capture object VideoCapture vid_capture("Resources/Cars.mp4"); // Print error message if the stream is invalid if (!vid_capture.isOpened()) < cout else < // Obtain fps and frame count by get() method and print // You can replace 5 with CAP_PROP_FPS as well, they are enumerations int fps = vid_capture.get(5); cout // Read the frames to the last frame while (vid_capture.isOpened()) < // Initialise frame matrix Mat frame; // Initialize a boolean to check if frames are there or not bool isSuccess = vid_capture.read(frame); // If frames are present, show it if(isSuccess == true) < //display frames imshow("Frame", frame); >// If frames are not there, close it if (isSuccess == false) < cout //wait 20 ms between successive frames and break the loop if key q is pressed int key = waitKey(20); if (key == 'q') < cout > // Release the video capture object vid_capture.release(); destroyAllWindows(); return 0; >

Источник

Читайте также:  Learn to program using python

Python opencv capture read

Often, we have to capture live stream with a camera. OpenCV provides a very simple interface to do this. Let’s capture a video from the camera (I am using the built-in webcam on my laptop), convert it into grayscale video and display it. Just a simple task to get started.

To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. A device index is just the number to specify which camera. Normally one camera will be connected (as in my case). So I simply pass 0 (or -1). You can select the second camera by passing 1 and so on. After that, you can capture frame-by-frame. But at the end, don’t forget to release the capture.

cap.read() returns a bool ( True / False ). If the frame is read correctly, it will be True . So you can check for the end of the video by checking this returned value.

Sometimes, cap may not have initialized the capture. In that case, this code shows an error. You can check whether it is initialized or not by the method cap.isOpened(). If it is True , OK. Otherwise open it using cap.open().

You can also access some of the features of this video using cap.get(propId) method where propId is a number from 0 to 18. Each number denotes a property of the video (if it is applicable to that video). Full details can be seen here: cv::VideoCapture::get(). Some of these values can be modified using cap.set(propId, value). Value is the new value you want.

Читайте также:  How functions work in javascript

For example, I can check the frame width and height by cap.get(cv.CAP_PROP_FRAME_WIDTH) and cap.get(cv.CAP_PROP_FRAME_HEIGHT) . It gives me 640×480 by default. But I want to modify it to 320×240. Just use ret = cap.set(cv.CAP_PROP_FRAME_WIDTH,320) and ret = cap.set(cv.CAP_PROP_FRAME_HEIGHT,240) .

Note If you are getting an error, make sure your camera is working fine using any other camera application (like Cheese in Linux).

Playing Video from file

Playing video from file is the same as capturing it from camera, just change the camera index to a video file name. Also while displaying the frame, use appropriate time for cv.waitKey() . If it is too less, video will be very fast and if it is too high, video will be slow (Well, that is how you can display videos in slow motion). 25 milliseconds will be OK in normal cases.

Источник

Оцените статью