How AI Watches Football: 5 Clever Techniques from a Deep-Dive Tutorial
When you’re watching a football match, it’s easy to get lost in the on-screen graphics that pop up in real-time. Player positions appearing on a mini-map, heatmaps showing field control, or a clean trajectory line tracing a free kick—these augmentations have become a seamless part of the viewing experience. As a data scientist, my focus immediately shifts from the players to the pipeline. I find myself thinking less about the match and more about the systems powering these visualizations.
Have you ever wondered what it takes to build such a system from scratch? It’s a fascinatingly complex problem that involves far more than just detecting players on a pitch. It requires a clever combination of different AI models, classical computer vision techniques, and practical, common-sense heuristics to tie it all together.
Recently, I came across a comprehensive “Football AI Tutorial” by Roboflow that builds a complete sports analytics pipeline, going from basic object detection to advanced statistics. I was struck by the elegant solutions to several non-obvious challenges. This article distills the five most surprising and impactful techniques from that project, revealing how object detection, semantic clustering, and geometric transforms must work in perfect concert to produce a single, coherent output.

1. You Don’t Sort Teams by Jersey Color, You Cluster Them in High-Dimensional Space
1.1. The Problem with the “Simple” Approach
The most intuitive way to separate players into two teams is to look at the color of their jerseys. It seems simple enough, but in practice, this method is incredibly unreliable. The source context highlights several reasons why this approach fails:
- A cropped image of a player inevitably includes the background—grass, spectator stands, or even other players.
- A player’s pose is constantly changing, which alters the proportion of jersey pixels to background pixels in the crop.
- Lighting conditions are not uniform across the pitch, causing color values to shift and change dynamically.
A simple color-averaging heuristic would be brittle and prone to error, failing the moment conditions aren’t perfect.
1.2. The “Over-Engineered” but Perfect Solution
The tutorial opts for a more complex but far more robust machine-learning-driven solution. Here’s how it works:
- For each detected player, a
S-CLIPmodel is used to generate a 768-dimensional “embedding vector.”S-CLIPwas chosen over the more commonCLIPbecause, as the source notes, “it’s more computationally efficient making it a better choice for realtime applications.” - These embeddings are powerful because they don’t just represent color; they capture the deeper “semantic meaning” of the image. This makes them resilient to variations in a player’s pose, lighting changes, or partial occlusions.
- As the tutorial explains, this is the key to the method’s robustness:
- With these high-dimensional vectors in hand, a technique called
UMAPis used to reduce the 768 dimensions down to a more manageable 3 dimensions, preserving the relationships between the data points. Finally, a standardK-meansclustering algorithm groups the players into two distinct teams.
This is a powerful technique because it trades a simple but fragile heuristic for a complex but incredibly reliable ML pipeline. It’s a fantastic lesson for any practitioner: when dealing with noisy visual data, abstracting features into a robust semantic space is a far more scalable pattern than building brittle, rule-based systems that rely on raw pixel values.
2. Creating a “God’s Eye” Radar View Relies on Smart Landmark Detection
2.1. The Magic of Homography
The core technique for creating the top-down, “video game style radar view” is called homography. In simple terms, homography is a mathematical transformation that maps points from one plane to another. It can effectively “undo” the perspective distortion of a camera, allowing you to take the angled view from the broadcast and map every player’s position onto a flat, 2D representation of the pitch.
2.2. The Challenge of a Moving Camera
Homography is straightforward if the camera is static. You can manually identify a few corresponding points between the camera’s view and a map of the pitch, calculate a transformation matrix once, and apply it to every frame. However, in sports broadcasts, the camera is constantly panning, tilting, and zooming to follow the action. This means the transformation matrix needs to be recalculated for every single frame. To do this, the system needs to know the location of at least four corresponding points on both the video frame and the real-world pitch map at all times.
2.3. The AI-Powered Solution
The tutorial’s solution to this dynamic challenge is elegant. Instead of trying to find reference points with classical computer vision, it uses a dedicated AI model for the job:
- A custom YOLOv8 keypoint detection model was trained to automatically identify 32 specific, characteristic points on the football pitch in every frame—things like the corners, penalty spots, and the center circle. A fascinating and practical detail from the training process was the need to disable “Mosaic augmentations,” as the creator noted they “misled our model making it expect multiple Peaches on one frame.”
- Because these landmarks have known, fixed locations on a standard football pitch, detecting them in the video frame provides the constantly updating set of reference pairs needed to calculate the homography matrix dynamically.
This is a lesson in system design: sometimes the most elegant solution is to train a secondary “helper” model whose sole purpose is to provide context—in this case, spatial context—that enables the primary models to function. This spatial awareness is the bridge that allows the player and ball detections from other models to be translated from simple bounding boxes on a screen into meaningful coordinates on a map.
3. The Secret to Finding a Tiny, Blurry Football Isn’t a Better Model, It’s a Bigger Image
3.1. The Ball Detection Challenge
Reliably detecting the football is one of the hardest parts of the project. The source outlines several key difficulties:
- It’s small: The ball often occupies just a few dozen pixels in a high-resolution 1920×1080 frame.
- It’s fast: The ball’s rapid movement frequently results in significant motion blur, making it hard for a model to identify.
- It’s in a cluttered environment: When the ball is high in the air, the background is a noisy crowd of spectators, which can easily confuse a detection model.
3.2. The Counter-Intuitive Fix
Instead of reaching for a more complex model architecture, the tutorial implements a surprisingly simple but highly effective fix related to data processing.
- The default input resolution for the YOLOv8 model is 640×640 pixels. When a full 1920×1080 frame is rescaled down to this size, the tiny number of pixels representing the ball is reduced “by four to five times,” effectively erasing the very information the model needs.
- The solution was to increase the model’s training and inference input resolution from 640×640 to 1280×1280. This larger input size ensures that the number of pixels representing the ball remains much closer to the original, which significantly boosts detection accuracy.
- Of course, this comes with a trade-off: using a higher resolution leads to slower training and inference times and requires more GPU memory. This forced an adjustment to a smaller batch size during the training process to avoid running out of memory.
This takeaway is a classic “data-centric AI” win. It’s a crucial lesson for any ML engineer facing a performance plateau: optimizing the data pipeline can yield greater returns than endlessly tweaking model architecture.
4. Assigning Goalkeepers to Teams with a Simple Centroid Heuristic
4.1. The Goalkeeper Problem
The sophisticated embedding-based team classification works beautifully for outfield players. However, it fails for goalkeepers, who wear different colored uniforms from the rest of their team and would be clustered incorrectly.
4.2. The Geometric Solution
Rather than trying to force the embedding model to handle this edge case, the tutorial implements a clever and simple geometric heuristic to solve it:
- First, the outfield players are classified into Team A and Team B using the embedding method. For this calculation, a player’s position is precisely defined as the point “in the middle of the bottom edge of the bounding box,” representing their contact point with the ground.
- Next, the geometric “centroid” (the average x,y position) is calculated for all players in Team A and all players in Team B.
- For each detected goalkeeper, the system calculates the distance from the goalkeeper to the centroid of Team A and the centroid of Team B.
- The goalkeeper is assigned to the team whose centroid is closer.
This solution works because of a logical assumption: a defending team will, on average, be positioned closer to its own goalkeeper than the attacking team is. This demonstrates real engineering maturity: knowing when to stop trying to force a complex model to handle every edge case, and instead applying a simple, interpretable, and computationally cheap rule to solve it. This is the art of practical system building.
5. Even Advanced AI Needs Simple Rules to Clean Up the Final Output
5.1. The Wobbly Trajectory
After all the heavy lifting is done, the resulting data still isn’t perfect. The wobbly ball trajectory is a direct result of errors propagating through the pipeline: small inaccuracies from the ball detector (Takeaway 3) are fed through the homography transformation (Takeaway 2). Furthermore, minor “vibrations” in the keypoint detections can cause the homography matrix itself to be unstable frame-to-frame. The occasional misdetection can cause “spikes” in the data, where the ball appears to jump to an impossible location for a single frame before returning.
5.2. The Last-Mile Filter
To fix this, the tutorial uses two layers of cleaning. First, to stabilize the homography itself, the matrix values are averaged over a 5-frame time window to smooth out keypoint vibrations. Then, a final, straightforward data cleaning function is applied to the ball’s path:
- The function iterates through the list of the ball’s detected positions, frame by frame.
- In each step, it calculates the distance the ball appears to have moved between the current frame and the previous one.
- If this distance exceeds a predefined threshold of 5 meters, the detection is flagged as an outlier and is discarded.
- This simple rule-based filter works “really well” to remove the noise and produce a clean, smooth ball trajectory ready for visualization.
This step highlights a crucial aspect of applied data science: the raw output of even the most complex models often requires a “last mile” of simple, domain-aware data cleaning to become truly useful and reliable.
Conclusion: A Final Thought
Building a sophisticated sports analytics AI is a masterclass in integration. It’s not about finding one single, all-powerful model. Instead, it’s about creatively combining multiple, specialized AI techniques—like object detection, keypoint detection, and embeddings—with classical computer vision principles like homography and a healthy dose of practical, clever heuristics to handle the inevitable edge cases. Each component solves one piece of the puzzle, and their synergy creates the impressive final result.
The synthesis of these techniques is a powerful template for any problem involving tracking objects in a dynamic, real-world space. It makes you wonder: what other domains, from logistics warehouse tracking to autonomous vehicle perception, could benefit from this blend of landmark detection, semantic clustering, and classical geometry?

