Skip to main content

Convert chess board images to FEN notation using deep learning

Project description

Chess FEN Detector

A Python package that uses deep learning to detect chess piece positions from images and convert them to FEN (Forsyth-Edwards Notation).

Features

  • ๐ŸŽฏ Accurate chess piece detection using CNN
  • ๐Ÿ–ผ๏ธ Support for various image formats (PNG, JPG, etc.)
  • ๐Ÿ“ฆ Easy-to-use Python API
  • ๐Ÿ’ป Command-line interface
  • ๐Ÿš€ Bundled pre-trained model included

Installation

From source

# Clone or download this package
cd chess-fen-detector

# Install in development mode
pip install -e .

# Or install normally
pip install .

Install dependencies only

pip install -r requirements.txt

Usage

Python API

Basic usage

from chess_fen_detector import ChessFENDetector

# Initialize the detector
detector = ChessFENDetector()

# Get FEN notation from an image
fen = detector.predict_fen('path/to/chessboard.png')
print(f"FEN: {fen}")

Display image with FEN

from chess_fen_detector import ChessFENDetector

detector = ChessFENDetector()

# Display the image with predicted FEN as title
detector.display_with_fen('path/to/chessboard.png')

Using convenience functions

from chess_fen_detector import get_fen, display_with_predicted_fen

# Quick FEN extraction
fen = get_fen('path/to/chessboard.png')

# Display with FEN
display_with_predicted_fen('path/to/chessboard.png')

Using a custom model

from chess_fen_detector import ChessFENDetector

detector = ChessFENDetector(model_path='path/to/custom_model.keras')
fen = detector.predict_fen('chessboard.png')

Command Line Interface

After installation, you can use the chess-fen-detect command:

# Basic usage
chess-fen-detect chessboard.png

# Display the image with predicted FEN
chess-fen-detect chessboard.png --display

# Use a custom model
chess-fen-detect chessboard.png --model custom_model.keras

# Enable verbose output
chess-fen-detect chessboard.png --verbose

FEN Notation

FEN (Forsyth-Edwards Notation) is a standard notation for describing chess positions. The package outputs FEN strings that represent the piece positions on the board.

Example output: rnbqkbnr-pppppppp-8-8-8-8-PPPPPPPP-RNBQKBNR

Piece symbols:

  • Lowercase: black pieces (p=pawn, r=rook, b=bishop, n=knight, q=queen, k=king)
  • Uppercase: white pieces (P=pawn, R=rook, B=bishop, N=knight, Q=queen, K=king)
  • Numbers: empty squares (1-8)
  • Dashes (-): separate ranks (rows)

Requirements

  • Python >= 3.7
  • TensorFlow >= 2.8.0
  • scikit-image >= 0.19.0
  • NumPy >= 1.21.0
  • Matplotlib >= 3.3.0

Model Information

The package includes a pre-trained CNN model (chess_best_model_mini.keras) that:

  • Takes chess board images as input
  • Resizes and processes them into 64 tiles (one per square)
  • Predicts the piece on each square
  • Outputs FEN notation

Package Structure

chess-fen-detector/
โ”œโ”€โ”€ chess_fen_detector/
โ”‚   โ”œโ”€โ”€ __init__.py          # Package initialization
โ”‚   โ”œโ”€โ”€ detector.py          # Main detector class
โ”‚   โ”œโ”€โ”€ cli.py              # Command-line interface
โ”‚   โ””โ”€โ”€ models/
โ”‚       โ””โ”€โ”€ chess_best_model_mini.keras  # Pre-trained model
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ example_usage.py    # Usage examples
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_detector.py    # Unit tests
โ”œโ”€โ”€ setup.py                # Setup configuration
โ”œโ”€โ”€ pyproject.toml          # Modern Python packaging config
โ”œโ”€โ”€ requirements.txt        # Dependencies
โ”œโ”€โ”€ MANIFEST.in            # Package data files
โ”œโ”€โ”€ README.md              # This file
โ””โ”€โ”€ LICENSE                # License information

Development

Running tests

pip install -e ".[dev]"
pytest tests/

Code formatting

black chess_fen_detector/
flake8 chess_fen_detector/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Acknowledgments

  • Uses TensorFlow/Keras for deep learning
  • Built with scikit-image for image processing

Troubleshooting

Model not found error

Make sure the model file is included in the package:

# Check if model exists
ls chess_fen_detector/models/

TensorFlow installation issues

For GPU support:

pip install tensorflow[and-cuda]

For CPU-only (smaller installation):

pip install tensorflow-cpu

Future Improvements

  • Support for different board orientations
  • Confidence scores for predictions
  • Batch processing of multiple images
  • Web API interface
  • Mobile-optimized model

Contact

For questions or issues, please open an issue on the GitHub repository.

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

chess_fen_detector-0.2.0.tar.gz (14.3 MB view details)

Uploaded Source

Built Distribution

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

chess_fen_detector-0.2.0-py3-none-any.whl (14.3 MB view details)

Uploaded Python 3

File details

Details for the file chess_fen_detector-0.2.0.tar.gz.

File metadata

  • Download URL: chess_fen_detector-0.2.0.tar.gz
  • Upload date:
  • Size: 14.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for chess_fen_detector-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4c689d79c96e169d743b45c06c25c5b45f50e7589a3f3781593871b3ad56c4a9
MD5 f3d1bb73b2ffa1153eaae00ec606bf2a
BLAKE2b-256 b6ffa145fa9b687c03cd3b2f8df17d46a79a93e826bd2b56736cdb0557a7ac50

See more details on using hashes here.

File details

Details for the file chess_fen_detector-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for chess_fen_detector-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b067774d58f3797272b6655b198babb1c84db3f5fed1b08414217260a1220802
MD5 6c72eb76011208a863efdb36f47b7228
BLAKE2b-256 597546d0ecd9b95f62fb2b8cdbe7416556dee8530bf26d1e14c5bf2e42236907

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