Analyze A Soccer Game Using Tensorflow Object Detection and OpenCV PDF
Analyze A Soccer Game Using Tensorflow Object Detection and OpenCV PDF
Analyze A Soccer Game Using Tensorflow Object Detection and OpenCV PDF
Introduction
The world cup season is here and off to an interesting start. Who ever
thought the reining champions Germany would be eliminated in the
group stage :(
For the data scientist within you lets use this opportunity to do some
analysis on soccer clips. With the use of deep learning and opencv we
can extract interesting insights from video clips. See example gif below
of the game b/w Australia and Peru played where we can identify all
the players + referees, the soccer ball and also predict which team the
player is based on the color of their jersey. And all of this can be done
real time.
https://towardsdatascience.com/analyse-a-soccer-game-using-tensorflow-object-detection-and-opencv-e321c230e8f2 1/6
01/05/2019 Analyze a Soccer game using Tensorflow Object Detection and OpenCV
please see the following blogs from me that introduce the API and
teach you how to build a custom model using the API.
The API provides pre-trained object detection models that have been
trained on the COCO dataset. COCO dataset is a set of 90 commonly
found objects. See image below of objects that are part of COCO
dataset.
In this case we care about classes — persons and soccer ball which are
both part of COCO dataset.
The API also has a big set of models it supports. See table below for
reference.
The models have a trade off between speed and accuracy. Since I was
interested in real time analysis, I chose SSDLite mobilenet v2.
Once we identify the players using the object detection API, to predict
which team they are in we can use OpenCV which is powerful library
https://towardsdatascience.com/analyse-a-soccer-game-using-tensorflow-object-detection-and-opencv-e321c230e8f2 2/6
01/05/2019 Analyze a Soccer game using Tensorflow Object Detection and OpenCV
for image processing. If you are new to OpenCV please see the tutorial
below:
OpenCV Tutorial
If you are using the Tensorflow Object Detection API for the first time,
please download the GitHub from this link. And install all the
dependencies using these instructions.
If you don’t have OpenCV setup, please build it from source using this
tutorial.
The main steps I followed are (please follow along in the jupyter
notebook on my Github):
• Load the SSDLite mobilenet model into a graph and load the list of
classes that are part of COCO dataset
• For each frame perform object detection using the loaded graph
https://towardsdatascience.com/analyse-a-soccer-game-using-tensorflow-object-detection-and-opencv-e321c230e8f2 3/6
01/05/2019 Analyze a Soccer game using Tensorflow Object Detection and OpenCV
• The result that comes back from the SSDLite is each identified
class along with its confidence score and bounding box prediction.
So now identify all persons identified with confidence > 0.6 and
crop them out.
• Now you have each player extracted out. We need to read the color
of their jersey to predict if they are an Australian player or a Peru
player. This is done by the code block detect team. We first define
the color ranges for red and blue colors. Then we use cv2.inRange
and cv2.bitwise to create a mask of that color. To detect team, I
count how many red pixels and yellow pixels were detected and
what is the percent of that compared to total num of pixels in the
cropped image.
• Finally all the code pieces are combined to run everything at the
same time and display results using cv2.imshow
1. With the camera angle at the Australian goal area you can
calculate how many Peru players are in the zone vs Australian
players
2. You can draw a heat map of each teams’ footprint — Example what
are the areas which saw high occupancy from Peru team
The Object Detection API also offers other models that are more
accurate but slower. You can try those as well.
Give me a ❤ if you liked this post:) Hope you pull the code and try it
yourself.
If you have a project that we can collaborate on, then please contact me
through my website or at [email protected]
https://towardsdatascience.com/analyse-a-soccer-game-using-tensorflow-object-detection-and-opencv-e321c230e8f2 4/6
01/05/2019 Analyze a Soccer game using Tensorflow Object Detection and OpenCV
References
https://towardsdatascience.com/analyse-a-soccer-game-using-tensorflow-object-detection-and-opencv-e321c230e8f2 5/6
01/05/2019 Analyze a Soccer game using Tensorflow Object Detection and OpenCV
https://towardsdatascience.com/analyse-a-soccer-game-using-tensorflow-object-detection-and-opencv-e321c230e8f2 6/6