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
Enter fullscreen mode Exit fullscreen mode

🚀 Running the Project Locally

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

🎯 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

🔍 Solving an Uploaded Puzzle

✏️ Solve By Manual Input The Sudoku Problem


🚀 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! ✨