Skip to content

Commit

Permalink
Docs/kaggle notebook soccertrack (#135)
Browse files Browse the repository at this point in the history
* add new notebook

* style fixes by ruff

* Add Kaggle docs screenshot

---------

Co-authored-by: AtomScott <[email protected]>
  • Loading branch information
AtomScott and AtomScott authored Dec 10, 2023
1 parent deae754 commit 17ba69c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions notebooks/01_get_started/kaggle_notebook_soccertrack.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cells":[{"cell_type":"markdown","metadata":{},"source":["This is a backup for a notebook that I made for SoccerTrack on Kaggle.\n","\n","[![](https://img.shields.io/badge/Kaggle-20BEFF?style=for-the-badge&logo=Kaggle&logoColor=white)](https://www.kaggle.com/code/atomscott/soccertrack-dataset-demo)"]},{"cell_type":"markdown","metadata":{"execution":{"iopub.execute_input":"2023-12-09T14:25:52.604608Z","iopub.status.busy":"2023-12-09T14:25:52.604169Z","iopub.status.idle":"2023-12-09T14:25:52.614676Z","shell.execute_reply":"2023-12-09T14:25:52.613313Z","shell.execute_reply.started":"2023-12-09T14:25:52.604577Z"}},"source":["# SoccerTrack Dataset Demo with SportsLabKit\n","\n","[SportsLabKit](https://github.com/AtomScott/SportsLabKit) is a Python library designed to simplify the process of tracking multiple objects in sports videos. It provides a modular and flexible architecture that allows you to customize your tracking pipeline according to your needs. In this notebook, we'll demonstrate how to use SportsLabKit to track players in a soccer match using the SoccerTrack dataset.\n","\n","<a href='https://github.com/AtomScott/SportsLabKit'>\n","<img src=\"https://repository-images.githubusercontent.com/433316752/1a54dc7b-1cc5-4d7b-911c-a9ac266cf6c7\" alt=\"Social Graph\" style=\"width: 400px; object-fit: cover; border-radius: 10px;\"></a>\n","\n","## Installation\n","\n","Before we start, we need to install SportsLabKit and [TorchReID](https://github.com/KaiyangZhou/deep-person-reid) for image embeddings. Run the following commands to install these libraries:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["%pip install sportslabkit --quiet\n","%pip install git+https://github.com/KaiyangZhou/deep-person-reid.git --quiet"]},{"cell_type":"markdown","metadata":{},"source":["Now we should be able to import SportsLabKit! "]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["import sportslabkit as slk"]},{"cell_type":"markdown","metadata":{},"source":["## Loading the Dataset\n","\n","We'll use the SoccerTrack dataset on Kaggle. Let's load it."]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["from pathlib import Path\n","\n","dataset_root = Path('/kaggle/input/soccertrack')\n","print(list(dataset_root.iterdir()))"]},{"cell_type":"markdown","metadata":{},"source":["For this demo, we'll use the Wide View video dataset:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["path_to_csv = dataset_root / 'wide_view/annotations/F_20200220_1_0000_0030.csv'\n","path_to_mp4 = dataset_root / 'kaggle/input/soccertrack/wide_view/videos/F_20200220_1_0000_0030.mp4'\n","path_to_kps = dataset_root / 'kaggle/input/soccertrack/fisheye_keypoints.json'"]},{"cell_type":"markdown","metadata":{},"source":["## Loading the Annotation Data\n","\n","Let's load the annotation data:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["bbdf = slk.load_df(path_to_csv)\n","bbdf.head()"]},{"cell_type":"markdown","metadata":{},"source":["## Loading the Video Data\n","\n","Next, we load the video data:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["cam = slk.Camera(path_to_mp4) # Camera object will be used to load frames\n","frame = cam[0] # When indexed like this cam will return a numpy array\n","slk.utils.cv2pil(frame, convert_bgr2rgb=False) # Utility function in SportsLabKit to show images"]},{"cell_type":"markdown","metadata":{"execution":{"iopub.execute_input":"2023-12-09T14:33:09.892008Z","iopub.status.busy":"2023-12-09T14:33:09.891548Z","iopub.status.idle":"2023-12-09T14:33:09.897592Z","shell.execute_reply":"2023-12-09T14:33:09.896382Z","shell.execute_reply.started":"2023-12-09T14:33:09.891976Z"}},"source":["\n","## Visualizing Frame Data\n","\n","Let's visualize the frame data:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["frame_idx = 1 # Note the bbdf starts index with 1 not 0\n","vis_frame = bbdf.visualize_frame(frame_idx=frame_idx, frame=frame)\n","slk.utils.cv2pil(frame, convert_bgr2rgb=False)"]},{"cell_type":"markdown","metadata":{"execution":{"iopub.execute_input":"2023-12-09T14:36:41.788764Z","iopub.status.busy":"2023-12-09T14:36:41.787875Z","iopub.status.idle":"2023-12-09T14:36:41.794287Z","shell.execute_reply":"2023-12-09T14:36:41.793023Z","shell.execute_reply.started":"2023-12-09T14:36:41.788724Z"}},"source":["## Using Pitch Coordinate Data\n","\n","Next, we'll use the pitch coordinate data:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["cam.source_keypoints, cam.target_keypoints = slk.utils.load_keypoints(path_to_kps)\n","codf = bbdf.to_codf(cam.H, method='bottom_middle')\n","codf.head()"]},{"cell_type":"markdown","metadata":{},"source":["## Visualizing Frame Data\n","\n","Let's visualize the frame data in pitch coordinates. We don't have to use pass the frame as we will be plotting on to a pitch."]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["codf.visualize_frame(frame_idx=frame_idx, ball_key='BALL')"]},{"cell_type":"markdown","metadata":{},"source":["## Tracking\n","\n","For tracking, it's recommended to turn on GPU use:"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["from sportslabkit.mot import SORTTracker\n","\n","det_model = slk.detection_model.load('YOLOv8x', imgsz=640)\n","motion_model = slk.motion_model.load('KalmanFilter', dt=1/30, process_noise=10000, measurement_noise=10)\n","\n","tracker = SORTTracker(detection_model=det_model, motion_model=motion_model)\n","res = tracker.track(cam)"]},{"cell_type":"markdown","metadata":{},"source":["Thanks for checking our this notebook!\n","\n","I have more guides about how to use the sportslabkit on perform tracking in the [official docs](https://sportslabkit.readthedocs.io/en/latest/)!\n","\n","![Screenshot 2023-12-09 at 23.35.41.png](./assets/kaggle_docs_screenshot.png)"]},{"cell_type":"markdown","metadata":{},"source":["Happy hacking!"]}],"metadata":{"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"datasetId":2481051,"sourceId":4977082,"sourceType":"datasetVersion"}],"dockerImageVersionId":30587,"isGpuEnabled":true,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"}},"nbformat":4,"nbformat_minor":4}
3 changes: 2 additions & 1 deletion sportslabkit/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from collections.abc import Iterable, Mapping
from typing import Any

import __main__ as main
from loguru import logger

import __main__ as main


class LoggerMixin:
def __init__(self) -> None:
Expand Down

0 comments on commit 17ba69c

Please sign in to comment.