📜  使用 OpenCV 在视频中显示日期和时间 - Python

📅  最后修改于: 2022-05-13 01:55:31.291000             🧑  作者: Mango

使用 OpenCV 在视频中显示日期和时间 - Python

OpenCV-Python是一个Python绑定库,旨在解决计算机视觉问题。它可以处理图像和视频以识别物体、面部,甚至是人类的笔迹
注:更多信息请参考OpenCV简介

在视频中显示日期和时间

当我们处理直播视频或持续时间较长的视频时,有时需要在视频上显示日期和时间。时间和日期将有助于了解和分析视频中检测到的任何异常,参考其时间和日期。要在视频上显示日期和时间,我们执行以下操作。
代码

Python3
# Import libraries
import numpy
import cv2
import datetime
 
# open the video
vid = cv2.VideoCapture('sample.mp4')
 
# Process until end.
while(vid.isOpened()):
    ret, frame = vid.read()
 
    if ret:
        # describe the type of
        # font you want to display
        font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
 
        # Get date and time and
        # save it inside a variable
        dt = str(datetime.datetime.now())
 
        # put the dt variable over the
        # video frame
        frame = cv2.putText(frame, dt,
                            (10, 100),
                            font, 1,
                            (210, 155, 155),
                            4, cv2.LINE_8)
         
        # show the video
        cv2.imshow('frame', frame)
 
        key = cv2.waitKey(1)
         
        # define the key to
        # close the window
        if key == 'q' or key == 27:
            break
    else:
         
        break
         
         
# release the vid object
vid.release()
 
# close all the opened windows.
cv2.destroyAllWindows()


输出

python-date-time-on-video-opencv