baseballcv 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
baseballcv/__init__.py ADDED
File without changes
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Dylan Drummey and Carlos Marcano
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,443 @@
1
+ Metadata-Version: 2.2
2
+ Name: baseballcv
3
+ Version: 0.1.0
4
+ Summary: A collection of tools and models designed to aid in the use of Computer Vision in baseball.
5
+ Author-email: Dylan Drummey <dylandrummey22@gmail.com>, Carlos Marcano <c.marcano@balldatalab.com>
6
+ Project-URL: Homepage, https://github.com/dylandru/BaseballCV
7
+ Project-URL: Bug Tracker, https://github.com/dylandru/BaseballCV/issues
8
+ Project-URL: Documentation, https://baseballcv.com
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.10
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: bs4==0.0.2
15
+ Requires-Dist: cryptography>=42.0.3
16
+ Requires-Dist: opencv-python==4.10.0.84
17
+ Requires-Dist: pybaseball==2.2.7
18
+ Requires-Dist: pytest==8.3.2
19
+ Requires-Dist: ultralytics!=8.3.41,!=8.3.42,>=8.2.90
20
+ Requires-Dist: transformers>=4.48.0
21
+ Requires-Dist: peft==0.13.2
22
+ Requires-Dist: streamlit>=1.37.0
23
+ Requires-Dist: streamlit-image-coordinates==0.1.6
24
+ Requires-Dist: streamlit-drawable-canvas==0.9.3
25
+ Requires-Dist: awscli==1.36.5
26
+ Requires-Dist: pytest-cov==6.0.0
27
+ Requires-Dist: supervision==0.25.0
28
+ Requires-Dist: pillow==10.3.0
29
+ Requires-Dist: tensorboard>=2.15.0
30
+ Requires-Dist: wandb==0.19.1
31
+ Requires-Dist: coco-eval==0.0.4
32
+ Requires-Dist: pytorch-lightning==2.5.0
33
+
34
+ # BaseballCV
35
+
36
+ [![BaseballCV Scripts](https://github.com/dylandru/BaseballCV/actions/workflows/pytest.yml/badge.svg)](https://github.com/dylandru/BaseballCV/actions/workflows/pytest.yml)
37
+ [![Package Build](https://github.com/dylandru/BaseballCV/actions/workflows/build.yml/badge.svg)](https://github.com/dylandru/BaseballCV/actions/workflows/build.yml)
38
+ [![codecov](https://codecov.io/github/dylandru/BaseballCV/graph/badge.svg?token=86UGAREPSB)](https://codecov.io/github/dylandru/BaseballCV)
39
+
40
+ **Created By: Dylan Drummey ([@drummeydylan](https://x.com/DrummeyDylan)) and Carlos Marcano ([@camarcano](https://x.com/camarcano))
41
+ Maintained By: Carlos Marcano**
42
+
43
+ ![BaseballCV Logo](assets/baseballcvlogo.png)
44
+
45
+ **A collection of tools and models designed to aid in the use of Computer Vision in baseball.**
46
+
47
+ ## Goals
48
+
49
+ Our goal is to provide access to high-quality datasets and computer vision models for baseball. By sharing our annotated data and model weights, we hope to foster innovation in sports analytics and make it easier for developers to create tools that analyze baseball games.
50
+
51
+ ## Installation and Setup
52
+
53
+ To get started with our datasets and models, follow these steps:
54
+
55
+ ### Install Package
56
+
57
+ The package can be installed from Github with the required dependencies using the following command:
58
+
59
+ ```bash
60
+ pip install git+https://github.com/dylandru/BaseballCV.git
61
+ ```
62
+
63
+ ### Clone the Repository Locally
64
+
65
+ The repository can be cloned from Github with the required dependencies installed using the following commands:
66
+
67
+ ```bash
68
+ git clone https://github.com/dylandru/BaseballCV.git
69
+ cd BaseballCV
70
+ pip install -r requirements.txt
71
+ ```
72
+ ### Important Reminders
73
+
74
+ - Imports vary from using package to using local files; every import from the package is prefixed with `baseballcv.`
75
+ - The package is currently in development and may not be fully functional.
76
+ - The package is not yet available on PyPI, so it must be installed from Github.
77
+
78
+ ## Available Assets
79
+
80
+ ### 1. Datasets
81
+ We provide open-source datasets containing images and corresponding annotations from baseball broadcasts and other video sources. These datasets are curated and annotated with bounding boxes and labels for various baseball objects. These datasets are designed to detect:
82
+
83
+ - Pitchers
84
+ - Hitters
85
+ - Catchers
86
+ - Baseball
87
+ - Pitcher's Glove
88
+ - Bat
89
+ - Other objects of interest
90
+
91
+ Datasets are available in common formats like YOLO, allowing for easy integration with computer vision pipelines. Our current datasets include:
92
+
93
+ **Available YOLO Pre-Annotated Datasets**:
94
+
95
+ - `baseball_rubber_home_glove.txt`: A comprehensive MLB broadcast-based YOLO-format annotated dataset with annotations for baseballs, the rubber, homeplate, and the catcher's mitt.
96
+ - `baseball_rubber_home.txt`: An MLB broadcast-based YOLO-format annotated dataset with annotations for baseballs, the rubber, and the catcher's mitt.
97
+ - `baseball.txt`: An MLB broadcast-based YOLO-format annotated dataset with annotations for solely baseballs.
98
+
99
+ **Available Raw Photos Datasets**:
100
+
101
+ - `broadcast_10k_frames.txt`: A collection of 10,000 unannotated MLB broadcast images that can be used for creating custom datasets or annotations.
102
+ - `broadcast_15k_frames.txt`: A collection of 15,000 unannotated MLB broadcast images that can be used for creating custom datasets or annotations.
103
+
104
+ **Available COCO-format Datasets**:
105
+ - `baseball_rubber_home_COCO.txt`: A comprehensive MLB broadcast-based COCO-format annotated dataset with annotations for baseballs, the rubber, and homeplate.
106
+ - `baseball_rubber_home_glove_COCO.txt`: A comprehensive MLB broadcast-based COCO-format annotated dataset with annotations for baseballs, the rubber, homeplate, and the catcher's mitt.
107
+
108
+ **Downloading Datasets**
109
+
110
+ If you are interested in training your own models with our datasets, you can download the one of the pre-annotated datasets. To download the datasets into a folder, you can use the following:
111
+
112
+ ```python
113
+ from baseballcv.functions import LoadTools
114
+
115
+ # Initialize LoadTools class
116
+ load_tools = LoadTools()
117
+
118
+ # Download images, .txt file annotations and .yaml file into folder using .txt file path
119
+ dataset_path = load_tools.load_dataset("datasets/yolo/baseball_rubber_home_glove.txt")
120
+
121
+ # Download images, .txt file annotations and .yaml file into folder using alias
122
+ dataset_path = load_tools.load_dataset("baseball_rubber_home_glove")
123
+
124
+ ```
125
+ If you are interested in annotating your own dataset, you can use one of the raw photos datasets. To download the raw photos datasets into a folder prefaced with `unlabeled_`, you can use the following:
126
+
127
+ ```python
128
+ from baseballcv.functions import LoadTools
129
+
130
+ # Initialize LoadTools class
131
+ load_tools = LoadTools()
132
+
133
+ # Download images into unlabeled_ folder using .txt file path
134
+ dataset_path = load_tools.load_dataset("datasets/raw_photos/broadcast_10k_frames.txt")
135
+
136
+ # Download images into unlabeled_ folder using alias
137
+ dataset_path = load_tools.load_dataset("broadcast_10k_frames")
138
+ ```
139
+
140
+ **Creating Your Own Datasets**
141
+
142
+ If you are interested in creating your own datasets from scratch, you can use our tools to help you, such as our photo generator.
143
+
144
+ ```python
145
+ from baseballcv.functions import DataTools
146
+
147
+ # Initialize DataTools class
148
+ data_tools = DataTools()
149
+
150
+ # Generate 10,000 photos into photos folder from across Savant videos between May 1, 2024 and July 31, 2024
151
+ data_tools.generate_photo_dataset(max_plays=5000, output_frames_folder="photos", max_num_frames=10000, max_videos_per_game=10, start_date="2024-05-01", end_date="2024-07-31", delete_savant_videos=True)
152
+ ```
153
+
154
+ With that given dataset, you can then use our automatic annotation tool to annotate the dataset based on an existing model.
155
+
156
+ ```python
157
+ from baseballcv.functions import DataTools
158
+
159
+ # Initialize DataTools class
160
+ data_tools = DataTools()
161
+
162
+ # Annotate photos folder based on glove tracking model with a confidence threshold of 60%
163
+ data_tools.automated_annotation(model_alias="glove_tracking", dataset_path="photos", conf=0.6)
164
+ ```
165
+
166
+ More datasets and tools will likely be added in the future, so check back!
167
+
168
+ ### 2. Pre-trained Models
169
+ We offer pre-trained YOLO models for object detection. The models are trained to detect the aforementioned objects with high accuracy.
170
+
171
+ **Available YOLO Models**:
172
+
173
+ - `bat_tracking.pt`: Trained to detect bat movement from a broadcast feed
174
+ - `ball_tracking.pt`: Trained to detect the baseball from the pitcher's hand to home from a broadcast feed.
175
+ - `pitcher_hitter_catcher.pt`: Trained to detect the pitcher, hitter and catcher from a broadcast feed.
176
+ - `glove_tracking.pt`: Trained to detect and track the catcher's glove, the ball, homeplate, and the pitcher's rubber from a broadcast feed.
177
+ - `ball_trackingv4`: Trained to track ONLY the baseball from pitcher's hand to home plate.
178
+
179
+ **Available Florence2 Models**:
180
+
181
+ - `florence_ball_tracking`: Trained to track the baseball from pitcher's hand to home plate among other VLM tasks.
182
+
183
+ **Available PaliGemma2 Models**:
184
+
185
+ - `paligemma2_ball_tracking`: Trained to track the baseball from pitcher's hand to home plate among other VLM tasks.
186
+
187
+ **Available DETR Models**:
188
+
189
+ - Coming Soon...
190
+
191
+ ## Downloading and Loading Models
192
+
193
+ ### Overview
194
+ The `LoadTools` class provides a convenient way to download and load different types of models, including YOLO and Florence2 models. This class handles the downloading of model weights and datasets from either the BallDataLab API or specified text files.
195
+
196
+ ### Loading Models
197
+
198
+ #### YOLO Models
199
+ To load a YOLO model, you can use the `load_model` method of the `LoadTools` class. Below is an example of how to load a YOLO model:
200
+
201
+ ```python
202
+ from baseballcv.functions import LoadTools
203
+
204
+ # Initialize LoadTools class
205
+ load_tools = LoadTools()
206
+
207
+ # Load YOLO model using alias
208
+ model_path = load_tools.load_model("bat_trackingv4")
209
+
210
+ # Load YOLO model using .txt file path
211
+ model_path = load_tools.load_model("models/YOLO/bat_tracking/model_weights/bat_tracking.txt")
212
+ ```
213
+
214
+ #### Florence 2 Models
215
+
216
+ To load a Florence 2 model, you can also use the load_model method of the LoadTools class. Below is an example of how to load a Florence 2 model:
217
+
218
+
219
+ ```python
220
+ from baseballcv.functions import LoadTools
221
+
222
+ # Initialize LoadTools class
223
+ load_tools = LoadTools()
224
+
225
+ # Load Florence2 model using alias
226
+ model_path = load_tools.load_model("florence_ball_tracking", model_type='FLORENCE2')
227
+
228
+ # Load Florence2 model using .txt file path
229
+ model_path = load_tools.load_model("models/vlm/FLORENCE2/ball_tracking/model_weights/florence_ball_tracking.txt", model_type='FLORENCE2')
230
+ ```
231
+
232
+ #### PaliGemma 2 Models
233
+
234
+ To load a PaliGemma 2 model, you can also use the load_model method of the LoadTools class. Below is an example of how to load a PaliGemma 2 model:
235
+
236
+
237
+ ```python
238
+ from baseballcv.functions import LoadTools
239
+
240
+ # Initialize LoadTools class
241
+ load_tools = LoadTools()
242
+
243
+ # Load Florence2 model using alias
244
+ model_path = load_tools.load_model("paligemma2_ball_tracking", model_type='PALIGEMMA2')
245
+
246
+ # Load Florence2 model using .txt file path
247
+ model_path = load_tools.load_model("models/vlm/paligemma2/ball_tracking/model_weights/paligemma2_ball_tracking.txt", model_type='PALIGEMMA2')
248
+ ```
249
+
250
+ ## Examples
251
+
252
+ Below are some examples showcasing our models in action. These include both image and video examples where the models detect various objects within a baseball broadcast.
253
+
254
+ ### Image Example
255
+
256
+ ![Example Detection](assets/phc_example_prediction.jpg)
257
+
258
+ The above image demonstrates our YOLO model detecting a pitcher, hitter, and catcher during a game broadcast.
259
+
260
+ ### Video Examples
261
+
262
+ https://github.com/user-attachments/assets/7f56df7e-2bdb-4057-a1d7-d4d50d81708e
263
+
264
+ https://github.com/user-attachments/assets/fa104a6d-ac26-460c-b096-7f20e2821c20
265
+
266
+ https://github.com/user-attachments/assets/962973c8-514b-4f39-ac02-ca9f82bf2b59
267
+
268
+ These videos showcase our models' ability to track multiple objects, including the ball, glove, and other elements in real-time.
269
+
270
+ ## Example Code
271
+
272
+ ### YOLO Models
273
+
274
+ #### Inference Example
275
+
276
+ Here's an example of how to use our pre-trained YOLO models to run inference on an image or video.
277
+
278
+ ```python
279
+ from ultralytics import YOLO
280
+
281
+ # Assuming model is already downloaded from .txt file
282
+ model = YOLO("models/ball_tracking/model_weights/ball_tracking.pt")
283
+
284
+ # Run inference on image
285
+ model.predict("example_baseball_broadcast.jpg", show=True)
286
+
287
+ # Run inference on video
288
+ model.predict("assets/example_broadcast_video.mp4", show=True)
289
+ ```
290
+
291
+ #### Extraction Example
292
+
293
+ ```python
294
+ from ultralytics import YOLO
295
+
296
+ model = YOLO("models/ball_tracking/model_weights/ball_tracking.pt")
297
+
298
+ # assign inference on video to results
299
+ results = model.predict("assets/example_broadcast_video.mp4", show=True)
300
+
301
+
302
+ for r in results: #loop through each frame
303
+ for box in r.boxes.cpu().numpy(): #loop through each box in each frame
304
+ print(f"XYXY: {box.xyxy}") #print xyxy coordinates of the box
305
+ print(f"XYWHN (Normalized XYWH): {box.xywh}") #print xywh coordinates of the box
306
+ print(f"XYXYN (Normalized XYXY): {box.xyxyn}") #print normalized xyxy coordinates of the box
307
+ print(f"Confidence: {box.conf}") #print confidence of the box
308
+ print(f"Track ID: {box.id}") #print track id of the box (may not exist)
309
+ print(f"Class Value: {box.cls}") #print class value of the box
310
+ ```
311
+
312
+ ### Florence 2 Models
313
+
314
+ #### Inference Example
315
+
316
+ Here's an example of how to use our pre-trained Florence 2 models to run inference on an image. The Florence 2 model supports multiple vision tasks including object detection, image captioning, and open vocabulary detection.
317
+
318
+ ```python
319
+ from baseballcv.models import Florence2
320
+
321
+ # Initialize our Florence 2 model - automatically selects the best available device (CUDA, MPS, or CPU)
322
+ model = Florence2()
323
+
324
+ # Run object detection
325
+ detection_results = model.inference(
326
+ image_path='baseball_game.jpg',
327
+ task='<OD>'
328
+ )
329
+
330
+ # Run detailed captioning
331
+ caption_results = model.inference(
332
+ image_path='baseball_game.jpg',
333
+ task='<DETAILED_CAPTION>'
334
+ )
335
+
336
+ # Run open vocabulary detection
337
+ specific_objects = model.inference(
338
+ image_path='baseball_game.jpg',
339
+ task='<OPEN_VOCABULARY_DETECTION>',
340
+ text_input='Find the baseball, pitcher, and catcher'
341
+ )
342
+ ```
343
+
344
+ #### Extraction Example
345
+
346
+ Here's an example of how to use our pre-trained Florence 2 models to extract predictions from an image. This example shows how to fine-tune the model on your own dataset and extract custom predictions.
347
+
348
+ ```python
349
+ from baseballcv.models import Florence2
350
+
351
+ # Initialize the model for training
352
+ model = Florence2()
353
+
354
+ # Run inference with model
355
+ results = model.inference(
356
+ image_path='new_baseball_image.jpg',
357
+ task='<OD>' # Use object detection to find the players
358
+ )
359
+
360
+ print(results) #Prints coordinates of BBoxes in JSON as such: {'OD': {'bboxes': [[x1, y1, x2, y2], [x1, y1, x2, y2], ...], 'labels': ['label1', 'label2', ...]}}
361
+
362
+ ```
363
+
364
+ ## Notebooks
365
+
366
+ Along with our datasets and models, we have provided a few notebooks to help you get started with our repo. These are designed to help you understand the application of our models to real-world baseball videos, which are all accessible in the `notebooks` folder.
367
+
368
+ - `ball_inference_YOLOv9.ipynb`: A notebook to get you started with our ball tracking model.
369
+ - `Glove_tracking.ipynb`: A notebook to get you started with our glove tracking model and extracting predictions.
370
+ - `YOLO_PHC_detection_extraction.ipynb`: A notebook to get you started with our pitcher, hitter, catcher detection model along with utilizing these predictions.
371
+ - `okd_nokd_classifier.ipynb`: A notebook to get you started with our OKD_NOKD dataset.
372
+ - `glove_framing_tracking.ipynb`: A notebook to get you started with our glove tracking model which also transposes the predictions to a 2D plane, while also extracting the predictions.
373
+ - `autolabeling_semisupervised.ipynb`: A notebook that shows how to create a base dataset of raw images, auto annotate it and then view the results for manual filtering.
374
+
375
+ ## Streamlit Applications
376
+
377
+ BaseballCV provides Streamlit-based web apps for interacting with our computer vision models and tools:
378
+
379
+ ### Annotation App
380
+
381
+ Located in: `/streamlit/annotation_app/`
382
+
383
+ A web interface for managing and annotating baseball images/videos:
384
+ - Open-source project designed to crowdsource baseball annotations
385
+ - Upload baseball videos or photos to annotate
386
+ - Annotation interface for labeling objects and keypoints
387
+ - Integration with AWS S3 for data / photo storage
388
+ - Built-in BaseballCV models for predicted annotations
389
+
390
+ Quick start:
391
+ ```bash
392
+ cd BaseballCV/streamlit/annotation_app/
393
+ docker-compose up -d
394
+ ```
395
+
396
+ Visit the folder's README for setup and usage instructions.
397
+
398
+ ### Future Streamlit Apps Available Soon
399
+
400
+ We are currently looking to expand our Streamlit app library, and are open to suggestions and contributions (please consult our contributing guidelines beforehand below).
401
+
402
+ ## Contributing
403
+
404
+ We welcome contributions from the community! Whether you're looking to improve our datasets, train better models, or build new tools on top of our work, feel free to open a pull request or start a discussion.
405
+
406
+ ### How to Contribute (for repository maintainers)
407
+
408
+ If you are interested in helping maintain or adding to the repository, please follow these steps:
409
+
410
+ - Fork the repository
411
+ - Create a new branch (git checkout -b feature/YourFeature)
412
+ - Commit your changes (git commit -m 'Add YourFeature')
413
+ - Push to the branch (git push origin feature/YourFeature)
414
+ - Open a pull request
415
+
416
+ ### How to Contribute (for supporters)
417
+
418
+ Our main need is for data annotation. With our new annotation app, you can help us annotate certain datasets. To do so, please follow these steps:
419
+
420
+ - Go to the App: [Annotation App](https://balldatalab.com/streamlit/baseballcv_annotation_app/)
421
+ - Enter a Username and Email
422
+ - Open Existing Project
423
+ - Select a Dataset (as of right now, we need with with `player_detection`))
424
+ - Open the Annotation Interface and begin annotating!
425
+
426
+ ## Component Status
427
+
428
+ [![Dataset Tools](https://img.shields.io/badge/Dataset%20Tools-passing-success)](https://github.com/dylandru/baseballcv/actions/workflows/pytest.yml)
429
+ [![Load Tools](https://img.shields.io/badge/Load%20Tools-passing-success)](https://github.com/dylandru/baseballcv/actions/workflows/pytest.yml)
430
+ [![Savant Scraper](https://img.shields.io/badge/Savant%20Scraper-passing-success)](https://github.com/dylandru/baseballcv/actions/workflows/pytest.yml)
431
+ [![BDL API](https://img.shields.io/badge/BDL%20API-passing-success)](https://github.com/dylandru/baseballcv/actions/workflows/pytest.yml)
432
+
433
+ ## License
434
+
435
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
436
+
437
+ ## Contact
438
+
439
+ If you have any questions or would like to discuss the project, please reach out to us at dylandrummey22@gmail.com or c.marcano@balldatalab.com. You can also follow us on Twitter/X to stay updated:
440
+
441
+ - Dylan Drummey: [@drummeydylan](https://x.com/DrummeyDylan)
442
+ - Carlos Marcano: [@camarcano](https://x.com/camarcano)
443
+
@@ -0,0 +1,6 @@
1
+ baseballcv/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ baseballcv-0.1.0.dist-info/LICENSE,sha256=UiqXFA5wEWEzcdm38uqosXDN7QUnVBafZIgym6CRMWo,1089
3
+ baseballcv-0.1.0.dist-info/METADATA,sha256=N478NS-LJPUcgVoaVYtU5Omoiq64AeYf5WIU1-vhZwU,18188
4
+ baseballcv-0.1.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
+ baseballcv-0.1.0.dist-info/top_level.txt,sha256=4rGGM2xEyYoTluiDODWvI0JukV8H6qc9hZikCWyxqms,11
6
+ baseballcv-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.8.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ baseballcv