LBW Detection in Cricket: A Deep Dive with OpenCV & NumPy
![Docsallover - LBW Detection in Cricket: A Deep Dive with OpenCV & NumPy](/docsallover/media/blog/docsallover-blog-lbw-detection-in-cricket-using-opencv-numpy.png)
Cricket! A sport brimming with drama, where fortunes can turn on a dime, often hanging on the umpire's crucial Leg Before Wicket (LBW) decision. Is it out? Is it not out? The debate rages in stands and commentary boxes alike. But what if technology could offer a more objective perspective?
This blog post introduces an exciting project: an LBW detection system powered by Python and computer vision. We're exploring how AI can "see" the game to assist with those nail-biting LBW calls.
The LBW Challenge: Why Automate?
LBW is notoriously complex. Umpires must judge trajectory and obstruction in a split second. Human error is inherent, and subjectivity can spark controversy. Technology offers the promise of greater consistency and objectivity, providing umpires with data-driven insights for more accurate decisions. Imagine enhancing fairness through AI – that's the goal.
Our Vision: Computer Vision LBW Detector
Our project envisions a computer vision system acting as an "AI Umpire." It analyzes cricket videos, frame by frame, to identify:
- The Ball: Precisely locate the cricket ball.
- The Batsman: Identify the batsman's position.
- The Pitch: Detect the pitch area.
By tracking these elements and applying basic LBW rules, the system can flag potential LBW scenarios, starting with the "Pad" condition.
How It Works
Here's a peek at the technology inside:
- Ball Detection (
ballDetect.py
): Color-based detection in HSV space, pinpointing the ball with contour finding. - Batsman Detection (
batsman.py
): RGB color filtering + edge detection (Canny), tunable for different videos. - Pitch Detection (
pitch.py
): Pitch recognition using contour detection. - LBW Logic (
ballPitchPad
inmain.py
): Simple "Pad" condition check – motion changes, ball position relative to batsman's leg (batLeg
).
Key Features: Concise & Powerful
- Real-time Processing: Efficient frame-by-frame video analysis.
- Visual Output: Contours for ball, batsman, pitch visualized; stacked images for tuning.
- Tunable Parameters: Interactive trackbars for batsman detection tuning.
- LBW Output: Clear "Potential LBW Detected!" or "No LBW Detected" console output.
- Modular Code: Well-organized Python modules for easy understanding and extension.
Try It Yourself: Quick Setup Guide
Want to run your own LBW analysis? Here's a quick guide:
- Prerequisites: Python 3.7+, OpenCV (
opencv-python
), cvzone, NumPy (install viapip
). - Installation:
- Clone the repository:
git clone [repository-url]
(Replace with your repo URL) - Create virtual environment:
python -m venv venv
, activate it. - Install dependencies:
pip install -r requirements.txt
(or install manually). - Place your video in
code/
folder (e.g.,lbw.mp4
).
- Clone the repository:
- Tune Batsman Detection: Run
python batsman.py
, use trackbars, presss
to save tuned values. - Update
main.py
: Paste saved tuned values intomain.py
. - Run LBW Detection: Execute
python main.py
. Usek
for pause/resume,q
to quit.
Download
Try it out on your cricket videos! Get the code, contribute your improvements, and help us refine this project:
Download View On GithubFeedback and suggestions are welcome – let us know what you think!
Code
Ball Detection: ball_detect.py
Batsman Detection: batsman.py
Pitch Detection: pitch.py
Main: main.py
Screenshots
Ball Detection (ball_detect.py
)
![LBW Detection - ball_detect.py](/docsallover/media/blog_internal_images/blog_150/ball-detection.png)
Batsman Detection (batsman.py
)
![LBW Detection - batsman.py](/docsallover/media/blog_internal_images/blog_150/batsman.png)
Pitch Detection (pitch.py
)
![LBW Detection - pitch.py](/docsallover/media/blog_internal_images/blog_150/pitch.png)
Main - LBW Detection (main.py
)
![LBW Detection - main.py](/docsallover/media/blog_internal_images/blog_150/main.png)
Terminal Output
![LBW Detection - Terminal Output](/docsallover/media/blog_internal_images/blog_150/terminal.png)