chessboarddetect

Hodgepodge of chessboard chessboard detection algorithms on images from actual matches.

https://github.com/elucidation/chessboarddetect

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.6%) to scientific vocabulary

Keywords

chess chessboard-detection chessboard-recognition tensorflow
Last synced: 6 months ago · JSON representation

Repository

Hodgepodge of chessboard chessboard detection algorithms on images from actual matches.

Basic Info
Statistics
  • Stars: 98
  • Watchers: 4
  • Forks: 21
  • Open Issues: 2
  • Releases: 0
Topics
chess chessboard-detection chessboard-recognition tensorflow
Created over 9 years ago · Last pushed over 1 year ago
Metadata Files
Readme License Citation

README.md

Chessboard Detection

2024: I've started a small youtube series explaining this repository: Teaching AI To See

Note : This repo is a staging ground of half-baked hacky code and different approaches to chessboard detection. Several different algorithms have been implemented each with varying tradeoffs.

SpeedChessTrackedLK Live Detection Speed Live Detection

More Tracking videos: https://photos.app.goo.gl/QYBZH4PrmR1FKaUa9

Desktop real-time demo (~30-200ms per frame) of the chessboard detector running, each frame is processed initially by the classifier, and once a match is found, Lucas-Kanade tracking is used to follow it onwards, occassionaly resetting if enough points are lost in the transition. It's written all in python (portions were written in C++/Halide but I've ended up not using them yet due to build issues).

The algorithm is a combination of opencv for video capture and several basic vision algorithms, finding saddle points in the image, which are then classified using a Tensorflow DNNClassifier. After that all potential chess tile quads are used to warp the points onto an ideal unity grid and they are scored for grid-i-ness, a kind of brutesac (ransac without the random). Lots of opportunities for optimization to be had.

Once the best scoring grid is found, we try and fit a chessboard to the points in unity grid space, then do one more refinement with opencv's findHomography and the final chessboard transform is shown as a red outline.

Example Unity Grid

Goal

  • Given a photo with a chess board in it, find the chessboard grid. The goal is to make this fast enough to run real-time on an android phone.

Labeled

This is to be an evolution on Tensorflow Chessbot, working with real images.

Algorithm #4 CNN Classifier + Lucas-Kanade tracking for consistency through video frames (~50-200ms per frame)

Trained both a CNN and a DNN, found the CNN to work better. Trained with with ~12k tiles (6k each of true/false chess corners) gets us to roughly 96% success rate before over-training. Combining Algorithm #3 with a Lucas Kanade tracking of found chessboard points gives more consistent chessboards across frames.

SpeedChessTrackedLK

More Tracking videos: https://photos.app.goo.gl/QYBZH4PrmR1FKaUa9

Notes: It does not yet track off-by-one errors, this is a reasonably simple fix (TODO).

The dataset can be found here. Please cite this repository if you use it, you can pull the citation from the about section.

Algorithm #3 (DNNClassifier) (~100-200ms per image)

One separate track is real-time chessboard classification on video feeds such as off of youtube videos. Using a combination of x-corner saddle detection and an ML DNN Classifier trained off of the previous algorithms on tiles of saddle points, we can find a triangle mesh for 'mostly' chessboard corners in realtime' (~20ms per 960x554 px frame). This is with python and opencv, the saddle point detection takes ~15ms, using a C++ Halide routine we've gotten this as low as 4ms, so there's lots of room for optimization in the future.

Triangle Mesh

Algorithm #2 (~30 sec per image)

  1. Find potential quad contours within the image
  2. Grow out a grid of points from potential contours, vote for best match to saddle points
  3. Warp image to grid, find set of 7 hough lines that maximize alternating chess-tile gradients
  4. Build rectified chess image and overlay image with final transform

Here are several results, 36 successes and 3 failures, red lines overlay the board outline and internal saddle points.

Results

Algorithm #1 (~minute per image)

Animation of several rectified boards that were found from images such as the one below

Animated Rectified Images

Find chessboard and warp image

  1. Use Probabilistic Hough Transform to find lines
  2. Prune lines based on strong alternating normal gradient angle frequency (checkerboard pattern)
  3. Cluster line sets into segments, and choose top two corresponding to two axes of chessboard pattern
  4. Find set of line intersections to define grid points
  5. Take bounding corner grid points and perspective warp image
  6. Re-center tile-map and refine corner points with cornerSubpix
  7. Refine final transform with updated corners & rectify tile image
  8. Correlate chessboard with tiled pattern, rotate 90 deg if orientation of dark/light tiles is off (A1 of H8 tiles must always be black in legal games, turns out a lot of stock images online don't follow this)

Old Example

We find the chessboard and warp the image

Example warpimage

TODO, split warped image into tiles, predict chess pieces on tiles

Some constraints:

  • Chessboard can be populated with pieces, possibly partially occluded by hands etc. too
  • Background can be crowded
  • No user input besides the image taken

Prior Work

There exists checkerboard detection algorithms, but they assume no occlusions, consistent black/white pattern, and clean demarcation with the background. We may be dealing with textured/patterned surfaces, heavy occlusion due to pieces or people's hands, etc., and the background may interrupt to the edges of the chessboard.

Current Work

A promising approach is Gradient Angle Informed Hough transforms to find chessboard lines, once again taking advantage of the alternating gradient angles of the internal chessboard tiles.

Owner

  • Name: Sam
  • Login: Elucidation
  • Kind: user
  • Location: Seattle

GitHub Events

Total
  • Watch event: 20
  • Fork event: 1
Last Year
  • Watch event: 20
  • Fork event: 1

Committers

Last synced: 11 months ago

All Time
  • Total Commits: 60
  • Total Committers: 1
  • Avg Commits per committer: 60.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 2
  • Committers: 1
  • Avg Commits per committer: 2.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Sam e****n@g****m 60

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 3
  • Total pull requests: 0
  • Average time to close issues: 12 months
  • Average time to close pull requests: N/A
  • Total issue authors: 3
  • Total pull request authors: 0
  • Average comments per issue: 1.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • omarbenk (1)
  • InstantWindy (1)
  • Elucidation (1)
Pull Request Authors
Top Labels
Issue Labels
enhancement (1)
Pull Request Labels