def spaced_frames(parser, start=None, end=None, interval=None, num_samples=None, fuzz=4):
if (interval is None and num_samples is None) or None not in (interval, num_samples):
raise ValueError('exactly one of (interval, num_samples) must be set')
vc = cv2.VideoCapture(parser.stream)
video_length = vc.get(7) / vc.get(5)
if not start or start < 0:
start = 0
if not end or end > video_length:
end = video_length
total_time = end - start
if not num_samples:
num_samples = total_time // interval
for time in np.linspace(start, end, num=num_samples):
time += randint(-1 * fuzz, fuzz) / vc.get(5)
time = min([max([0, time]), total_time])
vc.set(0, int(time * 1000))
success, frame = vc.read()
if success:
yield (time, cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
return
评论列表
文章目录