A web application in which AI solves the game of Sudoku!
Sudoku is a logic-based, combinatorial number-placement puzzle. In classic sudoku, the objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid contain all of the digits from 1 to 9.
The app is divided into 3 main parts:
1. Uploading a Sudoku game image:
- A user uploads a picture of the sudoku which is needed to be solved.
- The uploaded image is then sent to the Flask server.
2. Cell Extraction & Digit Recognition:
- The uploaded image once recieved at the Flask server is then preprocessed.
- For preprocessing, the image goes through many processes such as Adaptive Thresholding, Binary Inversion, Dilation, Flood-Filling & Erosion.
- This helps in finding the biggest blob i.e. border of the sudoku(incase of improper image).
- Then, lines are drawn on the image to find the borders of the sudoku board. The required part is then cropped and used for further purposes.
- Once the sudoku board is extracted, the board is divided into 81 cells. This helps in digit recognition.
- Each cell image is then reshaped into (28, 28, 1) which is the shape of images in the MNIST(handwritten digits dataset) on which the model has been trained.
- CNN(Convolution Neural Networks) was used for training on the images if the MNIST dataset. (Some articles suggested KNN(K-Nearest Neighbours) is a better approach. Worth trying in the future.)
- The model predicts on each cell image. Flood-filling was done on the cell images. If any image consists less than 5 white pixels, it is considered as an empty cell and assigned a value 0, else the model predicts the number in the cell from the cell image.
- To know more view the preprocess.py and recognition.py files.
3. Solving the Sudoku:
- The extracted digits are then fed into another model.
- This model has been trainined on the 1 million Sudoku games dataset. You can download it from here
- The model then predicts the numbers that will fill the empty spaces and renders it back to the web page.
- To know more view the solver.py file.
The trained models are present in the models folder.
- Clone and download the repo.
git clone <this_repo_url>
- Download the required python packages.
pip install -r requirements.txt
- Start the Flask server.
python app.py
- Open localhost:5000/solve in the browser.
- Extraction of the Sudoku board is not extremely accuarate in some cases.
- KNN instead of CNN for digit recognition.
- Accuracy of the models can be worked on.
Will work on these in the future!
© Sherwyn D'souza 2020