Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Description: So i'm currently working on a program to solve a rubik's cube using Python and OpenCV. In order to do this i first am trying to extract all the colors of the cubies, and then apply kociemba's algorithm in order to output a solution to the users. Unfortunately upon running this program i am coming up with errors, as it crashes whenever i submit each of the current colors and it does not give me a reason even when i debug it. I am unsure as to why, any help would be appreciated as i really have little experience when it comes to computer vision.

Below is the code:

import numpy as np # imports numpy as np to import all the numpy modules and i can use them as np
import cv2 # import for computer vision library 
import time # import time allows you to work with time
from colorlabeler import density, cubestr
import kociemba # A computer rubiks algorithm for solving the 3x3x3 cube

def screen_record(): 
    last_time = time.time()
    cv2.startWindowThread()
    # cv2.namedWindow("preview")

    cap = cv2.VideoCapture(0) # returns video from the first webcam on your computer

    faces = "FUDLRB"# the faces/titles of the Rubiks cube
    idx = 0 

    data = { #faces/titles of each screeen, cubie of rubiks cube
        "F" : ["", "", "", "", "", "", "", "", ""],
        "U" : ["", "", "", "", "", "", "", "", ""],
        "D" : ["", "", "", "", "", "", "", "", ""],
        "L" : ["", "", "", "", "", "", "", "", ""],
        "R" : ["", "", "", "", "", "", "", "", ""],
        "B" : ["", "", "", "", "", "", "", "", ""]
    }

    while(True):
        _, img = cap.read()
        last_time = time.time()
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        colors = {
        	"red" : (0, 0, 255),
        	"blue" : (255, 0, 0),
        	"green" : (0, 255, 0),
        	"yellow" : (0, 255, 255),
        	"white" : (255, 255, 255),
        	"orange" : (0, 165, 255)
        }

        offset = 75
        z = 0
        for i in (-1, 0, 1):
            for j in (-1, 0, 1):
                px = 358 + j * offset
                py = 280 + i * offset


                maxDens = [0, "white"]
                crop = img_hsv[(py-35):(py+35), (px-35):(px+35)]
                for k in ("red", "blue", "green", "yellow", "white", "orange"):
                    d = density(crop, k)
                    if d > maxDens[0]:
                        maxDens[0] = d
                        maxDens[1] = k

                cv2.circle(img,(px, py), 5, colors[maxDens[1]], -1)
                data[faces[idx]][z] = maxDens[1][0]
                z += 1


        # lower = np.array([110, 100, 100])
        # upper = np.array([130, 255, 255])
        #
        # mask = cv2.inRange(img_hsv, lower, upper)
        # output = cv2.bitwise_and(img, img, mask = mask)

        cv2.imshow(faces[idx] + ' Face', img)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            cap.release()
            break

        if cv2.waitKey(25) & 0xFF == ord('h'):
            idx += 1
            if idx == len(faces):
                print(kociemba.solve(cubestr(data)))
                cv2.destroyAllWindows()
                cap.release()
                break

screen_record()


What I have tried:

Traceback (most recent call last):
  File "c:\Users\eonei\Documents\Emmett\Rubiks-Cube-Solver\main.py", line 81, in <module>
    screen_record()
  File "c:\Users\eonei\Documents\Emmett\Rubiks-Cube-Solver\main.py", line 76, in screen_record
    print(kociemba.solve(cubestr(data)))
  File "C:\Users\eonei\AppData\Roaming\Python\Python39\site-packages\kociemba\__init__.py", line 61, in solve
    return _solve(cubestring, patternstring, max_depth)
  File "C:\Users\eonei\AppData\Roaming\Python\Python39\site-packages\kociemba\__init__.py", line 18, in _solve
    raise ValueError('Error. Probably cubestring is invalid')
ValueError: Error. Probably cubestring is invalid
PS C:\Users\eonei\Documents\Emmett\Rubiks-Cube-Solver> 
Posted
Updated 27-Apr-22 2:50am
v2

Seriously, you need to use the debugger to find out exactly what it is doing, and where it crashes. So go here: pdb — The Python Debugger — Python 3.10.4 documentation[^] and use set_trace to start debugging from a point just before where you "know it crashes". Then step through looking closely at what is happening until it does crash. That should tell you which line it crashed on, and that should help you to identify what causes it to crash.

From there you can start working out why it crashed, but without information other than "it's something in my code" you are just whistling in the dark.

Sorry, but we can't do that for you - this is a skill that you need to develop, and the only way to develop a skill is by using it: just like riding a bicycle you don't learn how to do it by looking at what someone else did!
 
Share this answer
 
Comments
Emmett O neill 27-Apr-22 6:37am    
Sorry, i should have made it clearer/worded my question better. I have already debugged my program, i am just unable to solve the issue as well as get my program to work correctly, as the only thing it can currently do is detect colours. Thanks for the feedback though! apologies for not making it clear!
Debugging skills are as important as design and coding skills. You will learn nothing if someone debugs your program for you. It's unlikely that many people here are even amiliar with Kociemba's algorithm, so someone wanting to help would have to start by reading up on it.

I would suggest that you return to debugging and post a follow-up (by editing your original post) if you get a better idea of where your program is running into trouble. "Please debug and fix my program" is asking for a lot when a program isn't fairly trivial.
 
Share this answer
 
Look at the second message below in the error messages:
Traceback (most recent call last):
  File "c:\Users\eonei\Documents\Emmett\Rubiks-Cube-Solver\main.py", line 81, in <module>
    screen_record()
  File "c:\Users\eonei\Documents\Emmett\Rubiks-Cube-Solver\main.py", line 76, in screen_record
    print(kociemba.solve(cubestr(data)))
  File "C:\Users\eonei\AppData\Roaming\Python\Python39\site-packages\kociemba\__init__.py", line 61, in solve
    return _solve(cubestring, patternstring, max_depth)
  File "C:\Users\eonei\AppData\Roaming\Python\Python39\site-packages\kociemba\__init__.py", line 18, in _solve
    raise ValueError('Error. Probably cubestring is invalid')
ValueError: Error. Probably cubestring is invalid

Which refers to:
Python
print(kociemba.solve(cubestr(data)))

So use the debugger to find out why your data is causing the failure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900