This program is designed to take in an image of a sudoku and display the solved version of the image
- How To Run
- Technology Stack
- Image Processing
- Convolutional Neural Network
- Sudoku Algorithm
- Flask Server
pip install numpy
pip install OpenCV
pip install tensorflow
pip install flask
pip install flask_uploads flask_wtf
- Than run app.py file in terminal
- Finally go to http://127.0.0.1:5000/
Using OpenCV I processed any given sudoku puzzle, using this process
- Converting it to GrayScale
- Gaussian Blurring the Image
- Adaptive Thresholding to contrast the image
- Finding the contour to find the corners of the Sudoku
- Warped the perspective of an image to give just the Sudoku grid
- Using a created split_Boxes() method to divide the image into 81 even images
- Using CNN to detect the numbers and place them into an array
- Overlay the array onto a blank image
- Using a created drawGrid() method to draw a grid around each number
- Warp the perspective back to how the photo is originally given
- Overlay the numbers onto the original image
The training data used for this CNN is from Kaggle, it contains just over 1000 unique images for each digit for over 10,000 total images. Additionally to improve accuracy of CNN, i implemented an attempt at even distribution
The model is a classical CNN inspired by the LeNet-5 architecture developed by Yann LeCun. (More Infomation: https://www.kaggle.com/code/blurredmachine/lenet-architecture-a-complete-guide)
The model was trained over 10 epochs with a batch size of 130 images. Next time i would try and acquire more computational power as this was near the limits for my computer
- Test Accuracy = 99.2125988007%
Here are some graphs to show how the training went for my CNN
After extracting a numerical array representing the numbers from the Sudoku grid image, we employ a backtracking algorithm to solve the puzzle. This recursive approach systematically attempts to place valid numbers in empty grid cells. If the algorithm encounters a dead end or an unsolvable puzzle, it backtracks to the last empty cell and explores alternative options. This process continues until the Sudoku puzzle is successfully solved.
This Flask server is designed to facilitate Sudoku puzzle solving through image processing and a backtracking algorithm. The structure of the code is organized as follows:
-
Uploads Configuration: Utilizes Flask-Uploads to handle uploaded images, configuring the upload destination.
-
Form Definition: Defines a FlaskForm using Flask-WTF, which includes a FileField for image uploads and a SubmitField.
-
Routes:
/uploads/<filename>
: Serves uploaded images./
: Main route for handling image uploads, processing, and displaying the results.
-
Image Processing Integration: Imports image processing methods for solving Sudoku puzzles from
Image_Processing.main
and supporting methods fromImage_Processing.image_methods
. -
Flask App Configuration: Configures Flask app settings, including the secret key and template folder location.
-
App Initialization: Initializes the Flask app with the specified template folder.
-
Form and Upload Configuration: Creates an instance of the
UploadForm
and configures image uploads using Flask-Uploads. -
Route Definitions:
/uploads/<filename>
: Returns uploaded images./
: Handles image uploads, initiates processing, and renders the results on the HTML template.