How to Capture Images and Videos With Computer Webcam Using OpenCV in Python ? | Part 2
In the previous post we saw how to capture images and videos in python with the help of VideoCapture() available in OpenCV. But, we left it incomplete, because we only captured the first frame which the Webcam was able to read within the sleep duration. In this post, we will understand how to capture a video rather than only a single frame.
First things first, we will have to import the OpenCV module.
import cv2
import cv2
video=cv2.VideoCapture(0)
#the above line will trigger the webcam for recording the video
video.release()
Next, we will introduce a counter variable that will help us counting the total number of frames that we are going to read in the duration when the webcam is active. Off course, all this proceeding section of code will be written before actually releasing the camera.
import cv2
video=cv2.VideoCapture(0)
a=1
Now, as discussed earlier to read all the subsequent frames one after the other we will make use of the while loop, and the condition will be such that until and unless python is able to read the video fro the device the statements in the loop will keep on executing.
import cv2
video=cv2.VideoCapture(0)
a=1
while True:
a=a+1
check,frame=video.read()
With every frame being read in the loop we increment the count of the number of frames, we all know that check will return True or False depending on whether python is able to read the video from the webcam correctly or not. We can also go ahead and print all the frames that are being read in the loop so that we will come to know the 3D-matrix representation of all the frames in the video.
print(frame)
Next, we have to create a window to display the video being captured and for creating a window we can make use of the cv2.imshow() method.
cv2.imshow("Capturing",frame)
In the last post we saw the use of sleep() method in order to allow camera to capture for 3 seconds, now in case of videos frames appear consequently one after the other. Therefore, in order to be able to capture all those frames in the order they appear, we will make use of the waitKey() method
key=cv2.waitKey(1)
#This will cause the frame to appear for 1ms and then the new frame will be generated.
Ultimately, because of this waitKey(1) a new frame is being processed after every 1ms. Now, unlike the last time when we manually mentioned the time amount for which we wanted the camera to be active here there is no such option. We will have to introduce some code in order to close that window
if (key == ord('q')):
break
What this ord() method does is that it takes a string of length 1 as arguement and returns the unicode character corresponding to that single length string. So in this scenario, if you press the key 'q' on your system it is an indication that OK, we do not want to read video any further and this window should now be closed. After that in order to close the window displaying the video we will invoke the destroyAllWindows() method and finally we will invoke the release() method so that the camera object is released.
Complete Code To Capture Video Using OpenCV-Python
import cv2
video=cv2.VideoCapture(0)
a=1
while True:
a=a+1
check,frame=video.read()
print(frame)
cv2.imshow("Capturing",frame)
key=cv2.waitKey(1)
if(key==ord('q')):
break
video.release()
cv2.destroyAllWindows()
Wow, What an Excellent post. I really found this to much informative. It is what I was searching for. I would like to suggest you that please keep sharing such type of info.Machine learning courses for beginners
ReplyDeleteYou have given great content here.computer vision syllabus I am glad to discover this post as I found lots of valuable data in your article. Thanks for sharing an article like this.
ReplyDelete