Skip to main content

Recognize chess positions from board images using deep learning

Project description

🧠 Chessboard Recognizer (Convert your chess images to FEN positions with one click!)

This project uses a deep learning model implemented in PyTorch to recognize the positions of chess pieces on a chessboard image and convert it into FEN notation. This library introduces an easy and fast function to simply predict a fen from an image. For more advanced usage it also provides reusable components for training, inference, and data preparation.

Full credits to linrock/chessboard-recognizer for chess image data, preprocessing and training algorithm, originally built on a no longer supported version of TensorFlow 2. This version transitions to PyTorch,


🧪 Usage Example

Check the demo usage notebook for more advanced usages (training/inference) 📓 examples/demo_usage.ipynb

Predict from an image

from recognizer import predict_fen
fen = predict_fen("../images/chess_image.png")
print(fen)

Output:

3rkb1r/1pp2ppp/2n1q1n1/p3Pb2/2Pp4/PN3NB1/1P1QPPPP/3RKB1R

🖼️ Sample Results

📷 Input:

🎯 Predicted FEN:

3rkb1r/1pp2ppp/2n1q1n1/p3Pb2/2Pp4/PN3NB1/1P1QPPPP/3RKB1R

---

🚀 Getting Started

Requirements

  • Python 3.10+
  • PyTorch
  • Other dependencies in requirements.txt
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

🏋️‍♀️ Training

Prepare a training set:

  • Use our provided dataset or
  • Generate your own with:

Train the model:

python train.py

🛠️ Tools

  • view_images.py: Debug 32x32 tile alignment with source chessboard
  • save_chessboard.py chessboard.png <subdirectory> "<actual_fen>": Save misclassified examples to improve training
  • debug.py: Visualize prediction confidences and FEN accuracy

🙏 Acknowledgements

This project is a continuation and modernization of:

Major thanks to these creators — this project wouldn’t exist without their work.

🧠 Core Classes

This project is centered around two powerful classes that handle training and prediction with a modern PyTorch-based architecture.

🔧 ChessRecognitionTrainer

Handles training and evaluation of the CNN-based chess piece classifier.

Example:

from chessimg2pos import ChessRecognitionTrainer

trainer = ChessRecognitionTrainer(
    images_dir="../../training_images/chessboards", # replace with your path
    model_path="../../models/test_model.pt",# replace with path where you want models tgo be saved
    generate_tiles=False,  # Set to True if tiles need to be generated from boards
    epochs = 5,
    overwrite = False
)
model, device, accuracy = trainer.train()

🔍 ImprovedChessPositionPredictor

Loads a trained model and predicts a FEN string from a chessboard image.

Example:

from chessimg2pos import ChessPositionPredictor

predictor = ChessPositionPredictor("../../models/test_model.pt")
result = predictor.predict_chessboard("../images/ccom_1.png", return_tiles=True)

print("Predicted FEN:", result["fen"])
print("Confidence:", result["confidence"])
predictor.visualize_prediction(result)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chessimg2pos-0.1.1.tar.gz (339.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chessimg2pos-0.1.1-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file chessimg2pos-0.1.1.tar.gz.

File metadata

  • Download URL: chessimg2pos-0.1.1.tar.gz
  • Upload date:
  • Size: 339.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for chessimg2pos-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f640cac85dc4798d0d954892d1087beea6bb886896fa4ec1ea6a454ec536cff1
MD5 f9edb644fecf66051820b3ae914eea33
BLAKE2b-256 3d38d12659b833c7fe8d933507a219b66d48ce7ce64044974fc9f4505d61a5a4

See more details on using hashes here.

File details

Details for the file chessimg2pos-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for chessimg2pos-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 867dad9cb0a844a6ce828ff5c7a585b72ca5f53ce8f9a8f28f5237c6cea3d8e6
MD5 40c956f43f090dcabdd0b2ebeee5a831
BLAKE2b-256 d861dc35f85f282966106586e7634282ef2e82fed30062856eabf0ed4dc44870

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page