Game state detection for Clash Royale using computer vision, designed for reinforcement learning applications.
- Unit Detection: Detects 150+ unit types on the battlefield using YOLOv8
- Tower Health: OCR-based tower health detection
- Game Time: Timer detection via OCR
- Object Tracking: ByteTrack for consistent unit tracking across frames
- Bluestacks Integration: Automated screenshot capture on macOS
- Create and activate virtual environment:
cd /Users/will/Documents/Projects/CReinforcement-Learning
python3 -m venv venv
source venv/bin/activate- Install dependencies:
pip install -r requirements.txtfrom cr_detection.state import GameStateDetector
# Initialize detector
detector = GameStateDetector()
# Capture from Bluestacks and detect
state = detector.capture_and_detect()
print(f"Game time: {state.time}s")
print(f"Friendly units: {len(state.get_friendly_units())}")
print(f"Enemy units: {len(state.get_enemy_units())}")
# Or detect from an image file
state = detector.detect_from_file("screenshot.png")# Capture from Bluestacks
python scripts/demo_detection.py
# Detect from image file
python scripts/demo_detection.py --image path/to/screenshot.png
# Continuous capture mode
python scripts/demo_detection.py --continuous
# Save visualization
python scripts/demo_detection.py --save output.pngCReinforcement-Learning/
├── src/cr_detection/
│ ├── capture/ # Bluestacks screenshot capture
│ ├── models/ # YOLOv8 detector and tracking
│ ├── ocr/ # PaddleOCR for text recognition
│ ├── processing/ # Image splitting and constants
│ ├── state/ # State building and high-level API
│ └── utils/ # Utility functions
├── models/ # Model weight files
├── scripts/ # Demo and utility scripts
└── tests/ # Test files
Currently supported aspect ratios:
- 2.22 (1080x2400) - Primary
- 2.16 (592x1280)
- 2.13 (600x1280)
- 1.78 (1080x1920) - Bluestacks 16:9 (placeholder params, may need calibration)
If detection isn't working correctly, you may need to calibrate the split parameters for your Bluestacks resolution:
- Take a screenshot during gameplay
- Use
calibrate_bbox_params()to visualize regions - Adjust parameters in
src/cr_detection/processing/constants.py
Detection models and core algorithms ported from KataCR.