Skip to content

Visualizing groupings within a photographed scene

Clustering data isn't limited to rows and columns; it can be extending into images too. As a demonstration, I'll delve into an illustration utilizing an image from the recent 2022 World Cup football tournament in Qatar, specifically grabbed during the Brazil versus Serbia match. Following is a...

Identifying distinct regions within an image
Identifying distinct regions within an image

Visualizing groupings within a photographed scene

Automatically Identifying Clusters in Sports Images Using YOLOv5 and DBSCAN

In the world of data science, a no-code platform called Experienced Data Science is making waves, offering an accessible way to learn the intricacies of the field. One of its creators, who also maintains a YouTube channel at Data Science Demonstrated, has recently shared a fascinating blog post demonstrating the use of clustering in sports analytics.

The post showcases a hybrid approach that combines YOLOv5, a state-of-the-art deep learning object detector, with the density-based clustering (DBSCAN) algorithm to automatically identify clusters within an image. This method was applied to a captivating image from the Brazil vs Serbia match during the World Cup Football 2022 in Qatar, just before a stunning goal from Richarlison.

The process begins with YOLOv5 detecting objects in the image, producing bounding boxes and class labels for each detected object. The spatial features of these detected objects, such as their centroid coordinates (x, y), are then extracted. Next, DBSCAN is employed to group the detected objects based on their spatial proximity and density.

Key DBSCAN parameters, eps (maximum distance between two samples to be considered neighbours) and min_samples (minimum number of points to form a dense region), are adjusted according to the scale and distribution of YOLO detections. Advanced methods like Density-aware Neighborhood Propagation (DNP) can further enhance cluster detection by propagating cluster labels from the highest-density points through descending density gradients.

This hybrid approach offers several advantages. First, YOLOv5 provides fast and accurate object detection, highlighting detected instances and their spatial coordinates. Second, DBSCAN uses distance and density criteria to identify clusters of points, avoiding predefining the number of clusters and identifying clusters of any shape, as well as noise.

To follow this practical approach, you can use the following Python outline:

```python

results = model(image) # 'model' is a pre-loaded YOLOv5 detections = results.xyxy[0].cpu().numpy() # bounding boxes in [x1, y1, x2, y2, confidence, class]

import numpy as np centroids = [] for det in detections: x1, y1, x2, y2 = det[:4] cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 centroids.append([cx, cy]) centroids = np.array(centroids)

from sklearn.cluster import DBSCAN

dbscan = DBSCAN(eps=50, min_samples=3) # eps and min_samples depend on your image scale labels = dbscan.fit_predict(centroids)

```

The results of DBSCAN were plotted in a scatter plot, with green dots representing identified dense areas. The image contained 17 objects (persons) as detected by YOLOv5, and the X and Y locations of each object in the image were identified.

This method can have various use cases, such as sports analytics, identifying over-crowding in public spaces, or identifying areas where shoppers spend more time in a shopping complex. To support the author and receive notifications about new stories, consider joining Medium with the provided referral link.

Join Medium

References:

  1. Redmon, J., & Farhadi, A. (2016). Yolo9000: Better, faster, stronger. ArXiv preprint arXiv:1612.08242.
  2. Ester, M., Kriegel, H. P., Sander, J., & Xu, X. (1996). A density-based clustering algorithm for discovering clusters of different densities. In Proceedings of the 1996 ACM SIGMOD international conference on Management of data (pp. 251-260). ACM.
  3. Xu, X., & Le, Q. V. (2005). Density-based clustering for images with noise and overlapping regions. IEEE Transactions on Pattern Analysis and Machine Intelligence, 27(10), 1411-1424.
  4. Bochkovskiy, A., Wang, S., & Wang, Y. (2020). Yolov5: Trainable single-shot detector with a neural architecture search. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (pp. 10398-10400). IEEE.
  5. Wang, W., & Reid, I. (2000). A k-nearest neighbors algorithm for object detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 22(10), 1288-1304.

The blog post demonstrates the use of technology in sports analytics, where a hybrid approach combines YOLOv5, a cutting-edge object detector, with DBSCAN for automatically identifying clusters in images, such as the sports scene from the Brazil vs Serbia match during the World Cup Football 2022.

This technique can further be applied to various fields, including sports, identifying over-crowding in public spaces, or detecting areas where shoppers spend more time in a shopping complex.

Read also:

    Latest