PySport

  • Home
  • OpenSource
  • About

PySport

  • Home
  • OpenSource
  • About

MatplotVideo: Sync your plot to your video

2020-05-07

Thanks to Joe Mulberry for the inspiration for this post :-)

While working on a new project we wanted to validate the output of our model. After staring at the data for a while I decided there should be a better way. I want to do some manual comparision between the data and the video.

In the yesterdays Friends of Tracking episode, Javier Fernández describes why it’s important to do a visual validation.

I faced so many times an error in an algorithm or in the modelling approach, I spotted that in the video so many times i’m actually wondering how can you do that without video […]
video is a good way of validating
 
– Javier Fernández - Soccer Data Scientist FC Barcelona (@JaviOnData) May 2020

Load the file in VLC and go, atleast that’s what I thought. But that’s not going to work. I need controls for easy forward and backwards seeking, and frame-by-frame seeking. VLC doesn’t help me there.

There must be a package for that. And there is a great package that - amongst other cool stuff - also helps with playing a video file: OpenCV. There is a nice prebuilt binary package for python package available.

OpenCV

There are a lot interesting tutorials and examples available on the web about how to use OpenCV. If you like to play around with reading a video file you can start with this example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

Sync the plots

*Example of syncing a combination of tracking- and event data, and video*

Lets look at a simple example of how to do the sync:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from matplotvideo import attach_video_player_to_figure
import matplotlib.pyplot as plt

# (timestamp, value) pairs

# sample: big bunny scene cuts
fancy_data = [
(0, 1),
(11.875, 1),
(11.917, 2),
(15.75, 2),
(15.792, 3),
(23.042, 3),
(23.083, 4),
(47.708, 4),
(47.75, 5),
(56.083, 5),
(56.125, 6),
(60, 6)
]


def on_frame(video_timestamp, line):
timestamps, y = zip(*fancy_data)
x = [timestamp - video_timestamp for timestamp in timestamps]

line.set_data(x, y)
line.axes.relim()
line.axes.autoscale_view()
line.axes.figure.canvas.draw()


def main():
fig, ax = plt.subplots()
plt.xlim(-15, 15)
plt.axvline(x=0, color='k', linestyle='--')

line, = ax.plot([], [], color='blue')

attach_video_player_to_figure(fig, "BigBuckBunny.mp4", on_frame, line=line)

plt.show()


main()

This example syncs fictive scene change detection data with bigbunny video.

The ‘magic’ to redraw the plot is this part:

1
2
3
4
line.set_data(x, y)             # set the new data
line.axes.relim() # recalculate the data limits
line.axes.autoscale_view() # scale view to limits
line.axes.figure.canvas.draw() # redraw canvas

But I only want to print info

You can also use this library without matplotlib. This can be usefull if you prefer a different way of outputting, print to console for example.

1
2
3
4
5
6
7
8
9
10
11
from matplotvideo import start_video_player

from super_fancy_model_package import fancy_model

frames, fps = load_tracking_data()

def on_frame(timestamp: float):
some_value = fancy_model.show_value(frames[fps * timestamp])
print(f"{timestamp}: {some_value}")

start_video_player("favorite_match.mp4", on_frame)

Let’s start

It’s available through PyPi and installable using your favorite package manager. For pip:

1
pip install matplotvideo

Source code can be found on github

More >>

« Prev1…34567Next »
© 2022 PySport
Sponsored by | join us