Skip to content

Commit 5e123ee

Browse files
author
axwalker
committed
Fix fps bug when creating heatmap frames
1 parent 8412d34 commit 5e123ee

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

heatmappy/video.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from collections import defaultdict
2+
import json
3+
import os
24
import random
35

46
from 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

setup.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
required = f.read().splitlines()
55

66
setup(
7-
name = 'heatmappy',
8-
packages = ['heatmappy'],
9-
version = '0.1.1',
10-
description = 'Draw image heatmaps in python',
11-
author = 'Lumen Research',
12-
author_email = '[email protected]',
13-
url = 'https://github.com/LumenResearch/heatmappy',
14-
download_url = 'https://github.com/LumenResearch/heatmappy/tarball/0.1.1',
15-
keywords = ['image', 'heatmap', 'heat map'],
16-
install_requires=required,
17-
classifiers = [
18-
'Programming Language :: Python :: 3'
19-
],
20-
include_package_data=True,
7+
name='heatmappy',
8+
packages=['heatmappy'],
9+
version='0.2.0',
10+
description='Draw image heatmaps in python',
11+
author='Lumen Research',
12+
author_email='[email protected]',
13+
url='https://github.com/LumenResearch/heatmappy',
14+
download_url='https://github.com/LumenResearch/heatmappy/tarball/0.1.1',
15+
keywords=['image', 'heatmap', 'heat map'],
16+
install_requires=required,
17+
classifiers=[
18+
'Programming Language :: Python :: 3'
19+
],
20+
include_package_data=True,
2121
)

0 commit comments

Comments
 (0)