chesscog.recognition package

Chess inference module that brings the whole pipeline together.

Submodules

chesscog.recognition.evaluate module

Script to evaluate the performance of the recognition pipeline.

$ python -m chesscog.recognition.evaluate --help
usage: evaluate.py [-h] [--dataset {train,val,test}] [--out OUT]
                   [--save-fens]

Evaluate the chess recognition system end-to-end.

optional arguments:
  -h, --help            show this help message and exit
  --dataset {train,val,test}
                        the dataset to evaluate (if unspecified, train and
                        val will be evaluated)
  --out OUT             output folder
  --save-fens           store predicted and actual FEN strings
chesscog.recognition.evaluate.evaluate(recognizer: chesscog.recognition.recognition.TimedChessRecognizer, output_file: IO, dataset_folder: pathlib.Path, save_fens: bool = False)

Perform the performance evaluation, saving the results to a CSV output file.

Parameters
  • recognizer (TimedChessRecognizer) – the instance of the chess recognition pipeline

  • output_file (typing.IO) – the output file object

  • dataset_folder (Path) – the folder of the dataset to evaluate

  • save_fens (bool, optional) – whether to save the FEN outputs for every sample. Defaults to False.

chesscog.recognition.recognition module

Module that brings together the whole recognition pipeline into a single class so it can be conveniently executed.

This module simultaneously acts as a script to perform a single inference:

$ python -m chesscog.recognition.recognition --help
usage: recognition.py [-h] [--white] [--black] file

Run the chess recognition pipeline on an input image

positional arguments:
  file        path to the input image

optional arguments:
  -h, --help  show this help message and exit
  --white     indicate that the image is from the white player's
              perspective (default)
  --black     indicate that the image is from the black player's
              perspective
class chesscog.recognition.recognition.ChessRecognizer(classifiers_folder: pathlib.Path = URI('models://'))

Bases: object

A class implementing the entire chess inference pipeline.

Once you create an instance of this class, the CNNs are loaded into memory (possibly the GPU if available), so if you want to perform multiple inferences, they should all use one instance of this class for performance purposes.

predict(img: numpy.ndarray, turn: bool = True) Tuple[chess.Board, numpy.ndarray]

Perform an inference.

Parameters
  • img (np.ndarray) – the input image (RGB)

  • turn (chess.Color, optional) – the current player. Defaults to chess.WHITE.

Returns

the predicted position on the board and the four corner points

Return type

typing.Tuple[chess.Board, np.ndarray]

class chesscog.recognition.recognition.TimedChessRecognizer(classifiers_folder: pathlib.Path = URI('models://'))

Bases: chesscog.recognition.recognition.ChessRecognizer

A subclass of ChessRecognizer that additionally records the time taken for each step of the pipeline during inference.

predict(img: numpy.ndarray, turn: bool = True) Tuple[chess.Board, numpy.ndarray, dict]

Perform an inference.

Parameters
  • img (np.ndarray) – the input image (RGB)

  • turn (chess.Color, optional) – the current player. Defaults to chess.WHITE.

Returns

the predicted position on the board, the four corner points, and a dict containing the time taken for each stage of the inference pipeline

Return type

typing.Tuple[chess.Board, np.ndarray, dict]

chesscog.recognition.recognition.main(classifiers_folder: pathlib.Path = URI('models://'), setup: callable = <function <lambda>>)

Main method for running inference from the command line.

Parameters
  • classifiers_folder (Path, optional) – the path to the classifiers (supplying a different path is especially useful because the transfer learning classifiers are located at models://transfer_learning). Defaults to models://.

  • setup (callable, optional) – An optional setup function to be called after the CLI argument parser has been setup. Defaults to lambda:None.