A Python application that uses OpenCV to monitor two cameras (one facing a golfer and another behind the golfer) and automatically creates side-by-side video clips of detected golf swings.
- Dual Camera Monitoring: Simultaneously captures video from two cameras
- Automatic Swing Detection: Uses motion detection to identify when a golf swing occurs
- Pre-Swing Buffer: Captures video from before the swing starts (configurable buffer)
- Side-by-Side Output: Combines both camera feeds into a single video with front and back views
- Automatic Clipping: Saves only the swing portion, from start to finish
- Live Preview: Optional live display of camera feeds with recording status
- Configurable Settings: Adjust sensitivity, timing, resolution, and more
- Python 3.7 or higher
- Two cameras (built-in webcam + USB camera, or two USB cameras)
- OpenCV and NumPy
- Clone the repository:
git clone https://github.com/DrTodd13/GolfSwingAnalyzer.git
cd GolfSwingAnalyzer- Install dependencies:
pip install -r requirements.txtRun the analyzer with default settings:
python golf_swing_analyzer.py- Edit the
config.pyfile to match your camera setup and preferences - Run the analyzer:
python run_analyzer.pyEdit config.py to customize:
- Camera IDs: Set which cameras to use (typically 0 and 1)
- Motion Threshold: Adjust sensitivity for swing detection
- Buffer Duration: How many seconds before the swing to include
- Cooldown Period: Time to wait between detecting swings
- Video Resolution: Camera capture resolution (can be set independently for each camera)
- FPS: Frames per second for capture and output
- Press
qto quit the application - Videos are automatically saved to the
output/directory
- Continuous Monitoring: The system continuously monitors both cameras
- Circular Buffer: Maintains a rolling buffer of recent frames (default 3 seconds)
- Motion Detection: Detects significant motion using frame differencing
- Swing Identification: When sustained motion is detected, begins recording
- Automatic Stop: Stops recording a few seconds after motion ceases
- Video Creation: Combines frames from both cameras side-by-side
- Save Output: Exports the clip with a timestamp filename
Videos are saved in the output/ directory with filenames like:
swing_20231226_143022.mp4
Each video contains:
- Frames from before the swing started (from buffer)
- The entire swing motion
- A few seconds after the swing completes
- Both camera views side-by-side
- Check camera IDs with your system (may need to try 0, 1, 2, etc.)
- Ensure cameras are connected and not in use by another application
- On Linux, you may need appropriate permissions for
/dev/video*
- Adjust
MOTION_THRESHOLDin config.py (lower = more sensitive) - Adjust
MIN_MOTION_DURATIONto require less sustained motion - Ensure adequate lighting and camera positioning
- Reduce
RESOLUTIONin config.py - Lower
FPSsetting - Close other applications using the camera
GolfSwingAnalyzer/
├── golf_swing_analyzer.py # Main application class
├── config.py # Configuration settings
├── run_analyzer.py # Example runner script
├── requirements.txt # Python dependencies
├── README.md # This file
└── output/ # Generated video clips (created automatically)
- Motion Detection: Uses frame differencing with Gaussian blur and thresholding
- Video Format: MP4 with H.264 codec (compatible with most players)
- Frame Synchronization: Ensures both cameras are synchronized frame-by-frame
- Buffer Management: Efficient circular buffer using Python deque
This project is open source. See LICENSE file for details.
Contributions are welcome! Please feel free to submit issues or pull requests.