samson82-aiml/cricket-batting-coach
0
๐ Cricket Batting Coach
A real-time cricket batting coach that uses computer vision to detect and analyze batting swings. Built with Streamlit and optimized for Hugging Face Spaces.
๐ Live Demo

๐ฏ Features
- Real-time Pose Detection: Uses MediaPipe for accurate pose tracking
- Swing Analysis: Detects forward batting swings with speed and direction analysis
- Live Video Processing: WebRTC-based video streaming for low-latency performance
- Swing Counter: Tracks and displays swing count in real-time
- Visual Feedback: Real-time overlay with swing detection indicators
๐ ๏ธ Technology Stack
- Frontend: Streamlit
- Video Processing: OpenCV, MediaPipe
- Real-time Communication: WebRTC
- Deployment: Hugging Face Spaces
๐ Requirements
- Python 3.11 (for MediaPipe compatibility)
- Webcam access
- Modern web browser with WebRTC support
๐ Quick Start
Local Development
- Clone the repository
git clone https://github.com/samjosdev/cricket-batting-coach.git
cd cricket-batting-coach- Install dependencies
pip install -r requirements.txt- Run the application
streamlit run app.py- Open your browser Navigate to
http://localhost:8501
Hugging Face Spaces Deployment
- Fork this repository
- Create a new Space on Hugging Face
- Go to Hugging Face Spaces
- Click "Create new Space"
- Choose "Streamlit" as the SDK
- Set the repository to your forked repo
- Configure the Space
- The Space will automatically detect the
app.pyfile - Dependencies will be installed from
requirements.txt - Python version will be set to 3.11
๐ฎ How to Use
- Start the Application
- Click "Start" to begin video processing
- Allow camera access when prompted
- Position Yourself
- Stand in front of the camera
- Ensure your full body is visible
- Hold a cricket bat or simulate batting motion
- Practice Your Swings
- Make forward batting swings
- The app will detect and count your swings
- Watch for the "SWING DETECTED!" indicator
- Monitor Your Progress
- Track your swing count in real-time
- Analyze swing speed and direction
- Reset counters as needed
๐ง Configuration
Python Version
The app is configured to use Python 3.11 for optimal MediaPipe compatibility:
.python-version: Specifies Python 3.11runtime.txt: Alternative Python version specification
Dependencies
Key dependencies are specified in requirements.txt:
streamlit==1.28.1
streamlit-webrtc==0.47.1
opencv-python-headless==4.8.1.78
mediapipe==0.10.7
numpy==1.24.3
av==10.0.0System Dependencies
Required system packages are specified in packages.txt:
libgl1-mesa-glx
libglib2.0-0
libsm6
libxext6
libxrender-dev
libgomp1๐ฏ Swing Detection Algorithm
The app uses a sophisticated algorithm to detect cricket batting swings:
- Pose Tracking: MediaPipe tracks 33 body landmarks
- Wrist Movement: Monitors wrist position changes over time
- Speed Calculation: Computes movement speed between frames
- Direction Analysis: Determines swing direction (forward/backward)
- Threshold Filtering: Applies minimum speed and direction thresholds
- Cooldown System: Prevents double-counting of swings
Detection Parameters
- Speed Threshold: 0.045 (minimum movement speed)
- Direction Threshold: 0.005 (minimum forward motion)
- History Length: 15 frames for analysis
- Cooldown: 80 frames between detections
๐ Troubleshooting
Common Issues
- Camera Not Working
- Ensure camera permissions are granted
- Try refreshing the page
- Check browser WebRTC support
- Swing Detection Issues
- Ensure good lighting
- Position yourself clearly in frame
- Make deliberate forward swings
- Performance Issues
- Close other browser tabs
- Use a modern browser (Chrome, Firefox, Safari)
- Ensure stable internet connection
Hugging Face Spaces Issues
- Build Failures
- Check Python version compatibility
- Verify all dependencies are available
- Review build logs for specific errors
- Runtime Errors
- Ensure MediaPipe version compatibility
- Check system dependency installation
- Verify WebRTC configuration
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- MediaPipe for pose detection
- Streamlit for the web framework
- OpenCV for computer vision
- Hugging Face for deployment platform
๐ Support
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Include your browser, OS, and error messages
Happy Batting! ๐
