Skip to content

A computer vision pipeline that detects Sudoku puzzles in photos and solves them.

Notifications You must be signed in to change notification settings

Paul-F7/cv-sudokusolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CV Sudoku Solver

Python PyTorch OpenCV YOLO

Photograph a Sudoku puzzle and get the solved grid as an image.

Input

Output

Model Performance

Grid Detection

  • 99.5% mAP — detection accuracy across varying conditions
  • 99.9% precision — minimal false positives
  • 100% recall — never misses a grid

Digit Recognition

  • 99% accuracy — correct digit on first prediction
  • 100% top-5 — correct answer always in top 5 predictions

Robustness

  • Printed and handwritten puzzles
  • Varying lighting and exposure
  • Skewed camera angles
  • Any paper color or background

Models

Grid Detection Model

YOLOv8 Nano object detection trained on 500+ images from Roboflow.

Training Results

Training Curves

Results

Precision-Recall Curve

PR Curve

Confusion Matrix

Confusion Matrix

Validation Predictions

Validation


Digit Classification Model

YOLOv8 Nano classifier trained on 4,400+ cell images to recognize digits 1-9 and empty cells.

Training Results

Training Curves

Results

Confusion Matrix

Confusion Matrix

Validation Samples


How It Works

  1. Detect — YOLOv8 locates the Sudoku grid in your photo
  2. Warp — OpenCV corrects perspective to create a perfect square
  3. Split — Grid is divided into 81 individual cells
  4. Recognize — YOLOv8 classifier identifies each digit
  5. Solve — Constraint propagation + backtracking finds the solution
  6. Render — Solution is drawn onto a clean grid image

Solving Algorithm

Narrows down options dynamically before solving via depth-first search. Dynamic approach, narrow down options, then depth-first search

  1. Constraint Propagation — Eliminates impossible values by checking rows, columns, and 3x3 blocks. Solves most puzzles without guessing.

  2. Backtracking(DFS) — For harder puzzles, recursively tries possibilities and backtracks on invalid states. Guarantees a solution if one exists.

Solves any valid Sudoku in under 100ms.


Project Structure

backend/
├── sudoku_detector/     # Computer vision pipeline
│   ├── services/        # Detection, warping, recognition
│   ├── training/        # Model training scripts
│   └── models/          # Trained weights (.pt files)
├── sudoku_solver/       # Solving algorithms
└── sudoku_display/      # Solution rendering

Getting Started

# Clone the repo
git clone https://github.com/yourusername/cv-sudokusolver.git
cd cv-sudokusolver

# Install dependencies
pip install -r requirements.txt

# Run on an image
cd backend
python -c "import cv2; from main import solve_sudoku_image; solve_sudoku_image(cv2.imread('path/to/sudoku.jpg'))"

Output is saved to solved_sudoku.jpg.

About

A computer vision pipeline that detects Sudoku puzzles in photos and solves them.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages