All basketball teams have a camera system called SportVU installed in their arenas. These camera systems track players and the ball throughout a basketball game.
The data produced by sportsvu camera systems used to be freely available on NBA.com, but was recently removed (I have no idea why). Luckily, the data for about 600 games are available on neilmj’s github. In this post, I show how to create a video recreation of a given basketball play using the sportsvu data.
This code is also available as a jupyter notebook on my github.
1 2 3 4 5
The data is provided as a json. Here’s how to import the python json library and load the data. I’m a T-Wolves fan, so the game I chose is a wolves game.
1 2 3
Let’s take a quick look at the data. It’s a dictionary with three keys: gamedate, gameid, and events. Gamedate and gameid are the date of this game and its specific id number, respectively. Events is the structure with data we’re interested in.
[u'gamedate', u'gameid', u'events']
Lets take a look at the first event. The first event has an associated eventid number. We will use these later. There’s also data for each player on the visiting and home team. We will use these later too. Finally, and most importantly, there’s the “moments.” There are 25 moments for each second of the “event” (the data is sampled at 25hz).
[u'eventId', u'visitor', u'moments', u'home']
Here’s the first moment of the first event. The first number is the quarter. The second number is the time of the event in milliseconds. The third number is the number of seconds left in the quarter (the 1st quarter hasn’t started yet, so 12 * 60 = 720). The fourth number is the number of seconds left on the shot clock. I am not sure what fourth number (None) represents.
The final matrix is 11x5 matrix. The first row describes the ball. The first two columns are the teamID and the playerID of the ball (-1 for both because the ball does not belong to a team and is not a player). The 3rd and 4th columns are xy coordinates of the ball. The final column is the height of the ball (z coordinate).
The next 10 rows describe the 10 players on the court. The first 5 players belong to the home team and the last 5 players belong to the visiting team. Each player has his teamID, playerID, xy&z coordinates (although I don’t think players’ z coordinates ever change).
[1, 1452903036782, 720.0, 24.0, None, [[-1, -1, 44.16456, 26.34142, 5.74423], [1610612760, 201142, 45.46259, 32.01456, 0.0], [1610612760, 201566, 10.39347, 24.77219, 0.0], [1610612760, 201586, 25.86087, 25.55881, 0.0], [1610612760, 203460, 47.28525, 17.76225, 0.0], [1610612760, 203500, 43.68634, 26.63098, 0.0], [1610612750, 708, 55.6401, 25.55583, 0.0], [1610612750, 2419, 47.95942, 31.66328, 0.0], [1610612750, 201937, 67.28725, 25.10267, 0.0], [1610612750, 203952, 47.28525, 17.76225, 0.0], [1610612750, 1626157, 49.46814, 24.24193, 0.0]]]
Alright, so we have the sportsvu data, but its not clear what each event is. Luckily, the NBA also provides play by play (pbp) data. I write a function for acquiring play by play game data. This function collects (and trims) the play by play data for a given sportsvu data set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Below I show what the play by play data looks like. There’s a column for event number (eventnum). These event numbers match up with the event numbers from the sportsvu data, so we will use this later for seeking out specific plays in the sportsvu data. There’s a column for the event type (eventmsgtype). This column has a number describing what occured in the play. I list these number codes in the comments below.
There’s also short text descriptions of the plays in the home description and visitor description columns. Finally, I use the team column to represent the primary team involved in a play.
I stole the idea of using play by play data from Raji Shah.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|1||1||10||Jump Ball Adams vs. Towns: Tip to Ibaka||None||OKC|
|2||2||5||Westbrook Out of Bounds Lost Ball Turnover (P1...||None||OKC|
|3||3||2||None||MISS Wiggins 16' Jump Shot||MIN|
|4||4||4||Westbrook REBOUND (Off:0 Def:1)||None||OKC|
When viewing the videos, its nice to know what players are on the court. I like to depict this by labeling each player with their number. Here I create a dictionary that contains each player’s id number (these are assigned by nba.com) as the key and their jersey number as the associated value.
1 2 3 4 5
Alright, almost there! Below I write some functions for creating the actual video! First, there’s a short function for placing an image of the basketball court beneath our depiction of players moving around. This image is from gmf05’s github, but I will provide it on mine too.
Much of this code is either straight from gmf05’s github or slightly modified.
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
The event that I want to depict is event 41. In this event, Karl Anthony Towns misses a shot, grabs his own rebounds, and puts it back in.
|37||41||1||None||Towns 1' Layup (2 PTS)||MIN|
We need to find where event 41 is in the sportsvu data structure, so I created a function for finding the location of a particular event. I then create a matrix with position data for the ball and a matrix with position data for each player for event 41.
1 2 3 4 5 6 7 8 9 10 11 12
Okay. We’re actually there! Now we get to create the video. We have to create figure and axes objects for the animation to draw on. Then I place a picture of the basketball court on this plot. Finally, I create the circle and text objects that will move around throughout the video (depicting the ball and players). The location of these objects are then updated in the animation loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
I’ve been told this video does not work for all users. I’ve also posted it on youtube.