Find chessboard FEN from a screenshot using TensorflowJs
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
frozen_model
LICENSE
README.md
chessboard_detection.js
chessbot_tensorflow_model.js
example_input.png
filters.js
index.html
readme_cropped_input.png
readme_gradient_find.png
readme_input.png
readme_prediction.png

README.md

TensorflowJs Chessboard Prediction from Screenshots in HTML5

Given a screenshot which has an online chessboard in it somewhere, it finds the chessboard and predicts the layout of the pieces on it. Everything runs client-side live using TensorflowJs

Live Demo

How it works

This uses a frozen Tensorflow Chessbot model. Unlike that repo this runs completely in javascript using TensorflowJs, all client-side.

Board detection

Finds and separates out the chessboard into a 256x256 px image containing the 32x32 tiles of the chessboard. A very simplified chessboard detector is implemented. It requires the board to mostly fill up the image, be very well aligned and centered. In comparison chessfenbot can find aligned chessboards all around the image. Issue #2

The image is blurred and then the sobel gradients X and Y are used to find strong vertical and horizontal edges in the image corresponding to the chessboard. This is used to guess the bounds of the chessboard, and then generate a 256x256 px grayscale image as input for the tensorflow model.

ML Prediction

Given 256x256px aligned chessboard image, it runs a TensorflowJs model to predict pieces on it. We freeze a model graph and weights from Tensorflow Chessbot, and load it up in Javascript using TensorflowJs, allowing everything to run locally client-side.

The model was trained using 32x32 px grayscale tiles of chessboard pieces. For each tile it independently predicts one of 13 classes, 6 white pieces, 6 black pieces, or an empty tile.

Running locally

A web server of some sort is needed to allow cross-origin sharing when running locally.

For example, one can use http-server.

http-server -o --cors -c-1 -a localhost