Sudoku is one of the most popular puzzle of all time.The goal of Sudoku is to fill 9x9 grid such that each row,each column and 3x3 grid contains all of the digits between 1 to 9. In this project we aim to create a real time Sudoku solver which recognise the elements of Sudoku puzzle and providing a digital solution using Computer vision.
- Grab the Sudoko Grid from the Webcam Image
- Extract and Detect the Digits
- Solve the puzzel and Print the Solution
(Click on the points to expand)
Extracting the Contour with biggest area
- Convert image to Gray scale (cv2.cvtColor)
- Blur the image using Gaussian Blur (cv2.GaussianBlur)
- Apply adaptive thresholding (cv2.adaptiveThreshold)
- Extract the contour with biggest area (cv2.contourArea)
Extract Sudoku Grid
Use cv2.warpPerspective to get stable Sudoku Grid(Click on the points to expand)
Process the Extracted Grid
Use cv2.morphologyEx and Invert the imageGet Individual Cells of the grid and Predict the Digit
Model used for prediction has been trained on subset of Chars4K Dataset which contains digits only (0-9). Model can be viewed here
While predicting the digits, blank cells would be predicted as 0. Blank cells can be identified by calculating sum of all the pixels (sum would be large)
(Click on the points to expand)
Solution and Final Result
To solve Sudoko, backtracking has been used. Sudoku Backtracking is a recursive algorithm which goes through each cells and sequentially assigns numbers from 1 to 9 if the cell is empty. Notebook for the same can be viewed here.
To display the result use cv2.putText.