Skip to content

saharshleo/sudokuSolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sudoku Solver

Hello World of Computer Vision and Machine Learning

Table of Contents

About The Project

DemoImg

Solve sudoku puzzle using computer vision and machine learning. Currently(v1.0) solves sudoku from image and overlays missing digits on it


Steps followed in this process:

  1. Preprocess image (resize, grayscale, blur, threshold, dilation/opening)
original resized gray blur threshold negate dilate
Original Resized GrayScale Gaussian Blur Adaptive Threshold Negate Dilation
  1. Draw external contours.
contour
External Contours

Current method assumes that the sudoku has largest contour in given image

  1. Infer corners of largest contour(sudoku puzzle) and Perspective transformation based on corners
corner transform
Corners Transform
  1. Infer grid from transformed image.
grid
Grid

Current method just divides the transformed image into number of cells in sudoku puzzle i.e 81

  1. Extract digits from cell by finding largest connected pixel structure in mid part of cell. Scale and centre each digit, so that it becomes apt for prediction using neural network
digits
Extracted Digits
  1. Classify Digits using trained model
  2. Solve the grid using backtracking algorithm
  3. Draw the numbers on black background, inverse transform it and add it to original image
missing_digit solution
Missing Digits Solution

Tech Stack

File Structure

.
├── run.py                  # Driver code
├── utils                   # helper classes
│   ├── img_processing.py   # helper functions for image processing
│   ├── classify_digit.py   # helper functions for digit classification
│   └── solve_sudoku.py     # helper functions to solve partially filled sudoku
├── test_imgs               # images for testing
├── assets                  # for readme
├── digit_classifier        # codes to train digit classifier
├── LICENSE
└── README.md 

Getting Started

Prerequisites

Tested on -

  • Tensorflow v2.2.0
  • OpenCV v4.1.0
  • numpy v1.18.5
  • scipy v1.4.1
  • tabulate v0.8.7

Installation

  1. Clone the repo
git clone https://github.com/saharshleo/sudokuSolver.git
  1. Download the pretrained model from releases v1.0
  2. Extract the model inside sudokuSolver/models/

Usage

cd /path/to/sudokuSolver
python run.py

For viewing the journey of image

python run.py --show True

Results

Inference

To Do

  • v1.0 Solve using Image processing and Machine learning
  • v1.1 Training on own data since model trained on mnist dataset did not gave acceptable results
  • v1.2 Solving on video stream
  • v1.3 Robust method for infering grid
  • v1.4 Different approach for extracting digits robust to lighting variations
  • v1.5 Able to recognize rotated sudoku's
  • v2.0 GUI game

Troubleshooting

  • Changing parameters like --process, --resize, --margin can prove to be effective for some images and models

Dilation for test_imgs/sudoku5.jpg

corners-dilation grid-dilation
Corners Dilation Infered Grid

Opening for test_imgs/sudoku5.jpg

corners-opening grid-opening
Corners Opening Infered Grid

Contributors

Resources

License

Describe your License for your project.