noword / gbr

Go board image recognition

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go board image recognition (GBR)

This project is aiming to create a program which will be able to analyse a image of a Go game board in order to determine board parameters and stone positions.

The project is build on wonderfull OpenCV library.

A lot of ideas and algorithms were found on excellent Adrian Rosebrock's PyImageSearch site and borrowed from thematical Slashdot threads.

The algorithm per se is the following:

  1. Detect board properties (board edges, spacing and board size):

    • Transform image using 4-points transformation
    • Set area to be recognized
    • If parameters set - run HoughLinesP to determine line segments, filter out small lines and reconstruct the image, allowing to remove board labels (this step seems obsolete, currently its easier to apply area mask excluding labels)
    • Run HoughLines to find all the lines across the board
    • Separate lines to vertical/horizontal ones
    • Remove duplicates and lines too close to each other
    • Calculate board edges as minimum and maximum coordinates of horizontal/vertical lines
    • Detect a board size as number of horizontal/vertical lines found.
  2. Find stones (black and white):

    • Apply pre-filters with parameters specified through the interface
    • Run HoughCircles to detect circles and convert found X,Y coordinates to board position
    • Apply post-filters to tune stone radius
  3. Eliminate duplicates where black and white stones occupy the same board position

Currently, the following filters are implemented:

  • Channel splitting (red channel is used in white stone detections, blue - in black one)
  • Thresholding
  • Dilating
  • Eroding
  • Blur
  • Pyramid mean filtering (useful when stones have textured faces or extensive glare)
  • Luminosity equalization using CLAHE
  • Watershed (post-filter).

Filter and board detection parameters can be changed through the interface and automatically saved to a property file (.GPAR). The property file is loaded automatically when an image is loaded for processing.

Recognised boards can be saved to an SGF file. The program creates standard SGF file, but, obviously, it cannot reproduce an actual game flow and just saves stones one by one.

Please note that this is not an AI system, but a "good old" algorythmic program. I couldn't manage to build an AI due to lack of time and compexity of the task, so it relies on computer vision algorithms to do the job. In particular, this means that correct board recognition is highly dependent on correct parameters set by end user, so even small changes could improve or reduce results.

Currently, I'm working on automatic parameter selection using Scikit-optimize library. This is really great lib, which simplifies multi-dimensional hyperparameter optimization very much, but, still, this is kind of experimental feature now.

Examples of source images (left) and results of their processing (right):

1 Plain computer board (source) with non-standard number of lines

2 A computer board showing results of score calculation

3 Real board (source)

4 Real board (source) with 4-points transformation and luminosity normalization applied

More images are available at img directory. All of them are either taken from my games or found on the Internet.

Requirements

Python 3.5 or later. Packages:

Installation and running

  1. Clone a repository to local disk by running:
git clone https://github.com/skolchin/gbr.git

This will downlowd all code and database of images (img/ folder) along with parameters I have set.

  1. Install packages. Please note that imutils and sgfmill packages are not included into Anakonda database, so they could be installed by pip only.

  2. Run python gbr2.py.

TODO

  • Find some ways to deal with glare on the stones

  • Allow to save recognized stone positions

  • Add stone reconcilation (detection of stones occupying the same position)

  • Add logging during board processing/dataset generation

  • Adopt the algorithm to the photos of real boards

  • Make "tagger" interface to simplify image processing

  • Implement board capture from webcam (new interface)

  • Add stones removal/adding/color changing

  • Implement score calculation

  • Add SGF file creation

  • Make a web interface with (probably) cloud deployment (postponded)

  • Make DLN dataset creation and review interfaces

  • Train DLN model to recognize stones on computer boards

  • Train DLN model to recognize stones on real boards

  • Create a mobile app (postponded)

  • Automatic parameters selection

About

Go board image recognition

License:MIT License


Languages

Language:Python 99.0%Language:Perl 0.6%Language:Batchfile 0.4%