- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display date and time in videos using OpenCV Python
OpenCV is an Open Source Computer Vision Library in python. It provides numerous functions to perform various Image and video processing operations. The library uses the python Numpy module to represent all the video or image arrays as a ndarray type. It needs the numpy library, we need to make sure that the numpy module is also installed in our python interpreter.
Displaying date and time in videos using OpenCV
In live streaming/video processing applications sometimes we need to display the date and time on videos. To achieve this, we can use the python datetime module.
The python datetime module is used to work with dates and times, it has various classes and functions to manipulate or represent dates and times in various formats. In this article, we will learn how to display date and time in videos using OpenCV Python.
OpenCV provides a method called cv2.putText(). It is used to write a text string in an image or video frames and this will be the main function for this task.
Syntax
cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
Where,
img: Input image/frame.
Text: A text string which has to be drawn.
org: A tuple of two coordinate values i.e. (X coordinate value, Y coordinate value).
fontFace: it specifies the font type. The supported font types are:
FONT_HERSHEY_SIMPLEX
FONT_HERSHEY_PLAIN
FONT_HERSHEY_DUPLEX
FONT_HERSHEY_COMPLEX
FONT_HERSHEY_TRIPLEX
FONT_HERSHEY_COMPLEX_SMALL
FONT_HERSHEY_SCRIPT_SIMPLEX
FONT_HERSHEY_SCRIPT_COMPLEX
FONT_ITALIC
fontScale: The font scale factor that is multiplied by the font-specific base size. It is used to increase/decrease the size of the text.
color: It is used to specify the color of the text. It takes the value in BGR format.
thickness (Optional): It specify the thickness of the lines used to draw the text. The default value is 1.
lineType (Optional): It specifies the type of line we want to use. The available 4 Line Types are −
FILLED
LINE_4
LINE_8 (Default)
LINE_AA
LeftOrigin (Optional): The default value is False. Put the text the top-left corner of the image.
Approach
Open the camera or read the input video file using cv2.VideoCapture() function.
Grab each frame using the video_obj.read() function.
Write the current DateTime on each frame using cv2.putText() function.
And finally, display each frame using cv2.imshow()
For closing, release the webcam and destroy all windows using video_obj.release() and cv2.destroyAllWindows() functions respectively.
Using a video file
By specifying the video file location to the cv2.VideoCapture() function it will read the video file data.
Example
In this example, we will display the current date and time on video.
import numpy import cv2 import datetime # open the video video = cv2.VideoCapture('Videos/Flowers video.mp4') while(video.isOpened()): ret, frame = video.read() if ret: # describe the font type font = cv2.FONT_HERSHEY_SIMPLEX # Get current date and time date_time = str(datetime.datetime.now()) # write the date time in the video frame frame = cv2.putText(frame, date_time,(10, 100),font, 1,(210, 155, 155), 4, cv2.LINE_4) # show the video cv2.imshow('frame', frame) key = cv2.waitKey(10) if key == ord('q'): break else: break video.release() cv2.destroyAllWindows()
Output
We have successfully displayed the current date time on a video output.
Using live video footage
We will display the current date time on a captured the live footage.
Example
In this example, we will display date and time in a live videos using OpenCV Python.
import cv2 import datetime cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() # describe the font type font = cv2.FONT_ITALIC # write current Date & Time on each frame date_time = str(datetime.datetime.now()) # write the date time in the video frame cv2.putText(frame,date_time,(10,100), font, 1,(0,0,255),2,cv2.LINE_AA) # show the video cv2.imshow('frame',frame) # wait for keypress k = cv2.waitKey(10) if k == ord('q'): break cap.release() cv2.waitKey(0) cv2.destroyAllWindows()