🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning

Sudoku is a classic number puzzle that challenges logical thinking. But what if we could build an AI-powered web app to solve Sudoku puzzles instantly? In this article, we’ll walk through how I built a Sudoku Solver using Flask, OpenCV, and scikit-lear…


This content originally appeared on DEV Community and was authored by Amit Chandra

Sudoku is a classic number puzzle that challenges logical thinking. But what if we could build an AI-powered web app to solve Sudoku puzzles instantly? In this article, we’ll walk through how I built a Sudoku Solver using Flask, OpenCV, and scikit-learn, and made it live on Render.

πŸš€ Live Demo

You can try out the live Sudoku Solver here:

πŸ“Œ Sudoku Solver

πŸ” How It Works

The application provides two ways to solve Sudoku puzzles:

πŸ“Έ Image Upload

  1. Upload a clear image of a Sudoku puzzle.
  2. The app extracts the Sudoku grid using OpenCV.
  3. A trained machine learning model recognizes the digits.
  4. The puzzle is solved using a backtracking algorithm.
  5. The final solution is displayed and available for download.

✏️ Manual Input

  1. Enter numbers into an interactive grid.
  2. Click "Solve" to get the completed Sudoku.
  3. Errors are handled gracefully if the puzzle is unsolvable.

πŸ› οΈ Tech Stack

  • Backend: Flask (Python)
  • Frontend: HTML, CSS, JavaScript
  • Computer Vision: OpenCV, NumPy
  • Machine Learning: scikit-learn (Digit Recognition Model)
  • Sudoku Solver Algorithm: Backtracking
  • Deployment: Render

πŸ“Œ Machine Learning Model

The app utilizes a scikit-learn model trained on digit datasets like MNIST. The key steps:

  1. Preprocessing: Images are converted to grayscale and processed with OpenCV.
  2. Feature Extraction: Digits are isolated from the Sudoku grid.
  3. Prediction: The trained model classifies digits accurately.

πŸ’‘ You can fine-tune the model by retraining it with train_model.py in the project directory.

πŸ—οΈ Project Architecture

/sudoku-solver
β”œβ”€β”€ /static
β”‚   β”œβ”€β”€ /css (Styles)
β”‚   β”œβ”€β”€ /images (Sample images)
β”‚   β”œβ”€β”€ /js (Frontend scripts)
β”œβ”€β”€ /templates
β”‚   β”œβ”€β”€ index.html (Main UI)
β”œβ”€β”€ /utils
β”‚   β”œβ”€β”€ image_processor.py (Handles image extraction)
β”‚   β”œβ”€β”€ solver.py (Solves Sudoku with backtracking)
β”œβ”€β”€ /model
β”‚   β”œβ”€β”€ train_model.py (Trains the digit recognition model)
β”œβ”€β”€ app.py (Flask application)
└── README.md

πŸš€ Running the Project Locally

  1. Clone the repository (if you have access):
   git clone your-repo-link
   cd sudoku-solver
  1. Set up a virtual environment (optional but recommended):
   python -m venv venv
   source venv/bin/activate  # Windows: venv\Scripts\activate
  1. Install dependencies:
   pip install -r requirements.txt
  1. Run the application:
   python app.py
  1. Open in your browser:
http://127.0.0.1:5000

🎯 Future Enhancements

βœ… Improve OCR with deep learning models like CNNs.

βœ… Add a difficulty classifier for Sudoku puzzles.

βœ… Implement a mobile-friendly UI for better accessibility.

πŸ”— Follow Me for More!

If you found this project useful, follow me for more AI & Python content:

πŸ“Œ GitHub: https://github.com/Amit-Chandra
πŸ“Œ LinkedIn: https://www.linkedin.com/in/connect-amit-chandra/
πŸ“Œ Twitter/X: https://x.com/CodeByAmit

🎯 Home Screen

Image description

πŸ” Solving an Uploaded Puzzle

Image description

✏️ Solve By Manual Input The Sudoku Problem

Image description

πŸš€ Conclusion

This Flask-based Sudoku Solver showcases the power of machine learning and computer vision in solving real-world problems. Whether you're an AI enthusiast or a Python developer, building such projects enhances your skills significantly.

Want more AI-powered projects? Stay connected! ✨


This content originally appeared on DEV Community and was authored by Amit Chandra


Print Share Comment Cite Upload Translate Updates
APA

Amit Chandra | Sciencx (2025-02-01T20:25:04+00:00) 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning. Retrieved from https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/

MLA
" » 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning." Amit Chandra | Sciencx - Saturday February 1, 2025, https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/
HARVARD
Amit Chandra | Sciencx Saturday February 1, 2025 » 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning., viewed ,<https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/>
VANCOUVER
Amit Chandra | Sciencx - » 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/
CHICAGO
" » 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning." Amit Chandra | Sciencx - Accessed . https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/
IEEE
" » 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning." Amit Chandra | Sciencx [Online]. Available: https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/. [Accessed: ]
rf:citation
» 🧩 Building a Web-Based Sudoku Solver with Flask & Machine Learning | Amit Chandra | Sciencx | https://www.scien.cx/2025/02/01/%f0%9f%a7%a9-building-a-web-based-sudoku-solver-with-flask-machine-learning/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.