Code Monkey home page Code Monkey logo

chessboard-recognizer's Introduction

Chessboard recognizer

Uses a convolutional neural network to recognize the positions of pieces on a chessboard image.

If you have an image of a chessboard in chessboard.png

Run the program like this

./recognize.py chessboard.png

To get the chessboard position in FEN notation

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

Sample results

Chess puzzle from a book:

Predicted: 2r2k1r/6bp/p3q3/4pp1Q/1p1n2P1/N7/PPP3BP/2KR1R2 (99.633% confidence)

Lichess analysis board diagram with arrows:

Predicted: 5r1k/2q1r1pp/2p4n/2P2B2/pPQ1pR2/P5P1/4R2P/7K (99.997% confidence)

Getting started

You'll need python 3 and Tensorflow 2

Set up your virtualenv and install python dependencies

virtualenv venv
source venv/bin/activate
pip3 install -r requirements.txt

You'll need a neural network model to use ./recognize.py

To use a pre-trained model, download nn.zip and unzip in the project root folder.

To train your own model, you'll first need lots of images of chessboards

  • For the images used in the pre-trained model, download training-images.zip and unzip in the project root directory
  • Or generate your own training images with this script:
    • ./generate_chessboards.py downloads a bunch of chessboard images with randomly-placed pieces

Then run this script to convert the chessboard images into 32x32 PNGs of each square of the board

  • ./generate_tiles.py converts these downloaded chessboard images into 32x32 PNGs used for training

Once you have tiles images ready for the training inputs, run this:

  • ./train.py creates a new neural network model

Once you have a neural network model ready, run ./recognize.py with a path to a chessboard image:

./recognize.py ~/Desktop/chessboard.png

Debugging

To verify that the generated 32x32 PNG tile images match the source chessboard image, use this script:

  • ./view_images.py for a convenient way to manually verify the generated images

Then open images.html to view the chessboard and tile images with their corresponding pieces.

To debug the predicted outputs, open debug.html after running ./recognize.py to view the actual/predicted boards

image

Each prediction shows the actual board, the predicted board, the prediction confidence for each square, and a link to a board editor so you can edit the actual FEN in case the predicted FEN is wrong.

Incorrect or low-confidence predictions are a great source of training chessboard images.

For a convenient way to add a training image, use this script:

  • ./save_chessboard.py chessboard.png <subdirectory> <actual fen>

Then you can generate more tiles and re-train the model for more-accurate future predictions.

Acknowledgements

chessboard-recognizer's People

Contributors

linrock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

chessboard-recognizer's Issues

conv2d_input to have shape (32, 32, 3) but got array with shape (32, 32, 1)

Can't recognize FEN from the image:

./recognize.py in2.png  
Tensorflow 2.1.0
Predicting chessboard in2.png
Traceback (most recent call last):
  File "./recognize.py", line 139, in <module>
    print(predict_chessboard(chessboard_image_path, args))
  File "./recognize.py", line 91, in predict_chessboard
    (fen_char, probability) = predict_tile(tile_img_data)
  File "./recognize.py", line 115, in predict_tile
    probabilities = list(model.predict(np.array([tile_img_data]))[0])
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 1013, in predict
    use_multiprocessing=use_multiprocessing)
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 498, in predict
    workers=workers, use_multiprocessing=use_multiprocessing, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 426, in _model_iteration
    use_multiprocessing=use_multiprocessing)
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 646, in _process_inputs
    x, y, sample_weight=sample_weights)
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2383, in _standardize_user_data
    batch_size=batch_size)
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2410, in _standardize_tensors
    exception_prefix='input')
  File "/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 582, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected conv2d_input to have shape (32, 32, 3) but got array with shape (32, 32, 1)

Chessboard won't get recognized correctly

Hello, the chessboards won't get recognized correctly or the tiles cannot be created, when I used screenshot. I checked the created tiles, the program detects chessboard wrong for example with the edges or asymmetrical. That's why It cannot recognize the chessboards from screenshots that I cut manually.

Maybe could you upgrade the program? ChessVisionBot from kochsebastian has a good chess recognition function, you can check it for an example.

Greetings
Ercan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.