Images are very important data nowadays from which we can obtain a lot of information which will helpful for analysis. Sometimes data to be processed can be in the form of video. To process this kind of data we have to read video extract frames and save them as images. Security camera videos we have seen that there is a timestamp and date at top of the frame. This kind of data also plays a crucial role in the analysis. So in this article, we are going to see how to extract frames from live video with timestamps and save them in the folder.
Python3
import cv2
import os
from datetime import datetime
path = r 'C:\Users\vishal\Documents\Bandicam'
os.chdir(path)
i = 1
wait = 0
video = cv2.VideoCapture( 0 )
while True :
ret, img = video.read()
font = cv2.FONT_HERSHEY_PLAIN
cv2.putText(img, str (datetime.now()), ( 20 , 40 ),
font, 2 , ( 255 , 255 , 255 ), 2 , cv2.LINE_AA)
cv2.imshow( 'live video' , img)
key = cv2.waitKey( 100 )
wait = wait + 100
if key = = ord ( 'q' ):
break
if wait = = 5000 :
filename = 'Frame_' + str (i) + '.jpg'
cv2.imwrite(filename, img)
i = i + 1
wait = 0
video.release()
cv2.destroyAllWindows()
|
Output:
Explanation:
- In the above program, the camera will open by giving 0 as input to cv2.VideoCapture() function (if you want to capture a frame from a video that is already recorded then you can give input as a path of that video to this function ).
- After that, we have written a loop that will run continuously until the user press any key on the keyboard.
- In this loop first read() function will capture a frame from the opened camera.
- Then datetime.now() will give the current date and time put this timestamp on that frame and display the image by using the cv2.imshow() function. Because we are doing this in the continuous loop it looks like a live video is playing.
- There are two variables used in the program are ‘ i ‘ and ‘wait’.
- ‘i’ variable is used for giving a unique name to images and the ‘wait’ variable for pausing saving images continuously.
- We are simply waiting for 5 seconds then we are saving images in the specified path.
- Finally when the user presses any key on the keyboard program will end.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Jan, 2023
Like Article
Save Article