Skip to contents

Running the app

Entry points and the Shiny modules the interface is built from.

run_app()
Run the tanmai Shiny application
mod_analyze_server()
Analyze tab server
mod_analyze_ui()
Analyze tab UI
mod_board_server()
Interactive board server
mod_board_ui()
Interactive board UI
mod_calibrate_server()
Calibrate tab server
mod_calibrate_ui()
Calibrate tab UI
mod_live_server()
Live game tracking server
mod_live_ui()
Live game tracking UI
app_ui()
Top-level Shiny UI
app_server()
Top-level Shiny server
cv_theme()
The application's Bootstrap theme
cv_brand()
Wordmark for the navbar
cv_section_title()
A section heading with an optional lead paragraph

Reading a board

Screenshot to FEN: cropping and splitting the image, matching pieces against template libraries, working out which way round the board is, and assembling a position.

recognize_position()
Recognize a board screenshot end-to-end
prepare_board()
Normalize a screenshot to an exact board-sized RGB image
autocrop_board()
Best-effort crop to the board by trimming near-uniform margins
split_board()
Split a normalized board into 64 square images
split_squares()
Prepare and split a screenshot into 64 square images in one step
classify_square()
Classify one square against a single template library
recognize_symbols()
Classify all 64 squares against a single template library
recognize_symbols_auto()
Recognize 64 squares against all available sets at once
template_matrix()
Stack several libraries' normalized templates into one matrix
build_fen()
Build a full FEN string from recognized symbols
fen_complete()
Fill in the FEN fields people leave off
grid_to_placement()
Convert a 64-symbol grid to a FEN piece-placement field
infer_castling()
Infer castling rights from piece placement
resolve_orientation()
Resolve board orientation
detect_flip()
Detect board orientation from rank labels along the left edge
detect_flip_piece_mass()
Detect orientation from where each army sits
detect_flip_start_brightness()
Detect orientation of a starting position from piece brightness
fix_template_colour_swap()
Assert (and if necessary repair) the white/black brightness invariant
resolve_calibration_flip()
Resolve the orientation of a calibration (starting-position) screenshot

Piece sets and calibration

The bundled cburnett set, the Lichess sets fetched on demand, and teaching the recognizer art it has never seen.

piece_set_manifest()
Manifest of every Lichess piece set
available_piece_sets()
Names of all piece sets available locally
fetch_piece_set()
Download one piece set's 12 SVGs into the cache
setup_piece_sets()
Fetch and build templates for every set in the manifest
build_set_templates()
Build and cache a set's template library
build_from_start_position()
Calibrate a template library from a starting-position board
load_template_library()
Load a template library from an RDS file
load_all_template_libraries()
Load every available template library
save_template_library()
Save a template library to an RDS file
render_position()
Render an 8x8 board with pieces placed from a symbol grid
piece_svg_path()
Resolve the image file path for a piece symbol within a set
read_piece_image()
Rasterize one piece image at a given square size
piece_set_path()
Locate the directory holding a set's 12 SVGs
piece_set_ext()
Find which file extension a downloaded piece set uses
piece_sets_dir()
Cache directory for downloaded piece sets
piece_theme_base()
Piece-image URL template for the interactive board
active_templates_path()
Path to the currently active template library
custom_templates_path()
Path for user-calibrated ("custom") templates
set_templates_path()
Path where a set's calibrated template library lives

Engines

Stockfish over UCI, both one-shot and as a session that keeps searching, plus the chess.js bridge used for legality and SAN.

best_move_uci()
Ask Stockfish for the best move in a position
ensure_stockfish()
Return a path to a Stockfish binary, downloading it on first use
find_local_engine()
Find a Stockfish binary already present on this machine
stockfish_bin_dir()
Directory where the downloaded Stockfish binary is cached
engine_session_start()
Start a persistent Stockfish process
engine_session_analyse()
Point a persistent engine at a position and start an infinite search
engine_session_poll()
Drain pending engine output and fold it into the session state
engine_session_eval()
Evaluate one position on a persistent engine, waiting for the result
engine_session_stop()
Stop a persistent engine session
parse_info_line()
Parse one UCI info line into a principal-variation record
parse_uci_score()
Parse a UCI score from an engine info line
format_score()
Format an engine score for display
eval_bar_pct()
Convert a score to a 0-100 eval-bar percentage (White's share)
wait_for_line()
Read engine output until a line starting with a given prefix appears
new_chess_context()
Create a chess.js V8 context
validate_fen()
Validate a FEN via chess.js
is_valid_fen()
Test whether a FEN is legal
legal_moves()
List every legal move in a position, in UCI
fen_after_move()
The position that results from playing a move
fen_move_to_san()
Convert a UCI move to SAN for a position
fen_placement()
The piece-placement field of a FEN
fen_with_turn()
Rewrite a FEN's side to move
fen_turn()
Which side is to move in a FEN
same_position()
Test whether two FENs describe the same position to play from

The human model

Maia run through lc0, and what it is used for: scoring how likely a mistake is, and inferring how strong the opponent is from what they played.

maia_dir()
Directory holding lc0 and the Maia weight files
maia_weights_path()
Path to a Maia weights file for a rating
match_maia_rating()
Snap a requested rating to the nearest available Maia network
nearest_installed_rating()
Nearest Maia network whose weights are actually on disk
find_lc0()
Locate the lc0 binary
ensure_maia()
Download lc0 and the Maia weights if they are not already present
human_model_available()
Is a human model usable right now?
maia_session_start()
Start a persistent Maia session
maia_session_stop()
Stop a Maia session
human_move_probabilities()
Probability a modelled human plays each legal move
parse_policy_lines()
Parse lc0's verbose move statistics into per-move probabilities
blunder_risk()
Assess how likely a human is to go wrong in a position
evaluate_moves()
Evaluate a specific set of moves in one search
score_to_cp()
Convert a UCI score into a single centipawn number
trappiness()
Rate moves by how likely they are to induce an opponent mistake
radar_arrow_moves()
Rank your moves by how likely they are to induce an opponent error
maia_pool_start()
Start one Maia session per rating
maia_pool_stop()
Stop a pool of Maia sessions
move_log_likelihood()
Log-probability of one played move under each rating's network
rating_posterior()
Posterior over ratings from accumulated log-likelihoods
estimate_rating()
Estimate an opponent's rating from the moves they played
rating_estimate_is_confident()
Is a rating estimate strong enough to act on?

Following a game

Turning a stream of screenshots into a game: the legality gate that decides what counts as a move, and the summaries built on top of it.

game_new()
Start a new tracked game
game_apply_move()
Play a move onto a tracked game
game_current_fen()
The position a tracked game currently stands at
game_ply()
Number of plies played in a tracked game
game_accept()
Fold an accepted observation into the game
track_observation()
Decide what a freshly recognized position means for a tracked game
find_move_paths()
Every short legal path from one position to a target placement
move_quality()
Classify how bad a move was
game_move_losses()
What each move cost the player who made it
game_turning_points()
The moves where the game turned
game_from_pgn()
Build a tracked game from PGN
pgn_summary()
A one-line description of an imported game
format_loss()
Describe what a move cost, in words a player would use
eval_graph_geometry()
Geometry for the evaluation graph
eval_graph_svg()
Draw the evaluation across a game as an inline SVG

Internals

Helpers that support the above.

app_sys()
Locate a file installed under inst/
cfg_env()
Read a configuration setting from the environment
golem_add_external_resources()
Register static resource paths and add css/js includes
image_input_ui()
Drag-drop file input plus a clipboard-paste zone
latest_image_input()
Combine upload and paste events into one image reactive
decode_data_url_image()
Decode a data-URL image string into a magick image
to_gray_matrix()
Convert a magick image to a grayscale integer matrix
normalize_vec()
Zero-mean, unit-norm flatten of a matrix
sq_background()
Estimate a square's background level from its four corners
sq_core()
Background-subtracted central region of a square
sq_energy()
Mean absolute energy of a square core
sq_idx()
Map a 0-indexed board row/column to a 1-indexed symbol-vector position
squares_mean_brightness()
Mean background-subtracted brightness of the pieces in a set of squares
label_ink()
Foreground ink in the rank-label strip of a left-edge square
tanmai tanmai-package
tanmai: Chess Board Screenshot to Best Move