Skip to content

reinmj/SnapSudoku

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SnapSudoku

Take a picture of a Sudoku and have SnapSudoku solve it for you!


TODO:

  • Add code to solve the Sudoku from the predicted output grid.
  • Add proper exception handling, only a basic working model created.
  • Improve algorithm to get better Sudoku Grid extraction, make it more robust against blurs .
  • Improve empty cell detection. Only a basic logic is used right now.
  • Improve accuracy by checking and removing same digits predicted in the same row/column.

How to use:

After cloning the repo, in the SnapSudoku directory, enter the following in the Terminal:

python sudoku.py <path-to-input-image>

Note: OpenCV for Python must be installed.

Working:

Here's a Sudoku image from a smartphone:

Input Sudoku Image


> The current code gives out the following output to the Terminal:

Final set of predicted digits Algorithm

  1. Basic image preprocessing - Thresholding.
  2. Crop out approx. Sudoku puzzle (Largest contour)
  3. Get the grid square vertices: (a better way of doing this is required!)
    3.1. Get the largest contour of the image.
    3.2. Get the largest bounding rectangle within the contour.
    3.3. Compute the grid corners.
  4. Do a Warp perspective on the sudoku image
  5. We will extract cells from this, by slicing the sudoku grid evenly.
  6. Digit isolation in cell is done through a series of steps:
    6.1. Extract the largest connected component in the image, giving more priority to the center pixels.
    6.2. Removing all major noise in the cell.
  7. Predict Digits using a Neural Network.

The only 3rd party library required is OpenCV. The Neural Network created was trained with around 250 digits. The constants used in the training phase, the training data-set is in this repository itself.

Here are some illustrations of the different stages:

After Preprocessing:

After Preprocessing

Final processed Sudoku Grid

Final processed Sudoku Grid

Here are a few digits after processing the cells:

Digit 3Digit 5Digit 8

Here's a typical empty cell:

empty cell

Predicted Grid:

Prediction

Contributions

I'll be very happy to get new ideas to improve the accuracy and make the application better. Feel free to give a pull request!

About

Extract and solve Sudoku from an image

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%