Skip to content

A Rubik's cube Solver that takes cube colors through webcam and prints out the solution moves

Notifications You must be signed in to change notification settings

Varun221/rubiks_camsolver

Repository files navigation

Rubiks_Camsolver

A Rubiks Cube Solver that extracts colors on the face through webcam and prints out the solution moves (working here). The Algorithm used to solve the rubiks cube is Kociemba Algorithm (read here).

Working

The Face of the cube is detected by Tiny YOLOv3 model trained on custom data prepared by Phone captures. The images were then labeled using labelImg and the annotations were generated.
A Tiny YOLOv3 model was then trained using this Awesome Repo, TrainYourOwnYOLO. The training process and explanation can be found in the notebook given above. After the training the weights file was generated (you can find it here), it was used along with the inference code in this repo to detect the faces of the cube in a video.
Once the cube face is detected, the box image is cropped and the colors of the faces are found. The colors of the faces are then used to generate Cube string which denotes the positions of the pieces of the cube.
To solve the cube, I used the library rubiks_solver which generates solutions for the cube in various methods.

Detecting the Face colors

The bounding box generated from tiny YOLOv3 was cropped to get a tight crop of the cube face. The face was then divided into 9 patches. From each patch I generated 9 sample pixels, the color of these pixels was found using its "Distance" from the predefined colors of the cube. Once the colors of the Pixels are found, the final color of the patch is decided through voting. This same process is applied on the 9 patches of a face and the colors of the face are found.

Usage

  1. If you want to use this project. I recommend using a virtual environment. The process can be found here.
  2. Once you have activated the virtual environment, Clone this repository, extract it if you downloaded a .zip or .tar file and cd into the cloned repository.
cd C:\rubiks_camsolver 
  1. Install the Requirements file
pip install -r requirements.txt
  1. Download the weights file from here and put it inside " detect_data " folder.
  2. Run the detector
python Detector.py
  1. Ignore the Deprecation warnings. You may not find a good color detection because of lighting conditions. To tune the detection values, take a screenshot of the video feed containing your cube, name it "Screenshot".
  2. Then run the color_tuner.py script. This opens up your screenshot. Click on any one color of the cube for example, yellow, keep clicking on various points of the yellow color, you can see that various values are printed on the terminal. These are the BGR values of the yellow in the image.
  3. Adjust the values in the face_detect.py script in the color dictionary. Do so for each color.
  4. Once done, you will have a working Rubiks cube solver.

References

YOLO TRAINING

  1. https://github.com/AntonMu/TrainYourOwnYOLO
  2. https://github.com/zzh8829/yolov3-tf2

RUBIKS CUBE SOLVING

  1. https://github.com/Wiston999/python-rubik
  2. https://github.com/mazenkurdi/rubiks-cube-solver
  3. https://github.com/pglass/cube

Releases

No releases published

Packages

No packages published