首先实例化化一个mmcv的videoreader,传入的参数为视频路径
import mmcv
video_reader = mmcv.VideoReader(video_path)
通过阅读mmcv.VideoReader的源码得知VideoReader的方法其实就是调用了opencv的VideoCapture中的方法。
videocapture简介VideoCapture()中参数是0,表示打开笔记本的内置摄像头,参数是视频文件路径则打开视频,如cap = cv2.VideoCapture("../test.avi")
class VideoReader:
def __init__(self, filename, cache_capacity=10):
.....
self._vcap = cv2.VideoCapture(filename)
.....
@property
def vcap(self):
""":obj:`cv2.VideoCapture`: The raw VideoCapture object."""
return self._vcap
mmcv的videoreader中很多方法都是调用这个vcap方法@property的作用,所以我就参考cv2获取时间戳,调用了这个vcap也就是调用cv2的VideoCapture来基于mmcv框架获取时间戳。主要是在使用mmcv框架时比较方便。
video_reader = mmcv.VideoReader(video_path)
for frame in track_iter_progress(video_reader):
result = inference_detector(model, frame, test_pipeline=test_pipeline)
# 获取时间戳
milliseconds = video_reader.vcap.get(cv2.CAP_PROP_POS_MSEC)
seconds = milliseconds//1000
milliseconds = milliseconds%1000
minutes = 0
hours = 0
if seconds >= 60:
minutes = seconds//60
seconds = seconds % 60
if minutes >= 60:
hours = minutes//60
minutes = minutes % 60
# print(int(hours), int(minutes), int(seconds), int(milliseconds))
times = "{}时:{}分:{}秒".format(hours, minutes, seconds)
#