11from collections import defaultdict
2+ import json
3+ import os
24import random
35
46from moviepy.editor import *
@@ -11,21 +13,25 @@ class VideoHeatmapper:
1113 def __init__(self, heatmapper):
1214 self.heatmapper = heatmapper
1315
14- def heatmap_on_video(self, video_path, points, heat_fps=25):
15- base = VideoFileClip(video_path)
16- width, height = base.size
16+ def heatmap_on_video(self, base_video, points, heat_fps=15):
17+ width, height = base_video.size
1718
1819 frame_points = self._frame_points(points, heat_fps)
1920 heatmap_frames = self._heatmap_frames(width, height, frame_points)
2021 heatmap_clips = self._heatmap_clips(heatmap_frames, heat_fps)
2122
22- return CompositeVideoClip([base] + list(heatmap_clips))
23+ return CompositeVideoClip([base_video] + list(heatmap_clips))
24+
25+ def heatmap_on_video_path(self, video_path, points, heat_fps=15):
26+ base = VideoFileClip(video_path)
27+ return self.heatmap_on_video(base, points, heat_fps)
2328
2429 @staticmethod
2530 def _frame_points(pts, fps):
2631 frames = defaultdict(list)
32+ interval = 1000 // fps
2733 for x, y, t in pts:
28- start = (t // fps ) * fps
34+ start = (t // interval ) * interval
2935 frames[start].append((x, y))
3036 return frames
3137
@@ -39,24 +45,29 @@ def _heatmap_clips(heatmap_frames, fps):
3945 for frame_start, heat in heatmap_frames:
4046 yield (ImageClip(heat)
4147 .set_start(frame_start/1000)
42- .set_duration(fps/1000)
43- .set_fps(fps))
48+ .set_duration((1000/fps)/1000))
4449
4550
46- if __name__ == '__main__' :
51+ def _example_random_points() :
4752 def rand_point(max_x, max_y, max_t):
4853 return random.randint(0, max_x), random.randint(0, max_y), random.randint(0, max_t)
4954
50- example_points = (rand_point(720, 480, 4000) for _ in range(15000))
51- example_vid = 'assets\SampleVideo_720x480_1mb.mp4'
55+ return (rand_point(720, 480, 4000) for _ in range(15000))
5256
53- img_heatmapper = Heatmapper(colours='default')
57+
58+ def main():
59+ example_vid = os.path.join('assets', 'SampleVideo_720x480_1mb.mp4')
60+
61+ img_heatmapper = Heatmapper(colours='default', point_strength=0.6)
5462 video_heatmapper = VideoHeatmapper(img_heatmapper)
5563
56- video = heatmap_video = video_heatmapper.heatmap_on_video (
64+ heatmap_video = video_heatmapper.heatmap_on_video_path (
5765 video_path=example_vid,
58- points=example_points
66+ points=_example_random_points()
5967 )
6068
61- video.write_videofile('out.mp4', bitrate="5000k", fps=24)
69+ heatmap_video.write_videofile('out.mp4', bitrate="5000k", fps=24)
70+
71+ if __name__ == '__main__':
72+ main()
6273
0 commit comments