Projects

Sudoku

An ad-free iOS sudoku game that scans a paper puzzle with the camera, reads the digits on the phone and lets you play, solve or get a hint.

I have always loved solving sudokus. I started with the ones in the newspaper, and later moved to the sudoku.com app. But I didn't want to wait for the next newspaper to check my results, and I love the authenticity of solving them on paper. So I built an app that lets me do both: solve on paper, scan the puzzle, and check it right away. No ads, and it works offline.

It started as a school project, before agentic coding existed. I came back to it in the age of AI to improve it and build a React Native app around it.

The scan

Point the phone at a puzzle. An outline snaps onto the grid and, once it holds still for a second, the app takes the photo. About half a second later the digits are on screen, ready to correct and play.

How it reads a page

Two stages. A cheap one runs on every camera frame and only looks for the four corners of the grid, so the overlay can follow the page. The expensive one runs once, on the photo: it finds the corners again, flattens the grid into a square, cuts it into 81 cells and sends them all through a small neural network.

From photo to board, on a real newspaper page with printed and handwritten digits.

Finding the grid

The same OpenCV detector runs on the live frames and on the photo. The image is scaled down, turned to grayscale and blurred a little. Then comes an adaptive threshold: every pixel is compared with the pixels around it, not with one fixed brightness for the whole picture. That keeps the grid lines white when half of the page is in shadow. The detector then looks for contours and simplifies each one into a polygon. The outline of a sudoku is a big shape with exactly four corners, so that is what it keeps.

A page can hold several sudokus, so the app picks the one nearest the centre of the view and sticks to it until another one is clearly nearer. You choose by aiming. The corners are smoothed over a few frames so the outline does not jitter. When the same grid has stayed put for a second, the app takes the photo.

The phone moves between lock-on and photo, so the corners are found again in the photo. The live outline only decides which grid it was. For an image from the photo library the search goes on inside the outline it finds, so a picture of a whole page ends up at the grid and not at the page.

Cutting and preparing the cells

The four corners define a perspective transform that turns the tilted photo into a flat square of 576 by 576 pixels. That square is cut into 81 cells of 64 by 64 pixels.

I leave the grid lines in the cells on purpose. The first model cropped them away, but a handwritten digit can reach the border of its cell. The crop cut off the bottom stroke of some 2s, which the model then read as 9s. With the full cell, the errors on three test scans went from seven to four.

Each cell then becomes a 32 by 32 input: grayscale, shrunk by averaging blocks of 2 by 2 pixels, and inverted so that ink is near 1 and paper near 0. The brightness stays absolute, it is never stretched per cell. The training code in Python and the app in TypeScript each implement these steps, and both are tested against the same reference images. A small difference, like inverted colours or another crop, does not crash anything. It just quietly makes every scan a bit worse.

The grid is flattened as one piece, so a strongly curved book page still bends the cells at the edges. Flattening each cell from its own four corners is something I want to try next.

The model

A convolutional network with two outputs per cell: which digit (or empty), and whether it is printed, handwritten or a pencil note. It is trained in PyTorch on labelled real cells plus synthetic ones, then exported to TensorFlow Lite so it runs on the phone, with no network.

It gets 99.4% of the digits right on 5,711 real test cells. The whole scan, from lock-on to result, takes about 0.5 seconds on an iPhone 17 Pro.

Before you can play, the printed digits must have exactly one solution. If they do not, a digit was probably misread, and the app marks the cells to check.

Collecting the data

A model that reads paper needs a lot of real cells. I have more than 33,000, cut from photos of sudokus: pictures of newspapers and books, photos from public and Creative Commons sets, and scans from the app itself, which only leave the phone if you switch on "Help improve the app". About 9,800 cells are empty. Of the rest, around 16,000 are printed digits and 7,500 handwritten. The model also trains on synthetic digits rendered from fonts.

Every cell needs two labels: the digit (or empty), and the kind (printed or handwritten). Typing 33,000 of those by hand would take forever, so I built a labelling tool that does the guessing.

A scan from the app goes through this loop:

Phone on the device

  1. Scan and predict. The model reads the 81 cells.
  2. Correct. I fix what it got wrong and play. Every fix is a label.
  3. Queue. Only with “Help improve the app” on. The grid photo goes in a local queue with the model’s answers, my final grid and which cells I changed.

AWS private bucket

  1. Upload. When the phone is online, it asks the API for a link and sends the photo to S3. A failed upload never blocks scanning.
  2. Wait. The scan stays marked as pending until I fetch it.

Laptop labelling tool

  1. Fetch. New scans land in the dataset.
  2. Check. Outline first, then digits, then kind. Doubtful cells first.
  3. Verify. Only cells a person confirmed are used for training.
  4. Retrain. PyTorch, then export to TensorFlow Lite.

The new model goes into the next version of the app. It also pre-labels the next batch, so there is less to fix each round.

From a scan on the phone to a better model. A failed upload never blocks scanning, and only cells I confirmed are used for training.

Labelling with the model's help

The tool is a small local web app. A photo goes through three steps:

  1. Outline. The detector proposes the grid outline. I look at the most doubtful ones first, drag the corners or reject the photo. This comes first because a wrong outline makes all 81 labels of that photo wrong.
  2. Digits. The photo is cut into cells and the current model guesses each one. The cells are laid out in one gallery per digit, so all the 7s sit together. A 1, a 9 or a smudge in a pile of 7s is visible at a glance. I click the wrong ones, press the right digit once and press Enter for the page.
  3. Kind. The same screen once more, to mark each digit as printed or handwritten. A handwritten 7 among the printed ones stands out the same way.

I did not start from zero. A small model, trained on cells I had labelled by hand back in college, did the first pre-labelling, and that already sped things up a lot. Every retrained model pre-labels the next batch better, so there is less to fix each round.

Inside each gallery the cells are sorted by how much the model doubts them. The doubt score adds up a low probability from the model, a model that reads something else than the label, a label that was changed, a digit twice in a row, column or box, and printed digits that have more than one solution. The edge of each tile runs from red to green, so the wrong cells collect at the front, in red, and a page that is all green can be approved in one go. A button confirms everything the model is at least 98% sure of (95% for the kind), and only cells that I confirmed go into training.

The Digits step: every cell the model reads as a 7, in one gallery. A green edge means the model is sure, and the doubtful cells would come first, in red. Cells from the Wicht sudoku dataset (CC BY 4.0).

The rest

Notes, undo, mistake highlighting, a timer, and hints that explain the technique, not just the answer. An account and sync on AWS keep the history across devices.

Built with