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>