Open In App

Save frames of live video with timestamps – Python OpenCV

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 necessary libraries
import cv2
import os
from datetime import datetime
  
# set path in which you want to save images
path = r'C:\Users\vishal\Documents\Bandicam'
  
# changing directory to given path
os.chdir(path)
  
# i variable is to give unique name to images
i = 1
  
wait = 0
  
# Open the camera
video = cv2.VideoCapture(0)
  
  
while True:
    # Read video by read() function and it
    # will extract and  return the frame
    ret, img = video.read()
  
    # Put current DateTime on each frame
    font = cv2.FONT_HERSHEY_PLAIN
    cv2.putText(img, str(datetime.now()), (20, 40),
                font, 2, (255, 255, 255), 2, cv2.LINE_AA)
  
    # Display the image
    cv2.imshow('live video', img)
  
    # wait for user to press any key
    key = cv2.waitKey(100)
  
    # wait variable is to calculate waiting time
    wait = wait+100
  
    if key == ord('q'):
        break
    # when it reaches to 5000 milliseconds
    # we will save that frame in given folder
    if wait == 5000:
        filename = 'Frame_'+str(i)+'.jpg'
          
        # Save the images in given path
        cv2.imwrite(filename, img)
        i = i+1
        wait = 0
  
# close the camera
video.release()
  
# close open windows
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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads