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, it vastly increases prediction accuracy, encompassing a wide variety of chess image formats from different sources. 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 basis for the training algorithm, originally a simple CNN architecture built on a no longer supported version of TensorFlow 2. This version transitions to PyTorch and vastly improves prediction accuracy on a wide variety of chess image formats.


🧪 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:

11111111/11111111/11111111/1111p1K1/11k1P111/11111111/11111111/11111111

🖼️ Sample Results

📷 Input:

🎯 Predicted FEN:

11111111/11111111/11111111/1111p1K1/11k1P111/11111111/11111111/11111111

---

🚀 Getting Started

Requirements

  • Python 3.10+
  • PyTorch
  • Other dependencies in requirements.txt
pip install chessimg2pos

or

git clone https://github.com/mdicio/chessimg2pos
pip install -r requirements.txt

🙏 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.4.tar.gz (140.0 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.4-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for chessimg2pos-0.1.4.tar.gz
Algorithm Hash digest
SHA256 82ab0d6a0065a20111c9360e8b62fb3a7becf46534fc9d78378b17cfa5b32628
MD5 fa9954b530fbd9429b477c89ab9a33ef
BLAKE2b-256 ba38af03b0d764d6cb112d7a3f914a43b9b558fea47ce3fad1cf95331b53b631

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chessimg2pos-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 877b50cb70b23d00712ecb4d94faff98288d2a44aa5cd73a7da786d2a623a494
MD5 3ba6f02ac39c588a60c42afd0d616753
BLAKE2b-256 42fad4f62a9845bde108bc3bd5642434dbc07d58df8bca94f9432b5fbad403a8

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