Have you seen the security cameras output where DateTime continuously keeps updating? In this blog, we will be doing the same using OpenCV-Python i.e. we will put current DateTime on the live webcam feed. So, let’s get started.
For fetching current DateTime, we will be using Python’s DateTime module. The following code shows how to get the current DateTime
1 2 3 |
# get current DateTime from datetime import datetime print(datetime.now()) |
To put the DateTime on the live video, we will be using cv2.putText() on each frame as shown below
1 2 |
# Put current DateTime on each frame cv2.putText(img,str(datetime.now()),(140,250), font, .5,(255,255,255),2,cv2.LINE_AA) |
To know more about cv2.putText(), refer to this blog.
Above are the two things, that we will be needing for this task. I hope you understand these. Now, let’s get started
Steps:
- Open the camera using cv2.VideoCapture()
- Until the camera is open
- Grab each frame using cap.read()
- Put the current DateTime on each frame using cv2.putText() as discussed above
- Display each frame using cv2.imshow()
- On termination, release the webcam and destroy all windows using cap.release() and cv2.destroyAllWindows() respectively.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import cv2 from datetime import datetime # Open the Camera cap = cv2.VideoCapture(0) while True: ret, img = cap.read() # Put current DateTime on each frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img,str(datetime.now()),(10,30), font, 1,(255,255,255),2,cv2.LINE_AA) # Display the image cv2.imshow('a',img) # wait for keypress k = cv2.waitKey(10) if k == ord('q'): break cap.release() cv2.destroyAllWindows() |
The snapshot of the output looks like this
Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.