A program that reads an input file with a sudoku and returns a solved one.
This project was inspired by an article on TowardsDataScience. However, this is my own approach. I'm using or-tools as a 'constraint programming device' - very quick solution with little amount of code. Simple CpModel and CpSolver are enough, the solver itself contains just the 3 obligatory constraints:
- different numbers in rows
- different numbers in columns
- different numbers in cells.
In order to read images, MNIST dataset is used to train.
From that, an algorithm with proper readings of hough lines is applied (with cv2). Finally, previously mentioned solver will take care of the rest.
The code can be divided into 5 sections:
- Training Reader - reads the MNIST Dataset.
- Solver - OR-Tools sovler.
- Model - model (compiling and fitting), confusion matrix and metrics.
- Image Reader - reads image and lines.
- Digit Prediction - reads digits from each cell and builds a sudoku.
The program is compiled in that order.
Current version of the code contains image example.png (). After building the model we can see it's summary in jupyter and the confusion matrix. As can be seen, it has a nice, slightly over 99% accuracy.
From there we go into image reading and digit predicting. The image with drawn lines should be displayed, and logs written in console.
In order to make sure that sudoku is feasible and properly read, probabilities are being saved in DigitPrediction class. A question about sudoku is asked to a user. If the data displayed is correct, 'yes' should be written.
If the sudoku is not correct - a simple 'no' answer would get us into handler. For each digit with probability smaller than 70% a question is asked.
Finally, a result for a sudoku is displayed.