0% found this document useful (0 votes)
26 views64 pages

Document From ManGo

The document outlines a project focused on enhancing football analysis using OpenCV, YOLO, and Python, addressing the limitations of manual tracking methods that are time-consuming and prone to errors. It proposes an automated system that utilizes real-time object detection to provide insights on player movements, ball possession, and team strategies, thereby improving decision-making for coaches and analysts. The project aims to make football analysis more efficient and accessible, particularly for lower-league teams that currently lack advanced analytical tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views64 pages

Document From ManGo

The document outlines a project focused on enhancing football analysis using OpenCV, YOLO, and Python, addressing the limitations of manual tracking methods that are time-consuming and prone to errors. It proposes an automated system that utilizes real-time object detection to provide insights on player movements, ball possession, and team strategies, thereby improving decision-making for coaches and analysts. The project aims to make football analysis more efficient and accessible, particularly for lower-league teams that currently lack advanced analytical tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 1

INTRODUCTION
1.1 INTRODUCTION TO THE PROJECT

Football, as one of the world’s most popular sports, is not only about physical prowess
but also about strategic and tactical precision. In recent years, the importance of data analytics
in football has skyrocketed, influencing everything from player training to in-game decision-
making.

Coaches and analysts have traditionally relied on manual methods to review match
footage and derive insights about formations, tactics, and player performance. However, these
manual processes are time-consuming, labor-intensive, and prone to human error, limiting their
effectiveness in fast-paced sports like football.

YOLO is chosen due to its real-time object detection capabilities and high accuracy,
making it suitable for fast-moving sports like football. OpenCV is employed for video
processing, enabling frame-by-frame analysis and manipulation.

Together, these tools allow for the identification of key elements on the field, such as
players, the ball, and referees, to extract meaningful insights. By detecting patterns and tracking
movement, the system can aid in analyzing team formations, player strategies, and event
occurrences such as shots on goal, fouls, and passes.

The project enhances tactical insights by offering precise data on player positioning
and movements throughout the match. Additionally, it improves referee monitoring and
decision-making, while enabling teams and coaches to make informed adjustments during and
after games. Overall, it contributes to a deeper understanding of the game, benefiting both
coaching and player development.

1.2 STATEMENT OF THE PROBLEM

The primary problem in football analysis lies in the manual tracking and assessment of
player movements, ball possession, and team strategies, which is time-consuming, prone to errors,
and lacks real-time precision.
The growing demand for more detailed and accurate football analysis, combined with the
limitations of manual video review, creates a need for automated solutions. Current manual
analysis methods are inefficient for tracking complex patterns such as player movements, ball
trajectories, and tactical formations over the course of an entire game. Additionally, the ability to
extract real-time insights during live matches is still underdeveloped, leaving coaches and analysts
dependent on post-match reviews. This bottleneck in real-time, data-driven decision-making has
motivated the development of automated systems for football analysis.

School of CSA, REVA University, Bengaluru 1


Football analysis with OpenCV, YOLO and Python 2024

Fig.1.1: Manual tracking and analysis

1.3 SYSTEM SPECIFICATIONS

1.3.1 SOFTWARE REQUIREMENTS:

1. Operating System:
o Windows 10/11, Linux (Ubuntu preferred), or macOS.
2. Programming Language:
o Python 3.7 or higher.
3. Libraries and Frameworks:
o YOLO (You Only Look Once): For real-time object detection.
o OpenCV (Open-Source Computer Vision Library): For video and image
processing.
o TensorFlow or PyTorch (optional): If you're training your YOLO model from
scratch.
o NumPy: For numerical operations.
o Supervision: (For further object detection tracking).
o Matplotlib/Seaborn: For visualizing the results and generating plots (heatmaps,
etc.).
4. Development Environment:
o IDE/Text Editor: PyCharm, VSCode, Jupyter Notebook, or any Python-compatible
IDE.

1.3.2 HARDWARE REQUIREMENTS:

1. CPU:

School of CSA, REVA University, Bengaluru 2


Football analysis with OpenCV, YOLO and Python 2024

o Minimum: Intel Core i5 or AMD Ryzen 5.


2. GPU (Highly recommended for YOLO and deep learning):
o Minimum: NVIDIA GTX 1050 or higher.
3. RAM:
o Minimum: 8 GB.
4. Storage:
o Minimum: 256 GB SSD.
o Recommended: 512 GB or 1 TB SSD for faster read/write speeds.
5. Other Peripherals:
o Camera (if capturing live video): A high-resolution camera if you’re analysing live
matches.
o Display: Full HD monitor or better for clear visualization.

School of CSA, REVA University, Bengaluru 3


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 2

LITERATURE SURVEY

1. Object Detection and Tracking for Football Data Analytics


Authors: Shankara Narayanan V, Syed Ashfaq Ahmed

This paper presents a method of quantifying ball possession and its usage in foot- ball
sports data analytics by using object detection and object tracking. After comparing the
performance of YOLOv5 and YOLOv8 which are two state-of-the-art object detection models, the
latter was chosen to be used along with BYTETrack for object detection and tracking. The input
will be a video stream of a football game taken from a tactical camera which is passed to the object
detection module. The detected objects are individually tracked and ball possession is calculated
per player by assigning unique track-id for all players. Finally, aggregating player’s individual ball
possession into their respective teams provides a way of estimating the team’s ball possession.

2. The use of match statistics that discriminate between successful and


unsuccessful soccer teams.
Authors: Castellano J, Casamichana D, Lago C.
The aim of this study was to identify the performance indicators that best discriminated
between winning, drawing and losing teams in three soccer World Cups (Korea/Japan 2002,
Germany 2006 and South Africa 2010), and to determine whether the indicators that differentiated
between successful and unsuccessful teams were repeated across these three tournaments. The
results of the initial univariate analysis identified nine variables that differed between winning,
drawing and losing teams, while in the subsequent multivariate analysis only four variables were
found to discriminate teams in relation to their performance. When analysing the three World Cups
as a whole the variables with the greatest discriminatory power were total shots and shots on
target (both made and received), although this finding was not constant across the three
tournaments when analysed separately. Previous studies of soccer World Cups (those held in 1998,
2002 and 2006) found similar patterns of play among teams (Castellano et al., 2007b; 2008), while
the present study shows slight differences in the variables that discriminate between successful
and unsuccessful teams competing in the last three World Cups.

3. Performance analysis in football: A critical review and implications for future


research.
Authors: Mackenzie R, Cushion C.

School of CSA, REVA University, Bengaluru 4


Football analysis with OpenCV, YOLO and Python 2024

This paper critically reviews existing literature relating to performance analysis (PA) in
football, arguing that an alternative approach is warranted. The paper considers the applicability
of variables analysed along with research findings in the context of their implications for
professional practice. This includes a review of methodological approaches commonly adopted
throughout PA research, including a consideration of the nature and size of the samples used in
relation to generalisability. Definitions and classifications of variables used within performance
analysis are discussed in the context of reliability and validity. The contribution of PA findings to
the field is reviewed. The review identifies an overemphasis on researching predictive and
performance controlling variables. A different approach is proposed that works with and from
performance analysis information to develop research investigating athlete and coach learning,
thus adding to applied practice. Future research should pay attention to the social and cultural
influences that impact PA delivery and athlete learning in applied settings.

4. SoccerNet-Tracking: Multiple Object Tracking Dataset and Benchmark in


Soccer Videos.
Authors: Cioppa A, Giancola S, Delie`ge A, Kang L, et al.
Tracking objects in soccer videos is extremely important to gather both player and team
statistics, whether it is to estimate the total distance run, the ball possession or the team formation.
Video processing can help automating the extraction of those information, without the need of any
invasive sensor, hence applicable to any team on any stadium. Yet, the availability of datasets to
train learnable models and benchmarks to evaluate methods on a common testbed is very limited.
In this work, we propose a novel dataset for multiple object tracking composed of 200 sequences
of 30s each, representative of challenging soccer scenarios, and a complete 45-minutes half-time
for long-term tracking. The dataset is fully annotated with bounding boxes and tracklet IDs,
enabling the training of MOT baselines in the soccer domain and a full benchmarking of those
methods on our segregated challenge sets. Our analysis shows that multiple players, referee and
ball tracking in soccer videos is far from being solved, with several improvement required in case
of fast motion or in scenarios of severe occlusion.

5. Trajectory-Based Ball Detection and Tracking with Applications to Semantic


Analysis of Broadcast Soccer Video
Authors: Xinguo Yu, Changsheng Xu, Hon Wai Leong, et al.
This paper first presents an improved trajectory-based algorithm for automatically detecting and
tracking the ball in broadcast soccer video. Unlike the object-based algorithms, our algorithm does
not evaluate whether a sole object is a ball. Instead, it evaluates whether a candidate trajectory,
which is generated from the candidate feature image by a candidate verification procedure based
on Kalman filter, is a ball trajectory. The algorithms in this approach not only improve play-break
analysis and high-level semantic event detection, but also detect the basic actions and analyze team

School of CSA, REVA University, Bengaluru 5


Football analysis with OpenCV, YOLO and Python 2024

ball possession, which may not be analyzed based only on the low-level feature. They used an
iterative procedure, which is more reliable than the previous naïve procedure, to process the
candidate trajectory. Moreover, they added goalmouth and ellipse detection to improve the ball
size estimation. The experimental results show that the improved algorithm boosts the accuracy of
the ball detection and tracking from about 85% to above 96% for the video segments in which all
frames without the field have been removed in advance.

School of CSA, REVA University, Bengaluru 6


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 3
SYSTEM ANALYSIS
The football analysis system is designed to automate and enhance the accuracy of tracking
players, referees, and the ball during a match. This system integrates multiple technologies to
overcome the challenges posed by manual methods and traditional video analysis.

3.1 EXISTING SYSTEM

Current football evaluations rely on manual analysis of match footage, which is laborious,
time-consuming, and prone to error. Analysts often review video footage after a match to track
player movement, ball position, and overall gameplay. This process often requires a lot of
repetition and lacks the accuracy needed to measure performance, such as tracking player
movements or calculating metrics such as speed and displacement.

Existing systems do provide some automated features, such as basic replay and event
tracking systems, but these are often expensive, require complex setups, and may not be accessible
to all football teams, especially at lower levels. Furthermore, they do not focus on providing in-
depth, accurate data like individual player tracking or ball possession analytics, which are essential
for performance evaluation and strategic planning.

Fig.3.1: Manual video analysis

3.2 LIMITATIONS OF THE EXISTING SYSTEM

Despite advancements in football analysis technology, there are still several limitations that
hinder its full potential. These limitations arise from issues related to technology, cost, accuracy,
and accessibility, all of which affect the system’s overall efficiency and usability.

School of CSA, REVA University, Bengaluru 7


Football analysis with OpenCV, YOLO and Python 2024

1. Manual Effort and Time Consumption:


A significant limitation is the heavy reliance on manual video review and
annotation. Analysts often need to spend hours, sometimes days, reviewing footage to
gather insights, which can slow down decision-making. This manual process is not only
time-consuming but also prone to human error, such as oversight or bias, which can reduce
the reliability of the analysis. This limitation is particularly pronounced in smaller leagues
and amateur settings where there are fewer resources for automated data collection tools,
making comprehensive analysis time-intensive and impractical.

2. High Costs of Automated Systems


High-end football analysis tools that offer player tracking, wearable sensors, and
real-time data come at a substantial cost. These systems are typically accessible only to
top-tier clubs and professional leagues. For smaller teams, the costs of implementing such
technology can be prohibitive. This creates a gap between elite clubs and lower-league
teams, limiting the latter's ability to access and benefit from advanced analysis
technologies, which can hinder their performance and development.

3. Inaccuracies in Player and Ball Tracking


While automated player and ball tracking technologies have improved, they still
face challenges. Tracking errors can occur due to occlusion, poor camera angles, or the fast
pace of the game. Such errors can result in misidentification of players or incorrect ball
tracking, which leads to inaccurate statistics such as player positioning, ball possession, or
pass success rates. These inaccuracies can have a significant impact on the insights derived
from the analysis, making some data unreliable for performance evaluation.
4. Lack of Advanced Tactical and Team-Level Insights
Current systems often focus more on individual player metrics—like speed,
distance covered, or touches—while providing less insight into team-level tactics.
Comprehensive tactical analysis, which includes team formations, transitions, and
collective strategies, is often underrepresented. This gap limits a coach’s ability to assess
the overall team strategy and its effectiveness during matches, as well as how individual
players contribute to the team’s tactical approach.

5. Limited Access for Lower-League and Amateur Teams


A critical limitation is that many of these advanced analysis tools are not accessible
to lower-league or amateur teams. Without access to more affordable, user-friendly

School of CSA, REVA University, Bengaluru 8


Football analysis with OpenCV, YOLO and Python 2024

solutions, these teams are left with basic video footage and little-to-no detailed analysis.
This limits their ability to use data to enhance player development or improve match
strategies, creating a disparity in the level of analysis between top-tier and grassroots teams.

3.3 PROPOSED SYSTEM

The proposed system revolutionizes football analysis by utilizing state-of-the-art computer


vision and machine learning techniques. Central to the system is YOLO (You Only Look Once),
an object detection model that accurately tracks crucial elements in the game, such as players,
referees, and the ball. This approach offers a faster, more precise alternative to traditional manual
analysis. Additionally, clustering algorithms are employed for identifying teams by recognizing
differences in player uniforms. Optical flow algorithms are used to analyze motion, enabling the
tracking of movement patterns and camera shifts. These combined methods enable comprehensive
game insights.

By automating these analytical processes, the system generates detailed data on ball
possession, player performance metrics, and overall team dynamics, surpassing the capabilities of
manual analysis. The key functionalities of the system are outlined as follows:

Functionalities:

1. Player and Ball Tracking:


The system automates the tracking of players, referees, and the ball during matches,
leveraging YOLO for precise detection. This automation minimizes manual video analysis
while providing accurate tracking of player positions and movements across the field,
essential for in-depth post-match review and tactical evaluation.
2. Team Ball Possession:
Through clustering techniques, the system distinguishes between teams based on uniform
colors, allowing accurate tracking of ball possession. Continuous monitoring of ball
possession offers teams critical insights into their control during the game, aiding in tactical
adjustments and strategic decisions.
3. Performance Metrics:
The system computes key performance indicators, such as the speed and distance each
player covers throughout the match. These metrics give coaches and analysts a clearer
picture of individual player performance, endurance, and tactical contributions, supporting
better fitness management and positioning strategies.
4. Strategic Insights:

School of CSA, REVA University, Bengaluru 9


Football analysis with OpenCV, YOLO and Python 2024

Beyond individual player analysis, the system provides real-time insights into team
formations and movement patterns. By monitoring positional and tactical dynamics,
coaches can evaluate how effectively their team is executing strategies and adapting to the
opponent’s formation, offering valuable insights for improving team performance.

This automated system not only simplifies football analysis but also offers actionable
insights into both individual and team performance. By incorporating object detection, team
identification, and motion analysis, it provides a powerful tool for post-match analysis and
strategic planning, enhancing the effectiveness of football analysis beyond traditional approaches.

3.4 ADVANTAGES OF THE PROPOSED SYSTEM

The proposed system offers numerous advantages that significantly improve football analysis they
are:

1. Automation and Efficiency:


The system automates the process of tracking players, referees, and the ball, which reduces
the need for manual video analysis. This leads to significant time savings, allowing analysts
to focus on interpreting the data and making strategic decisions rather than being bogged
down by data collection.
2. Accurate Player and Ball Tracking:
By leveraging YOLO's object detection capabilities, the system ensures precise tracking of
players, referees, and the ball throughout the match. This accuracy minimizes errors that
typically occur with manual tracking, offering more reliable and actionable insights.
3. Detailed Performance Metrics:
The system generates comprehensive metrics such as the speed and distance covered by
players, which are essential for evaluating individual performance. These insights give
teams a clearer understanding of each player's fitness levels, contributions, and tactical
positioning, ultimately helping identify areas for improvement.
4. Team Ball Possession Analysis:
The ability to track ball possession through team identification using uniform color
clustering provides valuable information about how long each team maintains control of
the ball. This data helps teams adjust their strategies to enhance ball control during critical
phases of the game.
5. Tactical and Strategic Insights:

School of CSA, REVA University, Bengaluru 10


Football analysis with OpenCV, YOLO and Python 2024

By analyzing team formations and movement patterns, the system offers real-time insights
into the effectiveness of strategies employed during the match. Coaches can evaluate how
well their team is adapting to the opposition's tactics and make necessary adjustments to
improve formations and overall gameplay.
6. Data-Driven Decision Making:
The system empowers teams to make more informed, objective decisions by providing a
wealth of data and insights. This supports strategic planning, player performance
assessments, and real-time tactical changes, giving teams a competitive advantage on the
field.
7. Scalability and Versatility:
Whether applied to professional matches or amateur leagues, the system is adaptable to
different levels of competition. Its flexibility also allows it to be tailored to various analysis
objectives, such as player development or team performance enhancement.

8. Enhanced Post-Match Review:


The system's automated tracking and comprehensive metrics make post-match analysis
more effective. Coaches can easily review specific plays, formations, and player
movements, using this data to refine strategies for future matches.

School of CSA, REVA University, Bengaluru 11


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 4

SYSTEM DESIGN

4.1 HIGH LEVEL DESIGN (ARCHITECTURAL)

High-level design (architectural design) provides an overview of the system’s architecture


and how its components interact.

Fig 4.1.1: Architectural design

School of CSA, REVA University, Bengaluru 12


Football analysis with OpenCV, YOLO and Python 2024

Here’s a general outline for a high-level design of a project, such as your football analysis
system:

1. System Overview

• Objective: Analyze football matches using YOLO for object detection and OpenCV for
video processing.
• Components:
o Input Module
o Processing Module
o Output Module

2. Components and Interaction

Input Module

• Function: Handles video input and prepares it for processing.


• Components:
o Video Capture: Captures video feed from a file or camera.
o Preprocessing: Resizes, normalizes, or filters the video frames if needed.

Processing Module

• Function: Analyzes the video frames using YOLO and OpenCV.


• Components:
o YOLO Object Detection:
▪ YOLO Model: Pre-trained model or custom-trained model to detect players,
ball, and other relevant objects.
▪ Detection Pipeline: Processes frames and extracts bounding boxes for
detected objects.
o OpenCV Processing:
▪ Frame Manipulation: Annotates frames with detection results.
▪ Feature Extraction: Extracts additional features or statistics, such as player
positions or ball trajectories.

Output Module

• Function: Provides results and insights from the analysis.


• Components:
o Visualization: Displays annotated video frames or statistical data.

3. Data Flow

School of CSA, REVA University, Bengaluru 13


Football analysis with OpenCV, YOLO and Python 2024

1. Video Input: Video is captured and fed into the system.


2. Preprocessing: Video frames are prepared for analysis.
3. Detection: YOLO detects objects and generates bounding boxes.
4. Processing: OpenCV processes frames and extracts features.
5. Output: Results are visualized.

School of CSA, REVA University, Bengaluru 14


Football analysis with OpenCV, YOLO and Python 2024

4.2 LOW LEVEL DESIGN

Fig 4.2.1: Low level diagram

Low-level design (LLD) involves detailing the internal workings of each component
described in the high-level design. It focuses on the implementation aspects, including data
structures, algorithms, and interactions within each module.

Here’s a more detailed look at the low-level design for your football analysis system:

School of CSA, REVA University, Bengaluru 15


Football analysis with OpenCV, YOLO and Python 2024

Data Flow

• Video Input -> Preprocessing: Load and preprocess video frames.

• Preprocessing -> YOLO Object Detection: Detect objects in preprocessed frames.

• YOLO Object Detection -> Object Tracking: Track detected objects.

• Object Tracking -> Event Detection: Analyze tracking data to detect events.

• Event Detection -> Annotate Frame: Annotate frames with tracking and event data.

• Annotate Frame -> Save Video Frame: Save annotated frames to output video.

• Annotate Frame -> Display Results: Display the annotated frames (optional).

• Event Detection -> Save Event Data: Save event data to file.

School of CSA, REVA University, Bengaluru 16


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 5

DATA COLLECTION AND PREPARATION


Data Collection and Preparation is a crucial step in any machine learning or computer vision
project. For your football analysis project, it involves gathering video data, preparing it for processing,
and ensuring it's in a suitable format for analysis.

5.1 DATA SOURCES

• RoboFlow:

The dataset used for training the system in this project comes from Roboflow, a platform known
for providing high-quality, labeled datasets tailored for computer vision tasks. This dataset consists of
images related to football, which are used to train the YOLO model for detecting players, referees,
and the ball in various scenarios. The annotations provided with the dataset ensure accurate object
detection and tracking during analysis. By leveraging Roboflow's resources, the project benefits from
a well-curated and comprehensive dataset, contributing to the system's performance and accuracy.

The Dataset is downloaded using an API key in YOLOv5 format to train the model.

Website link:

https://universe.roboflow.com/roboflow-jvuqo/football-players-detection-bzlaf/dataset/1

Fig.5.1.1: Roboflow dataset sample

School of CSA, REVA University, Bengaluru 17


Football analysis with OpenCV, YOLO and Python 2024

• Kaggle:
Sample input videos titled DFL Bundesliga Shootout were downloaded from Kaggle.
These videos provided real-world football footage, which was used to test and evaluate the
performance of the developed model. The dataset was essential in validating the system's
ability to detect and track key elements such as players, referees, and the ball during actual
gameplay. By working with authentic match footage, the project effectively simulated real-life
conditions, allowing for fine-tuning of object detection and tracking algorithms. This helped
improve the system's accuracy in analyzing player movements, ball possession, and game
dynamics within a football match.
Website link: https://www.kaggle.com/competitions/dfl-bundesliga-data-shootout

5.2 DATA PROFILING


The dataset obtained from Roboflow comprises 650 annotated images, with labels for four
distinct object classes: ball, goalkeeper, player, and referee. Each image is carefully annotated to
highlight these objects, allowing for accurate detection and classification in subsequent analysis.
The dataset is split into three distinct parts: 80% of the images are allocated to the training set to
build the model, 13% are reserved for the validation set to fine-tune the model and avoid
overfitting, and the remaining 7% form the test set to evaluate the model’s performance.

Fig.5.2.1: Annotated image of all classes

School of CSA, REVA University, Bengaluru 18


Football analysis with OpenCV, YOLO and Python 2024

Fig.5.2.2: Annotated image with class ball

Fig.5.2.3: Annotated image with class referee

School of CSA, REVA University, Bengaluru 19


Football analysis with OpenCV, YOLO and Python 2024

Fig.5.2.4: Annotated image with class goalkeeper

Fig.5.2.5: Annotated image with class player

School of CSA, REVA University, Bengaluru 20


Football analysis with OpenCV, YOLO and Python 2024

The dataset is provided in YOLOv5 format, streamlining the process of training object
detection models. This format is optimized for use with YOLO models, making it easy to directly
integrate the data into the training pipeline. With the pre-annotated images and proper dataset
splitting, this setup ensures efficient and structured training, allowing for improved model
accuracy and faster development cycles.

5.3 DATA CLEANING AND PREPROCESSING

Data cleaning and preprocessing are essential steps in ensuring that the dataset is suitable for
training machine learning models. In this project, data cleaning involved identifying and removing
any inconsistencies, duplicates, or irrelevant data from the dataset to maintain the quality of the input.
This was necessary to avoid introducing noise into the model during training.

For the Roboflow dataset, this meant reviewing the annotations for accuracy, ensuring that all
instances of the classes (players, referees, goalkeepers, and the ball) were correctly labeled and
removing any mislabeled or incomplete data points. The Kaggle videos were also reviewed to ensure
that only relevant portions of gameplay were used.

Preprocessing included converting the dataset into a format suitable for the YOLO model, such
as resizing and normalizing images to ensure uniformity. The dataset was split into training, validation,
and test sets in an 80-13-7 ratio to ensure balanced training and evaluation phases. The annotations
were converted to the YOLO format, which involved creating bounding boxes around the detected
objects in each image. The videos from Kaggle were preprocessed by extracting frames and feeding
them into the YOLO model for object detection and tracking analysis.

These cleaning and preprocessing steps were crucial for improving the model's performance
and ensuring that it could accurately detect and track objects during a football match.

School of CSA, REVA University, Bengaluru 21


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 6

EXPLORATORY DATA ANALYSIS


Exploratory Data Analysis (EDA) is a critical step in understanding the dataset before building
any models. It involves summarizing the main characteristics of the data and uncovering underlying
patterns, trends, and relationships that might influence the model's performance. In this football
analysis project, EDA provided valuable insights into the structure and distribution of the data, helping
to inform decisions on model selection, feature engineering, and preprocessing.

1. Class Distribution Analysis

The first step in EDA involved analyzing the distribution of the different classes: players,
goalkeepers, referees, and the ball. This helped identify any imbalances in the dataset. A balanced
class distribution is essential for ensuring that the model does not overfit or underfit any specific class.
For instance, if one class, such as players, had significantly more instances than the others, the model
might perform better at detecting players but struggle with other classes.

2. Image Quality and Size Distribution

The dataset contained 650 annotated images, split into 80% training, 13% validation, and 7%
testing sets. EDA explored the image dimensions and quality to ensure consistency. Variations in
image sizes can affect model performance, especially in object detection tasks, so any significant
discrepancies in image quality were identified and addressed during preprocessing.

3. Bounding Box and Object Analysis

The annotated bounding boxes in the images were inspected for size and placement accuracy.
EDA revealed the average size of bounding boxes for each class, which helped in fine-tuning the
YOLO model. Small objects like the ball might require different settings compared to larger objects
like players or goalkeepers.

4. Object Density and Frequency

EDA also examined how frequently certain objects appeared in the images and how many
objects were present in each image. For instance, most images likely contained multiple players, while
only a few had a referee or the ball. This analysis helped understand the dataset’s complexity and
allowed for adjustments in model training to ensure all objects were accurately detected.

5. Temporal Distribution in Videos

For the videos downloaded from Kaggle (DFL Bundesliga Shootout), frame-by-frame analysis
was performed to check the distribution of events and key moments, such as when the ball was in play
or when significant player actions occurred. This step ensured that the video frames used for training

School of CSA, REVA University, Bengaluru 22


Football analysis with OpenCV, YOLO and Python 2024

were representative of typical football matches and contained enough diverse scenarios for the model
to learn from.

In summary, EDA provided a comprehensive understanding of the dataset's characteristics,


highlighted potential challenges like class imbalances or size variations, and guided the necessary
adjustments for model training. Through these insights, EDA enhanced the overall data preparation
process, improving the model's ability to accurately track players, referees, and the ball during football
matches.

6.1 DATA VISUALIZATION TECHNIQUES


Data Visualization Techniques play a crucial role in understanding and interpreting the
data during the exploratory data analysis (EDA) phase and model evaluation. In this football
analysis project, various visualization techniques were employed to gain insights into the dataset,
identify patterns, and assess the model's performance.
• Confusion Matrix:
After model training, a confusion matrix was used to visualize the performance of the
YOLO object detection model. This matrix provided insights into how well the model predicted
each class (player, ball, referee, etc.) and highlighted any misclassifications, guiding further fine-
tuning of the model.

Fig.6.1.1: Confusion matrix

School of CSA, REVA University, Bengaluru 23


Football analysis with OpenCV, YOLO and Python 2024

• Video Frame Analysis


Frame-by-frame visualizations of the video data allowed for the tracking of player, ball,
and referee movements across a match. This type of visualization showed the real-time predictions
made by the model on football video sequences, which was useful for evaluating its performance
on dynamic, real-world data.

Fig.6.1.2: Player tracking metrices

Fig.6.1.3: Ball tracking metrices

School of CSA, REVA University, Bengaluru 24


Football analysis with OpenCV, YOLO and Python 2024

6.2 UNIVARIATE AND BIVARIATE ANALYSIS


Univariate analysis involves examining and summarizing a single variable to understand
its distribution, central tendency, and dispersion, using techniques such as descriptive statistics and
visualizations like histograms. In contrast, bivariate analysis focuses on exploring the relationship
between two variables, assessing how changes in one variable are related to changes in the other.
This can be achieved through methods like correlation analysis, regression analysis, and
visualizations such as scatter plots, which help in understanding the nature and strength of the
association between the variables.

Fig.6.2.1: Counts of all annotated classes

Fig.6.2.2: Heat map of ball tracking

School of CSA, REVA University, Bengaluru 25


Football analysis with OpenCV, YOLO and Python 2024

Fig.6.2.3: Heat map of goalkeeper tracking

Fig.6.2.4: Heat map of referee tracking

School of CSA, REVA University, Bengaluru 26


Football analysis with OpenCV, YOLO and Python 2024

Fig.6.2.2: Heat map of player tracking

School of CSA, REVA University, Bengaluru 27


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 7

METHODOLOGIES

The objective of this project is to develop a system capable of real-time analysis of football
matches by detecting and tracking key elements such as players and the ball using YOLO (You
Only Look Once) object detection, OpenCV for video processing, and Python as the development
framework.

7.1 DATA MODELS

1. Data Collection

• Video Input:
o The system takes in either live football match footage or pre-recorded videos as
input. The video can be sourced from publicly available matches or custom
recordings.
o OpenCV is used to capture video frames, which are further processed for object
detection.

2. Pre-processing

• Frame Extraction:
o Each video is broken down into individual frames using OpenCV. This allows the
model to analyze each frame as an image for object detection.
• Frame Resizing:
o Frames are resized to fit the input dimensions required by the YOLO model (e.g.,
416x416 pixels for YOLOv3), ensuring consistent object detection.
• Normalization:
o The pixel values of the frames are normalized to ensure optimal performance of the
YOLO model, which improves detection accuracy and speed.

7.2 MODEL SELECTION

1. Object Detection Using YOLO

• YOLO Model Integration:


o YOLOv3 or YOLOv5 is integrated into the system to perform real-time object
detection. YOLO is used due to its speed and accuracy, making it suitable for the
fast-paced nature of football.

School of CSA, REVA University, Bengaluru 28


Football analysis with OpenCV, YOLO and Python 2024

o Pre-trained YOLO weights are used to detect players, the ball, and referees.
Bounding boxes are drawn around detected objects in each frame, and class labels
are assigned to the objects.
• Class Customization:
o The YOLO model is customized to focus on specific classes relevant to football
analysis, such as players, the ball, and referees. Additional classes can be added
based on the analysis needs.

Fig.7.2.1: Bounding box for an object

2. OpenCV

OpenCV is an open-source computer vision library that provides a comprehensive suite of


tools for image and video processing, including functions for filtering, transformation, feature
detection, and visualization.

• Preprocessing:

o Video Enhancement: OpenCV helps in preparing the input video for analysis by
performing tasks such as resizing to ensure consistent frame dimensions,
normalizing pixel values for uniformity, and applying filters to reduce noise. This
step is crucial for improving the performance and accuracy of the YOLOv5 model
by providing cleaner and more standardized input data.

School of CSA, REVA University, Bengaluru 29


Football analysis with OpenCV, YOLO and Python 2024

• Post-processing:
o Detection Visualization: After YOLOv5 detects objects (players, ball) in the video
frames, OpenCV is used to draw bounding boxes, labels, and other annotations on
these frames. This visualization aids in interpreting and validating detection results.
o Tracking and Analysis: OpenCV’s tracking algorithms can be employed to follow
the movement of players and the ball across frames. This helps in analyzing
dynamic events, such as player interactions and ball trajectories, by providing
continuous tracking data and insights.

3. Kmeans: Pixel segmentation and clustering to detect t-shirt color

• Pixel Segmentation with K-Means: K-Means clustering to segment pixels based on their
RGB values. Each pixel in the video frame is treated as a data point in a 3D color space
(representing red, green, and blue channels). By grouping similar-colored pixels together,
K-Means effectively segmented the dominant colors in the frame, such as the t-shirt colors
of players.
• Clustering for T-shirt Color Detection: K-Means with a chosen number of clusters (K)
corresponding to the expected number of distinct t-shirt colors on the field. This clustering
allowed me to group pixels of similar colors, identifying which pixels correspond to
players’ t-shirts, enabling clear differentiation between teams based on uniform color.

Fig.7.2.2: Clustered Uniform Color

7.3 MODEL BUILDING


In this football analysis project, the model-building phase centred around utilizing the
YOLO (You Only Look Once) object detection algorithm, enhanced by additional techniques to
provide accurate and insightful analysis of football games.

Steps in Model Building:

School of CSA, REVA University, Bengaluru 30


Football analysis with OpenCV, YOLO and Python 2024

1. Data Preparation
o The first step in building the model involved preparing the dataset, which consisted
of annotated images of players, referees, the ball, and the goalkeeper from
Roboflow. These images were split into training, validation, and test sets in an
80:13:7 ratio, ensuring that the model had sufficient data for learning and
evaluation.
o Data augmentation techniques were applied to the training images, such as random
rotations, flips, and scaling, to improve model generalization. Augmentation helps
simulate various real-world scenarios and increases the diversity of the training
data, reducing overfitting.
2. Model Selection (YOLO)
o The core of the model was YOLO, a state-of-the-art, real-time object detection
algorithm. YOLO was chosen because of its ability to detect multiple objects
(players, ball, referees) in a single pass over the image, making it highly efficient
for analyzing fast-paced sports like football.
o YOLO works by dividing an image into a grid and predicting bounding boxes for
the objects along with their class probabilities. The model was trained to detect
specific football-related classes, including "player," "referee," "ball," and
"goalkeeper."
3. Transfer Learning
o Transfer learning was implemented to leverage a pre-trained YOLO model,
specifically YOLOv5, which had already learned basic features from a large,
generic dataset like COCO. This approach significantly reduced training time and
improved accuracy by adapting the model to the specific football dataset.
o The pre-trained YOLOv5 model was fine-tuned using the football dataset, allowing
it to specialize in detecting the defined football objects with greater accuracy.
4. Training the Model
o The model was trained using the Roboflow dataset, with the goal of minimizing the
loss function, which measures the difference between the predicted and actual
bounding boxes and class labels.
o During training, key hyperparameters such as learning rate, batch size, number of
epochs, and image size were fine-tuned to optimize model performance.
5. Object Detection
o Once trained, the model was able to accurately detect and label players, referees,
and the ball within video frames. Bounding boxes around each object provided
exact locations of these elements, while class labels (player, ball, referee) ensured
correct identification.
o The YOLO model’s speed and accuracy were crucial for processing football videos,
where fast-moving objects and frequent player interactions demand quick and
precise detection.

School of CSA, REVA University, Bengaluru 31


Football analysis with OpenCV, YOLO and Python 2024

6. Post-Processing and Refinement


o After detection, post-processing techniques were applied to refine the results. Non-
maximum suppression (NMS) was used to remove duplicate bounding boxes,
ensuring that only the best bounding box for each object was retained.
o Optical flow was incorporated to track the movement of detected objects over time.
This was particularly useful for understanding player and ball movement patterns
during the game, enabling deeper tactical analysis.
7. Team Identification and Ball Possession
o To identify the teams, clustering algorithms (like K-Means) were used to segment
players based on their jersey colors. This allowed the system to differentiate
between players on opposing teams and provided insights into ball possession
metrics by associating the ball’s location with the nearest player and their team
affiliation.
8. Evaluation and Validation
o The model was evaluated using the validation set, with metrics such as precision,
recall, and mAP (mean average precision) being computed to assess its
performance. High precision and recall values indicated that the model was able to
detect football-related objects accurately, with minimal false positives and false
negatives.
o The confusion matrix was also generated to identify any misclassifications and
areas for improvement, such as distinguishing closely positioned players or
handling occlusions.
9. Performance Metrics Calculation
o The model also provided performance metrics such as speed and distance covered
by players during a match. By tracking player movements across frames, the system
calculated how fast players were moving and how much ground they covered,
offering detailed player performance insights.

7.4 RESULTS

For this football analysis project, the following evaluation metrics were used to determine how
well the model performed in detecting and tracking players, referees, and the ball, as well as in
delivering actionable insights.

School of CSA, REVA University, Bengaluru 32


Football analysis with OpenCV, YOLO and Python 2024

Table 7.4.1: Model Evaluation

Key Evaluation Metrics:

1. Precision
o Precision measures the accuracy of the positive predictions made by the model. In
object detection, precision refers to the proportion of correctly detected objects
(e.g., players, referees, or the ball) out of all the objects that the model predicted as
positive.
o Formula:

o A high precision score indicates that the model has fewer false positives, meaning
it accurately detects objects without misclassifying too many non-objects.
2. Recall
o Recall measures the model's ability to correctly identify all relevant objects in the
dataset. In object detection, this means detecting all players, referees, and the ball
present in the images.
o Formula:

oA high recall score indicates that the model is able to detect most, if not all, of the
relevant objects, minimizing missed detections.
3. Mean Average Precision (mAP)
o mAP is a key metric for object detection models. It computes the average precision
at various Intersection over Union (IoU) thresholds, giving a more comprehensive
view of the model’s accuracy in detecting objects across multiple scales.

School of CSA, REVA University, Bengaluru 33


Football analysis with OpenCV, YOLO and Python 2024

o IoU measures the overlap between the predicted bounding box and the ground truth
bounding box.
o Formula:

where N is the number of classes and APi is the average precision for class i.

o A higher mAP score indicates better overall performance of the model in detecting
and localizing objects.
4. Confusion Matrix
o A confusion matrix is a table used to evaluate the performance of a classification
model. It provides a visual representation of true positives, false positives, true
negatives, and false negatives.
o For this project, the confusion matrix would illustrate how well the model classified
objects into the four classes: players, referees, goalkeepers, and the ball.
o It helps identify where the model struggles, such as confusing players for referees
or misclassifying background objects as players.

Fig.7.4.1: Confusion matrix for ball detection

School of CSA, REVA University, Bengaluru 34


Football analysis with OpenCV, YOLO and Python 2024

Fig.7.4.1: Confusion matrix for all classes

School of CSA, REVA University, Bengaluru 35


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 8

TESTING

Fig.8.1: YOLO object detection with class and confidence

Fig.8.1: Output Screenshot with Player speed and distance covered, Ball control of each team,
Players numbers, Team t-shirt colored bounded box and ball acquisition

School of CSA, REVA University, Bengaluru 36


Football analysis with OpenCV, YOLO and Python 2024

Fig.8.1: Output Screenshot with player speed and distance covered

Fig.8.1: Ball acquisition mark for player 6

School of CSA, REVA University, Bengaluru 37


Football analysis with OpenCV, YOLO and Python 2024

Fig.8.1: Output Screenshot with no ball acquisition as no player is near the ball

School of CSA, REVA University, Bengaluru 38


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 9

CONCLUSION AND SCOPE FOR FUTURE ENCHANCEMENT


This project utilizes a range of advanced methodologies to achieve comprehensive football
analysis. YOLO, an AI-based object detection model, was employed to track players, referees, and
the ball by creating bounding boxes around them for accurate detection.
K-Means clustering was applied for pixel segmentation and T-shirt color detection,
enabling the system to differentiate between teams. To account for camera movement, Optical
Flow was used to measure and adjust for motion, ensuring consistent tracking.
Perspective transformation was implemented to accurately represent scene depth and
perspective on the football field, providing a more realistic understanding of player positions and
movements. Furthermore, speed and distance calculations for each player were derived using
spatial tracking data.
The project successfully integrates these methodologies into a system that offers accurate
player tracking, ball tracking, referee identification, team ball possession analysis, and player
performance metrics such as speed and distance covered. By combining object detection,
clustering, motion analysis, and geometric transformations, this system provides valuable insights
into football match dynamics, contributing to better game analysis and player evaluation.
SCOPE FOR FUTURE ENCHANCEMENT
The current system provides robust real-time football match analysis using YOLO,
OpenCV, and Python for object detection, tracking, and event detection. However, several areas
can be explored and improved in future iterations of the project to enhance its functionality,
scalability, and accuracy. The following are some potential enhancements:

Improved Player and Team Behaviour Analysis

• Tactical Analysis and Formations: The system can be enhanced to automatically recognize
and analyze team formations, transitions between offensive and defensive strategies, and
positional heatmaps. This would provide coaches and analysts with detailed insights into
team dynamics and overall tactics.
• Player Performance Metrics: Additional performance metrics can be extracted, such as
player stamina, speed, and distance covered throughout the match. This data could be
valuable for individual player performance analysis.

Pose Estimation and Action Recognition

• Pose Estimation Models: Integrating pose estimation techniques could allow for the
identification of specific player movements, such as dribbling, kicking, or defensive

School of CSA, REVA University, Bengaluru 39


Football analysis with OpenCV, YOLO and Python 2024

postures. Pose estimation models like OpenPose could help in identifying player positions
with higher granularity.
• Action Recognition Models: Future versions of the system could detect and classify player
actions, such as passing, shooting, or tackling, using action recognition models. This would
allow for more detailed player performance metrics and tactical analysis.

Multi-Camera Integration

• Multi-Angle Video Analysis: By integrating footage from multiple camera angles, the
system could provide a 3D analysis of the game. This would improve object detection
accuracy, especially in scenarios where one camera angle might obstruct the view of
players or the ball.
• 3D Player Tracking: Multi-camera systems could enable 3D reconstruction of the football
field, allowing more accurate tracking of players' positions, distances between players, and
ball trajectories.

Cross-Sport Adaptation

• Generalizing to Other Sports: While the system is currently designed for football, the
underlying methodology can be adapted to other sports such as basketball, tennis, or
hockey. This would allow for broader applications and the ability to analyze multiple types
of team sports using a similar framework.

Real-Time Referee Assistance

• Automated Referee Decisions: The system could be enhanced to assist referees in real-time
by identifying fouls, offsides, and other infringements more accurately, potentially
reducing human error in officiating decisions. This could be implemented as a tool for
Video Assistant Referee (VAR) systems.
• Instant Replay and Review System: The system could offer an instant replay function,
highlighting critical moments in the match for referees to review in real-time, providing
additional insights into their decision-making process.

School of CSA, REVA University, Bengaluru 40


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 10

BIBLIOGRAPHY

Journal Articles:

1. Morgulev E, Azar OH, Lidor R. Sports analytics and the big-data era. International Journal of
Data Science and Analytics. 2018; 5:213-22.
2. Mackenzie R, Cushion C. Performance analysis in football: A critical review and
implications for future research. Journal of sports sciences. 2013;31(6):639-76.
3. Gong B, Cui Y, Zhang S, Zhou C, Yi Q, Go´mez-Ruano MA´. Impact of technical and
physical key performance indicators on ball possession in the Chinese Super League.
International Journal of Performance Analysis in Sport. 2021;21(6):909-21.
4. Lago-Pen˜as C, Dellal A. Ball possession strategies in elite soccer according to the evolu-
tion of the match-score: the influence of situational variables. Journal of human kinetics.
2010;25(2010):93-100.
5. Bradley PS, Lago-Pen˜as C, Rey E, Sampaio J. The influence of situational variables on ball
possession in the English Premier League. Journal of Sports Sciences. 2014;32(20):1867-73.
6. Dimopoulos GR. Implementing Multi-Class Object Detection in Soccer Matches Through
YOLOv5.
7. Cioppa A, Giancola S, Delie`ge A, Kang L, Zhou X, Cheng Z, et al. SoccerNet-Tracking:
Multiple Object Tracking Dataset and Benchmark in Soccer Videos. In: Proceedings of the
IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR) Workshops;
2022. p. 3491-502.
8. Yu X, Xu C, Leong HW, Tian Q, Tang Q, Wan KW. Trajectory-based ball detection and
tracking with applications to semantic analysis of broadcast soccer video. In: Proceedings of
the eleventh ACM international conference on Multimedia; 2003. p. 11-20.
9. Cortes C, Vapnik V. Support-vector networks. Machine learning. 1995;20: 273-97.
10. Castellano J, Casamichana D, Lago C. The use of match statistics that discriminate between
successful and unsuccessful soccer teams. Journal of human kinetics. 2012; 31:139.

School of CSA, REVA University, Bengaluru 41


Football analysis with OpenCV, YOLO and Python 2024

Conference Papers:

1. Iyer GN, Bala VS, Sohan B, Dharmesh R, Raman V. Automated third umpire decision
making in cricket using machine learning techniques. In: 2020 4th International Conference
on Intelligent Computing and Control Systems (ICICCS). IEEE; 2020. p. 1216-21.

Online Resources:

1. URL: https://docs.ultralytics.com. Ultralytics. YOLOv5: A state-of-the-art real-time object


detection system; 2021. (First Accessed on: 20-06-2024)
2. URL: https://kaggle.com/competitions/dfl-bundesliga-data-shootout. DFL - Bundesliga Data
Shootout. Kaggle; 2022. (First Accessed on: 23-08-2024)
3. URL: https://universe.roboflow.com/roboflow-jvuqo/football-players-detection-bzlaf/dataset
Roboflow training dataset. (First Accessed on: 03-07-2024)

School of CSA, REVA University, Bengaluru 42


Football analysis with OpenCV, YOLO and Python 2024

CHAPTER 11

APPENDIX
1. Sample Source Code/Pseudo Code

i. main.py:
from utils import read_video, save_video

from trackers import Tracker

import cv2

import numpy as np

from team_assigner import TeamAssigner

from player_ball_assigner import PlayerBallAssigner

from camera_movement_estimator import CameraMovementEstimator

from view_transformer import ViewTransformer

from speed_and_distance_estimator import SpeedAndDistance_Estimator

def main():

# Read Video

video_frames = read_video('input_videos/08fd33_4.mp4')

# Initialize Tracker

tracker = Tracker('models/best.pt')

tracks = tracker.get_object_tracks(video_frames,

read_from_stub=True,

stub_path='stubs/track_stubs.pkl')

# Get object positions

tracker.add_position_to_tracks(tracks)

# camera movement estimator

School of CSA, REVA University, Bengaluru 43


Football analysis with OpenCV, YOLO and Python 2024

camera_movement_estimator = CameraMovementEstimator(video_frames[0])

camera_movement_per_frame=camera_movement_estimator.get_camera_movement(video_fra
mes,

read_from_stub=True,

stub_path='stubs/camera_movement_stub.pkl')

camera_movement_estimator.add_adjust_positions_to_tracks(tracks,camera_movement_per_f
rame)

# View Trasnformer

view_transformer = ViewTransformer()

view_transformer.add_transformed_position_to_tracks(tracks)

# Interpolate Ball Positions

tracks["ball"] = tracker.interpolate_ball_positions(tracks["ball"])

# Speed and distance estimator

speed_and_distance_estimator = SpeedAndDistance_Estimator()

speed_and_distance_estimator.add_speed_and_distance_to_tracks(tracks)

# Assign Player Teams

team_assigner = TeamAssigner()

team_assigner.assign_team_color(video_frames[0],

tracks['players'][0])

for frame_num, player_track in enumerate(tracks['players']):

for player_id, track in player_track.items():

team = team_assigner.get_player_team(video_frames[frame_num],

track['bbox'],

player_id)

tracks['players'][frame_num][player_id]['team'] = team

tracks['players'][frame_num][player_id]['team_color'] = team_assigner.team_colors[team]

School of CSA, REVA University, Bengaluru 44


Football analysis with OpenCV, YOLO and Python 2024

# Assign Ball Aquisition

player_assigner =PlayerBallAssigner()

team_ball_control= []

for frame_num, player_track in enumerate(tracks['players']):

ball_bbox = tracks['ball'][frame_num][1]['bbox']

assigned_player = player_assigner.assign_ball_to_player(player_track, ball_bbox)

if assigned_player != -1:

tracks['players'][frame_num][assigned_player]['has_ball'] = True

team_ball_control.append(tracks['players'][frame_num][assigned_player]['team'])

else:

team_ball_control.append(team_ball_control[-1])

team_ball_control= np.array(team_ball_control)

# Draw output

## Draw object Tracks

output_video_frames = tracker.draw_annotations(video_frames, tracks,team_ball_control)

## Draw Camera movement

output_video_frames=
camera_movement_estimator.draw_camera_movement(output_video_frames,camera_movement
_per_frame)

## Draw Speed and Distance

speed_and_distance_estimator.draw_speed_and_distance(output_video_frames,tracks)

# Save video

save_video(output_video_frames, 'output_videos/output_video.avi')

if __name__ == '__main__':

main()

ii. yolo_inference.py:

School of CSA, REVA University, Bengaluru 45


Football analysis with OpenCV, YOLO and Python 2024

from ultralytics import YOLO

model = YOLO('models/best.pt')

results = model.predict('input_videos/08fd33_4.mp4',save=True)

print(results[0])

print('=====================================')

for box in results[0].boxes:

print(box)

Utils:

iii. video_utils.py:
import cv2
def read_video(video_path):
cap = cv2.VideoCapture(video_path)
frames = []
while True:
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
return frames
def save_video(ouput_video_frames,output_video_path):
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(output_video_path, fourcc, 24, (ouput_video_frames[0].shape[1],
ouput_video_frames[0].shape[0]))
for frame in ouput_video_frames:
out.write(frame)
out.release()

School of CSA, REVA University, Bengaluru 46


Football analysis with OpenCV, YOLO and Python 2024

iv. bbox_utils.py:
def get_center_of_bbox(bbox):
x1,y1,x2,y2 = bbox
return int((x1+x2)/2),int((y1+y2)/2)
def get_bbox_width(bbox):
return bbox[2]-bbox[0]
def measure_distance(p1,p2):
return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)**0.5
def measure_xy_distance(p1,p2):
return p1[0]-p2[0],p1[1]-p2[1]
def get_foot_position(bbox):
x1,y1,x2,y2 = bbox
return int((x1+x2)/2),int(y2)

Training

football_training_yolo_v5.ipynb:

!pip install ultralytics


!pip install roboflow
from roboflow import Roboflow
rf = Roboflow(api_key="3205MH29k2z3u5Ejc3HU") # THIS API KEY IS REVOKED.
project = rf.workspace("roboflow-jvuqo").project("football-players-detection-3zvbc")
version = project.version(1)
dataset = version.download("yolov5")
import shutil
shutil.move('football-players-detection-1/train',
'football-players-detection-1/football-players-detection-1/train'
)

School of CSA, REVA University, Bengaluru 47


Football analysis with OpenCV, YOLO and Python 2024

shutil.move('football-players-detection-1/test',
'football-players-detection-1/football-players-detection-1/test'
)
shutil.move('football-players-detection-1/valid',
'football-players-detection-1/football-players-detection-1/valid'
)
!yolo task=detect mode=train model=yolov5x.pt data={dataset.location}/data.yaml epochs=100
imgsz=640

Trackers:
trackers.py:
from ultralytics import YOLO
import supervision as sv
import pickle
import os
import numpy as np
import pandas as pd
import cv2
import sys
sys.path.append('../')
from utils import get_center_of_bbox, get_bbox_width, get_foot_position

class Tracker:
def __init__(self, model_path):
self.model = YOLO(model_path)
self.tracker = sv.ByteTrack()

def add_position_to_tracks(sekf,tracks):

School of CSA, REVA University, Bengaluru 48


Football analysis with OpenCV, YOLO and Python 2024

for object, object_tracks in tracks.items():


for frame_num, track in enumerate(object_tracks):
for track_id, track_info in track.items():
bbox = track_info['bbox']
if object == 'ball':
position= get_center_of_bbox(bbox)
else:
position = get_foot_position(bbox)
tracks[object][frame_num][track_id]['position'] = position

def interpolate_ball_positions(self,ball_positions):
ball_positions = [x.get(1,{}).get('bbox',[]) for x in ball_positions]
df_ball_positions = pd.DataFrame(ball_positions,columns=['x1','y1','x2','y2'])

# Interpolate missing values


df_ball_positions = df_ball_positions.interpolate()
df_ball_positions = df_ball_positions.bfill()

ball_positions = [{1: {"bbox":x}} for x in df_ball_positions.to_numpy().tolist()]

return ball_positions

def detect_frames(self, frames):


batch_size=20
detections = []
for i in range(0,len(frames),batch_size):
detections_batch = self.model.predict(frames[i:i+batch_size],conf=0.1)
detections += detections_batch

School of CSA, REVA University, Bengaluru 49


Football analysis with OpenCV, YOLO and Python 2024

return detections

def get_object_tracks(self, frames, read_from_stub=False, stub_path=None):

if read_from_stub and stub_path is not None and os.path.exists(stub_path):


with open(stub_path,'rb') as f:
tracks = pickle.load(f)
return tracks

detections = self.detect_frames(frames)

tracks={
"players":[],
"referees":[],
"ball":[]
}

for frame_num, detection in enumerate(detections):


cls_names = detection.names
cls_names_inv = {v:k for k,v in cls_names.items()}

# Covert to supervision Detection format


detection_supervision = sv.Detections.from_ultralytics(detection)

# Convert GoalKeeper to player object


for object_ind , class_id in enumerate(detection_supervision.class_id):
if cls_names[class_id] == "goalkeeper":
detection_supervision.class_id[object_ind] = cls_names_inv["player"]

School of CSA, REVA University, Bengaluru 50


Football analysis with OpenCV, YOLO and Python 2024

# Track Objects
detection_with_tracks = self.tracker.update_with_detections(detection_supervision)

tracks["players"].append({})
tracks["referees"].append({})
tracks["ball"].append({})

for frame_detection in detection_with_tracks:


bbox = frame_detection[0].tolist()
cls_id = frame_detection[3]
track_id = frame_detection[4]

if cls_id == cls_names_inv['player']:
tracks["players"][frame_num][track_id] = {"bbox":bbox}

if cls_id == cls_names_inv['referee']:
tracks["referees"][frame_num][track_id] = {"bbox":bbox}

for frame_detection in detection_supervision:


bbox = frame_detection[0].tolist()
cls_id = frame_detection[3]

if cls_id == cls_names_inv['ball']:
tracks["ball"][frame_num][1] = {"bbox":bbox}

if stub_path is not None:


with open(stub_path,'wb') as f:

School of CSA, REVA University, Bengaluru 51


Football analysis with OpenCV, YOLO and Python 2024

pickle.dump(tracks,f)

return tracks

def draw_ellipse(self,frame,bbox,color,track_id=None):
y2 = int(bbox[3])
x_center, _ = get_center_of_bbox(bbox)
width = get_bbox_width(bbox)

cv2.ellipse(
frame,
center=(x_center,y2),
axes=(int(width), int(0.35*width)),
angle=0.0,
startAngle=-45,
endAngle=235,
color = color,
thickness=2,
lineType=cv2.LINE_4
)

rectangle_width = 40
rectangle_height=20
x1_rect = x_center - rectangle_width//2
x2_rect = x_center + rectangle_width//2
y1_rect = (y2- rectangle_height//2) +15
y2_rect = (y2+ rectangle_height//2) +15

School of CSA, REVA University, Bengaluru 52


Football analysis with OpenCV, YOLO and Python 2024

if track_id is not None:


cv2.rectangle(frame,
(int(x1_rect),int(y1_rect) ),
(int(x2_rect),int(y2_rect)),
color,
cv2.FILLED)

x1_text = x1_rect+12
if track_id > 99:
x1_text -=10

cv2.putText(
frame,
f"{track_id}",
(int(x1_text),int(y1_rect+15)),
cv2.FONT_HERSHEY_SIMPLEX,
0.6,
(0,0,0),
2
)

return frame

def draw_traingle(self,frame,bbox,color):
y= int(bbox[1])
x,_ = get_center_of_bbox(bbox)

triangle_points = np.array([

School of CSA, REVA University, Bengaluru 53


Football analysis with OpenCV, YOLO and Python 2024

[x,y],
[x-10,y-20],
[x+10,y-20],
])
cv2.drawContours(frame, [triangle_points],0,color, cv2.FILLED)
cv2.drawContours(frame, [triangle_points],0,(0,0,0), 2)

return frame

def draw_team_ball_control(self,frame,frame_num,team_ball_control):
# Draw a semi-transparent rectaggle
overlay = frame.copy()
cv2.rectangle(overlay, (1350, 850), (1900,970), (255,255,255), -1 )
alpha = 0.4
cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame)

team_ball_control_till_frame = team_ball_control[:frame_num+1]
# Get the number of time each team had ball control
team_1_num_frames =
team_ball_control_till_frame[team_ball_control_till_frame==1].shape[0]
team_2_num_frames =
team_ball_control_till_frame[team_ball_control_till_frame==2].shape[0]
team_1 = team_1_num_frames/(team_1_num_frames+team_2_num_frames)
team_2 = team_2_num_frames/(team_1_num_frames+team_2_num_frames)

cv2.putText(frame, f"Team 1 Ball Control: {team_1*100:.2f}%",(1400,900),


cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 3)
cv2.putText(frame, f"Team 2 Ball Control: {team_2*100:.2f}%",(1400,950),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 3)

School of CSA, REVA University, Bengaluru 54


Football analysis with OpenCV, YOLO and Python 2024

return frame

def draw_annotations(self,video_frames, tracks,team_ball_control):


output_video_frames= []
for frame_num, frame in enumerate(video_frames):
frame = frame.copy()

player_dict = tracks["players"][frame_num]
ball_dict = tracks["ball"][frame_num]
referee_dict = tracks["referees"][frame_num]

# Draw Players
for track_id, player in player_dict.items():
color = player.get("team_color",(0,0,255))
frame = self.draw_ellipse(frame, player["bbox"],color, track_id)

if player.get('has_ball',False):
frame = self.draw_traingle(frame, player["bbox"],(0,0,255))

# Draw Referee
for _, referee in referee_dict.items():
frame = self.draw_ellipse(frame, referee["bbox"],(0,255,255))

# Draw ball
for track_id, ball in ball_dict.items():
frame = self.draw_traingle(frame, ball["bbox"],(0,255,0))

School of CSA, REVA University, Bengaluru 55


Football analysis with OpenCV, YOLO and Python 2024

# Draw Team Ball Control


frame = self.draw_team_ball_control(frame, frame_num, team_ball_control)

output_video_frames.append(frame)

return output_video_frames
Team Assigner:
team_assigner.py:
from sklearn.cluster import KMeans

class TeamAssigner:
def __init__(self):
self.team_colors = {}
self.player_team_dict = {}

def get_clustering_model(self,image):
# Reshape the image to 2D array
image_2d = image.reshape(-1,3)

# Preform K-means with 2 clusters


kmeans = KMeans(n_clusters=2, init="k-means++",n_init=1)
kmeans.fit(image_2d)

return kmeans

def get_player_color(self,frame,bbox):
image = frame[int(bbox[1]):int(bbox[3]),int(bbox[0]):int(bbox[2])]

School of CSA, REVA University, Bengaluru 56


Football analysis with OpenCV, YOLO and Python 2024

top_half_image = image[0:int(image.shape[0]/2),:]

# Get Clustering model


kmeans = self.get_clustering_model(top_half_image)

# Get the cluster labels forr each pixel


labels = kmeans.labels_

# Reshape the labels to the image shape


clustered_image = labels.reshape(top_half_image.shape[0],top_half_image.shape[1])

# Get the player cluster


corner_clusters = [clustered_image[0,0],clustered_image[0,-1],clustered_image[-
1,0],clustered_image[-1,-1]]
non_player_cluster = max(set(corner_clusters),key=corner_clusters.count)
player_cluster = 1 - non_player_cluster

player_color = kmeans.cluster_centers_[player_cluster]

return player_color

def assign_team_color(self,frame, player_detections):

player_colors = []
for _, player_detection in player_detections.items():
bbox = player_detection["bbox"]

School of CSA, REVA University, Bengaluru 57


Football analysis with OpenCV, YOLO and Python 2024

player_color = self.get_player_color(frame,bbox)
player_colors.append(player_color)

kmeans = KMeans(n_clusters=2, init="k-means++",n_init=10)


kmeans.fit(player_colors)

self.kmeans = kmeans

self.team_colors[1] = kmeans.cluster_centers_[0]
self.team_colors[2] = kmeans.cluster_centers_[1]

def get_player_team(self,frame,player_bbox,player_id):
if player_id in self.player_team_dict:
return self.player_team_dict[player_id]

player_color = self.get_player_color(frame,player_bbox)

team_id = self.kmeans.predict(player_color.reshape(1,-1))[0]
team_id+=1

if player_id ==91:
team_id=1

self.player_team_dict[player_id] = team_id

return team_id

School of CSA, REVA University, Bengaluru 58


Football analysis with OpenCV, YOLO and Python 2024

speed_and_distance.py:
import cv2
import sys
sys.path.append('../')
from utils import measure_distance ,get_foot_position

class SpeedAndDistance_Estimator():
def __init__(self):
self.frame_window=5
self.frame_rate=24

def add_speed_and_distance_to_tracks(self,tracks):
total_distance= {}

for object, object_tracks in tracks.items():


if object == "ball" or object == "referees":
continue
number_of_frames = len(object_tracks)
for frame_num in range(0,number_of_frames, self.frame_window):
last_frame = min(frame_num+self.frame_window,number_of_frames-1 )

for track_id,_ in object_tracks[frame_num].items():


if track_id not in object_tracks[last_frame]:
continue

start_position = object_tracks[frame_num][track_id]['position_transformed']
end_position = object_tracks[last_frame][track_id]['position_transformed']

School of CSA, REVA University, Bengaluru 59


Football analysis with OpenCV, YOLO and Python 2024

if start_position is None or end_position is None:


continue

distance_covered = measure_distance(start_position,end_position)
time_elapsed = (last_frame-frame_num)/self.frame_rate
speed_meteres_per_second = distance_covered/time_elapsed
speed_km_per_hour = speed_meteres_per_second*3.6

if object not in total_distance:


total_distance[object]= {}

if track_id not in total_distance[object]:


total_distance[object][track_id] = 0

total_distance[object][track_id] += distance_covered

for frame_num_batch in range(frame_num,last_frame):


if track_id not in tracks[object][frame_num_batch]:
continue
tracks[object][frame_num_batch][track_id]['speed'] = speed_km_per_hour
tracks[object][frame_num_batch][track_id]['distance'] =
total_distance[object][track_id]

def draw_speed_and_distance(self,frames,tracks):
output_frames = []
for frame_num, frame in enumerate(frames):
for object, object_tracks in tracks.items():
if object == "ball" or object == "referees":

School of CSA, REVA University, Bengaluru 60


Football analysis with OpenCV, YOLO and Python 2024

continue
for _, track_info in object_tracks[frame_num].items():
if "speed" in track_info:
speed = track_info.get('speed',None)
distance = track_info.get('distance',None)
if speed is None or distance is None:
continue

bbox = track_info['bbox']
position = get_foot_position(bbox)
position = list(position)
position[1]+=40

position = tuple(map(int,position))
cv2.putText(frame, f"{speed:.2f}
km/h",position,cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),2)
cv2.putText(frame, f"{distance:.2f}
m",(position[0],position[1]+20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),2)
output_frames.append(frame)

return output_frames

player_ball_assigner.py:

import sys

sys.path.append('../')

from utils import get_center_of_bbox, measure_distance

class PlayerBallAssigner():

def __init__(self):

School of CSA, REVA University, Bengaluru 61


Football analysis with OpenCV, YOLO and Python 2024

self.max_player_ball_distance = 70

def assign_ball_to_player(self,players,ball_bbox):

ball_position = get_center_of_bbox(ball_bbox)

miniumum_distance = 99999

assigned_player=-1

for player_id, player in players.items():

player_bbox = player['bbox']

distance_left = measure_distance((player_bbox[0],player_bbox[-1]),ball_position)

distance_right = measure_distance((player_bbox[2],player_bbox[-1]),ball_position)

distance = min(distance_left,distance_right)

if distance < self.max_player_ball_distance:

if distance < miniumum_distance:

miniumum_distance = distance

assigned_player = player_id

return assigned_player

view_transformer.py:

import numpy as np

import cv2

class ViewTransformer():

def __init__(self):

court_width = 68

School of CSA, REVA University, Bengaluru 62


Football analysis with OpenCV, YOLO and Python 2024

court_length = 23.32

self.pixel_vertices = np.array([[110, 1035],

[265, 275],

[910, 260],

[1640, 915]])

self.target_vertices = np.array([

[0,court_width],

[0, 0],

[court_length, 0],

[court_length, court_width]

])

self.pixel_vertices = self.pixel_vertices.astype(np.float32)

self.target_vertices = self.target_vertices.astype(np.float32)

self.persepctive_trasnformer = cv2.getPerspectiveTransform(self.pixel_vertices,
self.target_vertices)

def transform_point(self,point):

p = (int(point[0]),int(point[1]))

is_inside = cv2.pointPolygonTest(self.pixel_vertices,p,False) >= 0

if not is_inside:

return None

School of CSA, REVA University, Bengaluru 63


Football analysis with OpenCV, YOLO and Python 2024

reshaped_point = point.reshape(-1,1,2).astype(np.float32)

tranform_point = cv2.perspectiveTransform(reshaped_point,self.persepctive_trasnformer)

return tranform_point.reshape(-1,2)

def add_transformed_position_to_tracks(self,tracks):

for object, object_tracks in tracks.items():

for frame_num, track in enumerate(object_tracks):

for track_id, track_info in track.items():

position = track_info['position_adjusted']

position = np.array(position)

position_trasnformed = self.transform_point(position)

if position_trasnformed is not None:

position_trasnformed = position_trasnformed.squeeze().tolist()

tracks[object][frame_num][track_id]['position_transformed'] = position_trasnformed

ii) Plagiarism Report:

School of CSA, REVA University, Bengaluru 64

You might also like