Skip to content

Project of Embedded Software for the Internet of Thing course. A system able to play chess with a person through the recognition of the state of chessboard and selection of the move according to a chosen difficulty level.

Notifications You must be signed in to change notification settings

aledevv/smartchess

Repository files navigation

Smartchess

drawing

Author Author Author

Version Platform MIT License

Application for Raspberry PI to play chess games on a real chessboard against Stockfish chess engine.

Demo video

IMAGE ALT TEXT HERE

Files and directories

  • main.py: main application

  • gui.py: the code of the user interface

  • 2fen_pgn: function to convert 2 FEN images to PGN move

  • constants.py: file in which all constant values and parameters are present

  • engine.py: code of ML models for position inference and game management

  • images folder contains pictures to process.

  • weights: folder containing model weights for detections

Warning

Weights are tuned using our chessboard and pieces. If you want to use yours, you have to fine-tune a new model (this applies to both corner and pieces detection)

Project layout

├── 2fen_pgn.py
├── README.md
├── assets
│   ├── chess2.png
│   ├── corners
│   │   ├── batch_training.jpeg
│   │   ├── charts.png
│   │   ├── corners.png
│   │   ├── labels.jpeg
│   │   └── p-curve.png
│   ├── corners_.png
│   ├── grid_.png
│   ├── photo_.jpeg
│   ├── pieces
│   │   ├── conf_matrix.png
│   │   ├── conf_matrix_normalized.png
│   │   ├── correlogram.jpeg
│   │   ├── labels.png
│   │   ├── plots.png
│   │   ├── pr_curve.png
│   │   ├── results.csv
│   │   ├── roboflow_deploy
│   │   │   ├── model_artifacts.json
│   │   │   ├── results.csv
│   │   │   ├── results.png
│   │   │   └── state_dict.pt
│   │   └── roboflow_deploy.zip
│   ├── pieces_.png
│   └── virtual_.png
├── constants.py
├── engine.py
├── fen2pgn.py
├── gui.py
├── images
│   ├── png
│   │   ├── 1.jpeg
│   │   └── chessboard.png
│   └── svg
│       └── chessboard.svg
├── libcairo.2.dylib
├── main.py
├── requirements.txt
└── weights
    ├── new_corners.pt
    └── pieces.pt

Detection process

  1. When user makes a move, is taken a photo of their chessboard.
  2. A first model detects corners of the chessboard.
  3. Image is cropped and transformed from 3D into 2D and squares position is mapped through a grid.

Note

Since the camera has wide angle the image results to be distorted, as a result the grid could not be perfectly aligned to the squares. An offset parameter has been applied to adjust the grid position once the camera is set on a fixed position.

  1. Pieces are detected with a second model
  2. Piece positions are infered by intersecating bounding box areas with grid cells
  3. FEN is written and it can be exported to Lichess or sent to a Stockfish API for analysis

    The same process is described in this repo.

Training

We used YOLOv8n models to both train corners and pieces recognition. Some training details are shown below.

Corners

Pieces

Requirements

Hardware

  • Raspberry (PI 3/4 suggested)
  • Camera [OV5647]
  • LCD display [K-0403]
  • FFC cable 15 pin
  • Structure: base 34x34cm height 36cm

Software and Dependencies

  • How to set up the environment Rasperry, camera and see if they are working...
  • Every time we train a model we do an helth dataset check which shows the disposition of every piece on the board and how many time it appears on each square. We re-trained the model in the areas where pieces were less rapresented, we did this to improve as much as possible the generalization.

Installation

Clone respository

git clone https://github.com/aledevv/smartchess.git

Install requirements

pip install -r -requirements.txt

Install tk-inter for gui

sudo apt-get install python-tk

Usage

python main.py

The opening windows will ask you to choose difficulty and which color you are going to play. Pressing "Play" a position check will be launched to verify whether the pieces are set in the starting postion, if they don't an error pop-up will be shown, otherwise the game window will appear.

The game window will show the current board position.

If you are playing with the white pices you have to play a move and press "New move".

If you are playing with the black pieces you have to make engine's move, then yours and press "New Move".

Moves are stored on the right side of the window (white's move on the left and black's ones on the right). Each time the engine or the player will make a move a positional check has to be excuted to veify the correctness of the position. In case of illegal move or if the player made engine's move wrongly an error pop-up will be shown, asking to correct the mistake. The games ends either according to chess game rules (checkmate, stalemate, repetition...) or by using the "Resign" button for the user.

Caution

Resign button is heighly suggested to exit the game.

If you want to quit the game, use the "Resign" button instead of closing a window. By opening the game again camera issues could happen.

Links

Authors

License

MIT License click here for more details.

About

Project of Embedded Software for the Internet of Thing course. A system able to play chess with a person through the recognition of the state of chessboard and selection of the move according to a chosen difficulty level.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages