Application for Raspberry PI to play chess games on a real chessboard against Stockfish chess engine.
-
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)
├── 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
- When user makes a move, is taken a photo of their chessboard.
- A first model detects corners of the chessboard.
- 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.
- Pieces are detected with a second model
- Piece positions are infered by intersecating bounding box areas with grid cells
- 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.
We used YOLOv8n models to both train corners and pieces recognition. Some training details are shown below.
- Raspberry (PI 3/4 suggested)
- Camera [OV5647]
- LCD display [K-0403]
- FFC cable 15 pin
- Structure: base 34x34cm height 36cm
- 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.
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
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.
MIT License click here for more details.