Skip to content

ChessLens is an AI-powered chess recognition system that uses a smartphone camera to detect pieces, track moves, validate games, and export them automatically to Lichess.

Notifications You must be signed in to change notification settings

FloudMe77/Chess_Position_Recognition_System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ChessLens - Chess Position Recognition System

πŸ“‹ Overview

ChessLens is an intelligent chess position recognition system that combines computer vision and machine learning to automatically detect and identify chess pieces on a board using a smartphone camera. The system features comprehensive game tracking functionality that allows players to play complete chess games with move validation, position tracking, and automatic game export to Lichess. The system consists of two main components:

  • Android Application: Captures video feed, tracks game progress, validates moves, and exports games to Lichess
  • Python Server: Processes images using trained YOLO models to detect chess pieces and board positions in real-time

Real-time piece recognition demo Real-time chess piece recognition with YOLO models in action

πŸ—οΈ Project Structure

Chess/
β”œβ”€β”€ android/                          # Android application
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ src/main/java/com/example/chesslens/
β”‚   β”‚   β”‚   β”œβ”€β”€ MainActivity.kt       # Main activity with camera/board view switching
β”‚   β”‚   β”‚   β”œβ”€β”€ ui/                   # UI components (CameraApp, BoardContent, ClockApp)
β”‚   β”‚   β”‚   β”œβ”€β”€ viewmodel/            # MVVM architecture components
β”‚   β”‚   β”‚   β”œβ”€β”€ game/                 # Chess game logic (move validation, position tracking)
β”‚   β”‚   β”‚   β”œβ”€β”€ serialization/        # PGN export and Lichess integration
β”‚   β”‚   β”‚   β”œβ”€β”€ websocket/            # WebSocket client for server communication
β”‚   β”‚   β”‚   └── utils/                # Image analysis and chess utilities
β”‚   β”‚   β”‚   └── config/               # Websocket configuration
β”‚   β”‚   β”‚   └── chessMovements/       # Chess pieces movement logic
β”‚   β”‚   β”‚   └── enums/                # Usefull enums
β”‚   β”‚   └── build.gradle.kts
β”‚   └── build.gradle.kts
β”œβ”€β”€ chesslens_server/                 # Python backend server
β”‚   β”œβ”€β”€ server.py                     # FastAPI WebSocket server
β”‚   β”œβ”€β”€ src/chess_rec/
β”‚   β”‚   └── chess_position_recognizer.py  # Core recognition logic
β”‚   β”‚   └── draw_helper.py            # Functions for better visualization
β”‚   β”œβ”€β”€ models/                       # Trained YOLO models
β”‚   β”‚   β”œβ”€β”€ yolo_board_seg/          # Board segmentation model
β”‚   β”‚   └── yolo_chess_piece/        # Piece detection model
β”‚   β”œβ”€β”€ datasets/                     # Training datasets
β”‚   β”œβ”€β”€ scripts/                      # Data generation and training scripts
β”‚   β”‚   β”œβ”€β”€ dataset_maker.py          # Bootstrapping dataset generation
β”‚   β”‚   β”œβ”€β”€ dataset_shuffler.py       # Dataset shuffling utilities
β”‚   β”‚   β”œβ”€β”€ main.py                   # Main bootstrapping script
β”‚   β”‚   └── saved_position.py         # Position saving utilities
β”‚   β”œβ”€β”€ train_yolo.ipynb             # YOLO model training notebook
β”‚   └── pyproject.toml               # Python dependencies
└── README.md

πŸš€ How It Works

1. Image Capture & Processing

  • Android app captures video frames using CameraX at 5 FPS (200ms intervals)
  • Frames are sent to the Python server via WebSocket connection
  • Server processes images using two YOLO models:
    • Board Segmentation Model: Detects and segments the chessboard
    • Piece Detection Model: Identifies individual chess pieces

YOLO board segmentation demo YOLO board segmentation model detecting chessboard boundaries

YOLO piece detection demo YOLO piece detection model identifying chess pieces

2. Position Recognition

The ChessPositionRecognizer class performs the following steps:

  1. Board Detection: Uses trained YOLO11n-seg model to detect chessboard boundaries
  2. Perspective Correction: Transforms the board to a square 640x640 image
  3. Piece Detection: Identifies all chess pieces using trained YOLO11n model
  4. Position Mapping: Maps detected pieces to chess notation (e.g., "e4", "d5")

3. Game Tracking System

The Android application features a comprehensive chess game tracking system:

Move Detection & Validation

  • PositionManager: Orchestrates game logic and move validation
  • MoveValidator: Validates legal moves including castling, en passant, captures
  • GameStateManager: Tracks current player turn and game end conditions
  • Move Confirmation: Players manually accept detected moves for accuracy

Game Features

  • Real-time Position Updates: Live board state visualization
  • Move History: Complete game recording with move list
  • Game End Detection: Automatic checkmate and stalemate detection
  • Visual Feedback: Move highlighting on the board

Chess game tracking demo

*Complete chess game tracking with move validation and player interaction*

Lichess Integration

  • PGN Export: Automatic conversion of games to standard PGN format
  • Lichess Import: Direct upload to Lichess.org via API
  • Game URLs: Shareable links to played games
  • FEN Support: Starting position recording for analysis

4. Real-time Display

  • Recognized positions are sent back to the Android app
  • App displays the current board state with piece positions
  • Supports both portrait and landscape orientations
  • Visual move highlighting and game status indicators

πŸ€– YOLO Model Development

Model Architecture

The system uses two specialized YOLO models:

  1. Board Segmentation Model (YOLO11n-seg)

    • Purpose: Detect and segment chessboard boundaries
    • Architecture: YOLO11n with segmentation head
    • Input: 640x640 images
    • Output: Binary mask of chessboard area
  2. Piece Detection Model (YOLO11n)

    • Purpose: Detect and classify chess pieces
    • Architecture: YOLO11n standard detection
    • Classes: 12 classes (6 white pieces + 6 black pieces)
    • Input: 640x640 images
    • Output: Bounding boxes with piece classifications

Training Process

1. Board Segmentation Training

Dataset: Custom homemade dataset with 30+ chessboard images captured in home environment. Initially experimented with ChessReD dataset but performance was inferior for home conditions.

Training: Used YOLO11n-seg architecture with 100 epochs, 640x640 input size, batch size 16. The model was trained to detect and segment chessboard boundaries with binary mask output.

Results: Achieved exceptional performance with precision: 0.998, recall: 1.0, mAP50: 0.995, and inference speed of ~38ms on GTX 1660 SUPER. The model reliably detects chessboard boundaries even under varying lighting and perspective conditions.

2. Piece Detection Training

Initial Approach: Started with public datasets (ChessReD, GitHub chess datasets, Roboflow) but achieved only moderate performance (mAP50: 0.646). The models struggled with accurate piece classification, especially for black pieces.

Self-Training Breakthrough: Implemented a bootstrapping method where I used the initial model to detect piece locations while generating ground truth from known board positions. Created a custom dataset with ~800 images across 10 different chess positions.

Fine-tuning Process: Conducted sequential fine-tuning on custom datasets, starting with self_training_set_part1 followed by self_training_set_part2. Used lower learning rates (0.001) for fine-tuning to preserve learned features.

Final Results: Achieved exceptional performance with mAP50: 0.993 (49.7% improvement), precision: 0.983, recall: 0.984, and inference speed of ~35ms on GTX 1660 SUPER. The model now reliably detects and classifies both white and black pieces with near-perfect accuracy.

Key Training Insights

  1. Domain Adaptation: Public datasets performed poorly on home environment images
  2. Self-training: Bootstrapping method significantly improved performance
  3. Sequential Fine-tuning: Multiple rounds of fine-tuning on custom data
  4. Data Augmentation: Standard YOLO augmentations (mosaic, flip, HSV)
  5. Hardware: Training on NVIDIA GTX 1660 SUPER with CUDA

πŸ› οΈ Setup & Installation

Prerequisites

  • Python 3.11+
  • Android Studio (for Android development)
  • NVIDIA GPU with CUDA support (recommended for training)
  • Docker (optional, for containerized deployment)

Server Setup

cd chesslens_server
poetry install
# Start FastAPI server on port 8765
poetry run uvicorn server:app --host 0.0.0.0 --port 8765

Docker Setup (Recommended)

cd chesslens_server
# Build Docker image with CUDA support
docker build -t chesslens-server .

# Run container with GPU support
docker run --gpus all -p 8765:8765 

Android Setup

  1. Open android/ in Android Studio
  2. Update server IP in Config.kt file
  3. Sync Gradle dependencies
  4. Build and run on device/emulator
  5. Grant camera permissions when prompted

Configuration

  • Update WEBSOCKET_URL in android/app/src/main/java/com/example/chesslens/config/Config.kt
  • Ensure server and Android device are on the same network
  • Default server port: 8765

πŸ”§ Technical Details

Server Architecture

  • Framework: FastAPI with WebSocket support
  • Computer Vision: OpenCV for image processing
  • ML Framework: Ultralytics YOLO
  • Async Processing: Non-blocking image analysis
  • Performance: ~35ms inference time per image

Android Architecture

  • UI Framework: Jetpack Compose with Material 3
  • Camera: CameraX with image analysis (200ms intervals)
  • Architecture: MVVM with ViewModel and StateFlow
  • Game Logic: Comprehensive chess engine with move validation
  • Communication: WebSocket client with automatic reconnection
  • Networking: OkHttp3 with Lichess API integration

Data Flow

Android Camera β†’ ChessImageAnalyzer β†’ WebSocket β†’ Python Server β†’ 
YOLO Models β†’ Position Recognition β†’ WebSocket β†’ PositionManager β†’ 
Move Validation β†’ Game State β†’ PGN Export β†’ Lichess Integration

🎯 Use Cases

  • Chess Analysis: Automatic position recording and game analysis
  • Training: Position tracking for chess training apps with Lichess integration
  • Tournament: Automated game recording and digital scorekeeping
  • Education: Chess teaching tools with game replay capabilities
  • Online Play: Convert over-the-board games to digital format
  • Game Archive: Build personal game databases with automatic PGN generation

πŸ“ License

This project is licensed under the MIT License.

πŸ‘¨β€πŸ’» Author

FloudMe77 - marecik.dariusz@gmail.com


This project demonstrates advanced computer vision techniques for real-time chess position recognition, combining state-of-the-art YOLO models with custom training methodologies.

About

ChessLens is an AI-powered chess recognition system that uses a smartphone camera to detect pieces, track moves, validate games, and export them automatically to Lichess.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published