LBW Detection in Cricket: A Deep Dive with OpenCV & NumPy

Posted on Feb. 11, 2025
Data Science Projects
Docsallover - LBW Detection in Cricket: A Deep Dive with OpenCV & NumPy

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 in main.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:

  1. Prerequisites: Python 3.7+, OpenCV (opencv-python), cvzone, NumPy (install via pip).
  2. Installation:
    1. Clone the repository: git clone [repository-url] (Replace with your repo URL)
    2. Create virtual environment: python -m venv venv, activate it.
    3. Install dependencies: pip install -r requirements.txt (or install manually).
    4. Place your video in code/ folder (e.g., lbw.mp4).
  3. Tune Batsman Detection: Run python batsman.py, use trackbars, press s to save tuned values.
  4. Update main.py: Paste saved tuned values into main.py.
  5. Run LBW Detection: Execute python main.py. Use k 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 Github

Feedback 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

Batsman Detection (batsman.py)

LBW Detection - batsman.py

Pitch Detection (pitch.py)

LBW Detection - pitch.py

Main - LBW Detection (main.py)

LBW Detection - main.py

Terminal Output

LBW Detection - Terminal Output

DocsAllOver

Where knowledge is just a click away ! DocsAllOver is a one-stop-shop for all your software programming needs, from beginner tutorials to advanced documentation

Get In Touch

We'd love to hear from you! Get in touch and let's collaborate on something great

Copyright copyright © Docsallover - Your One Shop Stop For Documentation