/SudokuSolver

Solving Sudoku using Computer Vision and Backtracking

Primary LanguagePython

Sudoku solver

Installation

The repository can either be cloned or downloaded as a zip.

Install tesseract by following the instructions here

Other requirements can be installed by running pip install -r requirements.txt

Usage

Execute the code as follows :

python main.py 'input.png'

Here the path of the input image is passed as a command line argument.

Working

Preprocessing sudoku image (converting to grayscale, blurring, finding contours and dividing image into 81 squares). Our stackoverflow question summarises most of our work in this module.

The input sudoku image

missing

Converting image to grayscale

missing

Finding the 4 contours of the image

missing

Cropping and warping the image

missing

The final image that is divided into 81 images

missing

Processing of a single box in the sudoku board

missing

missing

(Recognizes the digits from each of the 81 images and stores it in a list)

  • The 81 images are stored in the directory temp
  • Models trained on MNIST performed poor here as these digits were not handwritten. Hence we used pytesseract. This answer was helpful.
  • If the box doesn't contain any number, the list holds . in it's place and 0-9 otherwise.

Verification of predicted digits with user

Each digit predicted is checked with the user to see if it's predicted correctly. If not, the user will be able to correct it accordingly.

The list of length 81 is converted into a 9x9 array. Later we solve this board using backtracking and display the solved board to the user.

OUTPUT

missing

...

missing

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Better techniques for extracting digits from the sudoku image and digit recognition could be done.

Please make sure to update tests as appropriate.

REFERENCES

Python solution to solve sudoku using backtracking in leetcode

Digit Recognition using OpenCV and pytesseract

Sudoku Grabber in OpenCV