sherwyn11 / Sudoku-AI

A web application in which AI solves the game of Sudoku!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sudoku-AI

A web application in which AI solves the game of Sudoku!

Overview

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.

Flow of the app:

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.

Demo

Getting started

  1. Clone and download the repo.
  git clone <this_repo_url>
  1. Download the required python packages.
pip install -r requirements.txt
  1. Start the Flask server.
python app.py
  1. Open localhost:5000/solve in the browser.

Limitations and Future Scope

  • 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!

License

License: MIT

MIT License Link


© Sherwyn D'souza 2020

About

A web application in which AI solves the game of Sudoku!

License:MIT License


Languages

Language:Python 89.4%Language:HTML 10.6%