1
1
from collections import defaultdict
2
+ import json
3
+ import os
2
4
import random
3
5
4
6
from moviepy .editor import *
@@ -11,21 +13,25 @@ class VideoHeatmapper:
11
13
def __init__ (self , heatmapper ):
12
14
self .heatmapper = heatmapper
13
15
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
17
18
18
19
frame_points = self ._frame_points (points , heat_fps )
19
20
heatmap_frames = self ._heatmap_frames (width , height , frame_points )
20
21
heatmap_clips = self ._heatmap_clips (heatmap_frames , heat_fps )
21
22
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 )
23
28
24
29
@staticmethod
25
30
def _frame_points (pts , fps ):
26
31
frames = defaultdict (list )
32
+ interval = 1000 // fps
27
33
for x , y , t in pts :
28
- start = (t // fps ) * fps
34
+ start = (t // interval ) * interval
29
35
frames [start ].append ((x , y ))
30
36
return frames
31
37
@@ -39,24 +45,29 @@ def _heatmap_clips(heatmap_frames, fps):
39
45
for frame_start , heat in heatmap_frames :
40
46
yield (ImageClip (heat )
41
47
.set_start (frame_start / 1000 )
42
- .set_duration (fps / 1000 )
43
- .set_fps (fps ))
48
+ .set_duration ((1000 / fps )/ 1000 ))
44
49
45
50
46
- if __name__ == '__main__' :
51
+ def _example_random_points () :
47
52
def rand_point (max_x , max_y , max_t ):
48
53
return random .randint (0 , max_x ), random .randint (0 , max_y ), random .randint (0 , max_t )
49
54
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 ))
52
56
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 )
54
62
video_heatmapper = VideoHeatmapper (img_heatmapper )
55
63
56
- video = heatmap_video = video_heatmapper .heatmap_on_video (
64
+ heatmap_video = video_heatmapper .heatmap_on_video_path (
57
65
video_path = example_vid ,
58
- points = example_points
66
+ points = _example_random_points ()
59
67
)
60
68
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 ()
62
73
0 commit comments