Skip to content
All writing

Post by

How I Built a Rubik's Cube Solver That Uses Your Camera

Published · 4 min read

Cube Solver is a free Rubik's cube solver by Niranjan D: show your 3x3 to a phone or webcam, then follow 3D arrows. How its camera scanner reads every colour.

Cube Solver: a scanned Rubik's cube, a 3D cube with a turn arrow, and the current turn explained

Most people who own a Rubik's cube have never solved it. It sits on a shelf, scrambled, because the usual help assumes too much. Online solvers ask you to type in all 54 stickers, then answer in a code like R U R' U' that means nothing if you have never seen it.

Cube Solver is the free web app I built for that person. You hold your cube up to your phone camera or laptop webcam, side by side, and it reads the colours on its own. Then it shows you every turn on a 3D cube with an arrow, until the cube in your hands is solved. I'm Niranjan D, a full-stack developer and Artificial Intelligence & Data Science student in Chennai, and this post covers what the app does and how the camera part works.

What it does

  • Scans with your camera. Show it any side, in any order, held any way up. There is no button to press: a side is captured the moment the reading holds steady.
  • Checks the cube is possible before solving. If a sticker was misread, it says so in plain words, such as "one corner is twisted in place", and you can fix that sticker by hand.
  • Solves in about 20 moves, never more than 22. You can also choose a beginner layer-by-layer solution or CFOP, the method speedcubers use.
  • Shows every turn in 3D. The layer about to move is outlined, an arrow curls round it, and the turn is written out in words. It can read each move aloud, so both hands stay on the cube.
  • Needs nothing installed. It runs in the browser on Android, iPhone, Windows, Mac and Linux, with no account. No webcam? Upload photos of the six sides, or tap the colours in.

Try it at cube.niranjand.in.

How the camera reads your cube

Reading a cube sounds easy: point a camera at it and look at nine squares. In practice the light lies. A warm bulb makes red look orange, a bright window turns white into pale blue, and a shadow across one corner changes every colour it touches.

Early versions sent several camera frames a second to the server. That was slow everywhere and very slow on mobile data. Now the reading happens in your browser, about ten times a second, and nothing leaves your device until all six sides are in.

Is that even a cube?

Nine flat patches are not enough, because a desk or a wall is flat too. The scanner also looks for the dark lines between stickers, and checks that the stickers are bright enough. Black has no colour, so a covered lens would otherwise read as nine white squares.

Which side is it?

The centre sticker names the side, because centres never move on a real cube. Colours are compared as ratios rather than raw values, because a shadow makes a sticker darker without changing those ratios much. Once a colour has been seen, a new centre can only be one of the colours still missing.

Red and orange

These are the pair cameras mix up most, so they get special care:

  • The camera's brightness curve is undone first, which roughly doubles the gap between red and orange.
  • The side names are decided together, not one at a time, and re-checked after every capture. A side briefly called "orange" is renamed as soon as the real orange side turns up.
  • Every one of the cube's 20 pieces must appear exactly once. Misreading one red sticker as orange still produces a real piece, but a duplicate one, so this check catches it.

Which way up was each side?

You can hold each side any way round. Once all six are in, the app tries all 4,096 combinations of side rotations and keeps the one in which all 20 pieces are real pieces. The right answer stands out clearly.

In tests with harsh warm light and heavy over-exposure, cubes with a red and orange mix-up fell from 19% to 0.5%, and fully correct cubes rose from 72.5% to 99%.

How it solves

The default mode races two solvers. The browser solves the cube itself in about 10 milliseconds with a two-phase algorithm and shows that answer straight away. Meanwhile the server spends up to a second looking for something shorter, and the app keeps whichever is shorter. Every answer is replayed on the browser's own cube model before you see it, so a wrong solution can never reach the screen.

Fewer turns means fewer chances to go wrong with a real cube in your hands. That is why the default aims for the shortest solution rather than the one easiest to remember.

Want to solve it yourself?

The app also has a step-by-step guide for learning to solve the cube without help. It starts with the beginner method, then covers CFOP, Roux and ZZ, and how to start speedcubing, with every move playable on a 3D cube. When you are ready to time yourself, the speedcubing timer has scrambles and Ao5 and Ao12 averages.

Built with

The browser side is plain JavaScript with Three.js for the 3D cube and no build step. The server is Python with FastAPI, OpenCV and NumPy for reading photos. The whole thing is free to use.

If your cube has been sitting scrambled on a shelf, open Cube Solver, show it each side, and follow the arrow.

Topics

  • Cube Solver
  • Rubik's cube
  • Computer vision
  • JavaScript
  • Python
Next postHow I Built Helm, a Secure Android Remote for Windows PCs