-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rubik's Cube.py
441 lines (339 loc) · 14.4 KB
/
Rubik's Cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
# coding: utf-8
# In[67]:
def arrows(frame_height,frame_width,length):
'''
Cordinates to draw indicating arrows
frame_height : height of frame
frame_width : width of frame
length: length of arrow to be drawn
return : Dictionary of arrows for various moves
'''
center_x = frame_width//2
center_y = frame_height//2
gap = 80
u = ( center_x+length-20, center_y-gap, center_x - length+20 , center_y-gap)
u_h = (center_x - length+20 , center_y-gap, center_x+length-20,center_y-gap)
d = (center_x - length +20, center_y+gap , center_x+length-20,center_y+gap)
d_h = ( center_x+length-20,center_y+gap, center_x - length+20, center_y+gap)
l = (center_x-gap , center_y-length , center_x-gap ,center_y+length)
l_h = (center_x-gap ,center_y+length,center_x-gap , center_y-length )
r = ( center_x+gap ,center_y+length,center_x+gap , center_y-length )
r_h= (center_x+gap , center_y-length , center_x+gap ,center_y+length)
arrows = {"R":[r] , "U":[u] , "L":[l],"D":[d] ,"R'":[r_h] , "U'":[u_h] , "L'":[l_h],"D'":[d_h] , "F":[u_h,d_h,l_h,r_h],
"F'":[u,d,l,r] , "B":[u,d,l,r] , "B'":[u_h,d_h,l_h,r_h]}
return arrows
# In[68]:
def getGrid(frame_height,frame_width):
'''
Generating Co-ordinate to draw the grid
frame_height : height of frame
frame_width : width of frame
return : Dictionary of coordinate for grid
'''
center_x = frame_width//2
center_y = frame_height//2
width = 25
height = 25
gap=42
gridPos=[
[center_x-(width//2)-gap-width ,center_y-(height//2)-gap-height, center_x-(width//2)-gap , center_y-(height//2)-gap],
[center_x-width//2 , center_y-(height//2)-gap-height , center_x+width//2 , center_y-(height//2)-gap],
[center_x+(width//2)+gap , center_y-(height//2)-gap-height , center_x+(width//2)+gap+width , center_y-(height//2)-gap],
[center_x-(width//2)-gap-width , center_y-height//2 , center_x-(width//2)-gap , center_y+height//2],
[center_x-width//2 , center_y-height//2 , center_x+width//2 , center_y+height//2],
[center_x+(width//2)+gap , center_y-height//2 , center_x+(width//2)+gap+width , center_y+height//2],
[center_x-(width//2)-gap-width , center_y+(height//2)+gap , center_x-(width//2)-gap ,center_y+(height//2)+gap+height],
[center_x-width//2 , center_y+(height//2)+gap , center_x+width//2 , center_y+(height//2)+gap+height],
[center_x+(width//2)+gap , center_y+(height//2)+gap, center_x+(width//2)+gap+width , center_y+(height//2)+gap+height]
]
return gridPos
def visualCube(fixed=False):
'''
Generating Co-ordinates for visual cube
return: List of co-ordinates to draw grid
'''
if not fixed:
width = 20
height = 20
startx,starty = 20,10
else:
width = 15
height = 15
startx,starty = 20,90
grid=[]
for i in range(1,10):
grid.append( [startx,starty,startx+width,starty+height])
startx+=width
if i%3==0:
starty+=height
startx = 20
return grid
# In[69]:
def getRoi(frame,gridPos):
'''
Function to return color at each of the grid
frame: frame
gridPos: position of the 9 grids
return : color in all nine grids in form of string - Example "bbwyogrgr"
'''
color = ""
for i in range(len(gridPos)):
roi = frame[gridPos[i][1]:gridPos[i][3] , gridPos[i][0]:gridPos[i][2]]
roi = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
all_contour = []
for key,j in COLORS.items():
mask = cv2.inRange(roi,j[0],j[1])
cnts,_ = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
max_area=0
for c in cnts:
area = cv2.contourArea(c)
if area>max_area:
max_area=area
if max_area>300:
all_contour.append((key,max_area))
if len(all_contour)>0:
color_key = max(all_contour , key = lambda x: x[1])[0]
if color_key == 'r1' or color_key=='r2':color_key = 'r'
if color:color += color_key
else:color=color_key
if len(color)==9:
return color
return False
# In[70]:
def solve(cube,actual_pattern):
'''
Function to solve rubiks cube using Koceimba.
cube: Dictionary containing colors in all the faces of cube
actual_pattern: Actual color pattern of your cube
return : List containing all the moves to solve the given cube
'''
pattern = "ybrgow"
CUBE = ['' for _ in range(6)]
for key,i in cube.items():
c = ""
for j in i:
c += pattern[ actual_pattern.index(j) ]
CUBE[actual_pattern.index(key)] = c
cube = ''.join(CUBE)
for i in pattern:
x = cube.count(i)
if x!=9:
return False
try:
x = utils.solve(cube,'Kociemba')
y = []
for i in x:
i=str(i)
#if '2' in i:
# i=i[:-1]
# y.append(i)
y.append(i)
return y
except:
return False
# In[71]:
def reset():
'''
Reinitialise Diffrent variables in case some error arises
return: various temperort variables requires in main programe
'''
pattern = "ybrgow"
faces = {i:'' for i in pattern}
sub = {i:i for i in pattern}
counter = 0
previousColor = None
conf = 0
temp_pattern,actual_pattern = "",""
got_color=False
return pattern,faces,sub,counter,previousColor,conf,temp_pattern,actual_pattern,got_color
# In[72]:
#===========SOLVING STAGE=================
def stage2(cam,answer,front_face,arrow):
'''
Stage2 i.e the solving stage where various arrows are indicated
cam: reference to webcam
answer: moves to solve the cube
front_face: Color at the center of front face of the cube
arrow: Dictionary containig cordinates to draw arrow for various moves
return: None
'''
pause_time = 7 #time for next move to display
previousColor = None
conf = 0
start = False
counter = 0
previousTime = None
while True:
_,frame = cam.read()
frame=cv2.resize(frame,(frame_width,frame_height))
if counter<len(answer):
color = getRoi(frame,gridPos)
if color and not start :
if not previousColor:previousColor=color
if color == previousColor:conf+=1
elif color!=previousColor:
conf=0
previousColor=None
if conf>=15 and color[4]==front_face:
start=True
previousTime = time.time()
if start:
ans = answer[counter]
y = ans.replace('2','')
x = arrow[y]
if 'F' in ans:dir_text="Front Face"
elif 'B' in ans:dir_text="Back Face"
else:dir_text = ""
for c,i in enumerate(x):
if "U2" in ans or "D2" in ans:
cv2.arrowedLine(frame,(i[0],i[1]+17),(i[2],i[3]+17),(58,184,0),2)
if "R2" in ans or "L2" in ans:
cv2.arrowedLine(frame,(i[0]-17,i[1]),(i[2]-17,i[3]),(58,184,0),2)
if "F2" in ans or "B2" in ans:
if c<2:
cv2.arrowedLine(frame,(i[0],i[1]-20),(i[2],i[3]-20),(58,184,0),2)
else:
cv2.arrowedLine(frame,(i[0]-20,i[1]),(i[2]-20,i[3]),(58,184,0),2)
cv2.arrowedLine(frame,(i[0],i[1]),(i[2],i[3]),(58,184,0),2)
cv2.putText(frame,dir_text , (int(frame_width//2 - 55) ,80) , cv2.FONT_HERSHEY_SIMPLEX,0.8,(58,184,0),2)
cv2.putText(frame,ans , (30,40) , cv2.FONT_HERSHEY_SIMPLEX,1.2,(90,255,20),2)
if time.time()-previousTime >= pause_time:
counter+=1
previousTime = time.time()
else:
cv2.putText(frame,"Please Keep front face towards the camera and top face upwards..." , (50,50) , cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,0),2)
cv2.rectangle(frame,((frame_width//2)-15 , (frame_height//2)-15),((frame_width//2)+15 , (frame_height//2)+15) , (255,255,255),2)
cv2.imshow("a",frame)
if cv2.waitKey(5)==ord('q'):
break
# In[76]:
def stage1():
'''
Color picking stage from all the faces of the cube
return: None
'''
pattern,faces,sub,counter,previousColor,conf,temp_pattern,actual_pattern,got_color = reset()
cam=cv2.VideoCapture(0)
pt = time.time()
tt=7
while True:
if got_color:
answer = solve(faces,actual_pattern)
#print(faces,actual_pattern)
print(answer)
if not answer:
stage1()
pattern,faces,sub,counter,previousColor,conf,temp_pattern,actual_pattern,got_color = reset()
else:
stage2(cam,answer,front_face,arrow)
cam.release()
cv2.destroyAllWindows()
break
#========GO TO STAGE2 HERE==========
_,frame = cam.read()
frame=cv2.resize(frame,(frame_width,frame_height))
if time.time()-pt<=tt:
cv2.putText(frame,"Keep top face towards the camera and front face towards the bottom!",(int(frame_width//2 - 270),20),cv2.FONT_HERSHEY_SIMPLEX,0.48,(0,255,255),2)
cv2.putText(frame,"Starting in: " +str(tt - int(time.time()-pt)),(int(frame_width//2 - 150),70),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,255),2)
else:
color = getRoi(frame,gridPos)
if color :
if not previousColor:previousColor=color
if color == previousColor:conf+=1
elif color!=previousColor:
conf=0
previousColor=None
if conf>=25:
center = color[4]
faces[center] = color
if center not in temp_pattern:
temp_pattern+=center
conf = 0
previousColor = None
if list(faces.values()).count('') == 0:
for k in seq:
actual_pattern+=temp_pattern[k]
front_face = actual_pattern[2]
got_color = True
counter = len(temp_pattern)
if not got_color:
try:
cv2.putText(frame,texts[counter] , (frame_width//2 - 75,50) , cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,50,255),2)
except:
pass
else:cv2.putText(frame,"Solving,Please Wait..." , (100,50) , cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2)
for j,i in enumerate(visualGrid):
try:
color_key = color[j]
if color_key=='r':clr=(0,0,255)
elif color_key=='g':clr=(0,255,0)
elif color_key=='b':clr=(255,0,0)
elif color_key=='y':clr=(0,255,255)
elif color_key=='o':clr=(175,55,245)
elif color_key=='w':clr=(255,255,255)
else:clr=(166, 156, 162)
except:
clr = (166, 156, 162)
cv2.rectangle(frame,(i[0],i[1]),(i[2],i[3]),clr,-1)
cv2.rectangle(frame,(i[0]-1,i[1]-1),(i[2]+1,i[3]+1),(0,0,0) ,1)
for i in range(6) :
clrs = [(166, 156, 162) for _ in range(9)]
color_key = faces[pattern[i]]
if color_key!='':
clrs=[]
for j in color_key:
if j=='r':clr=(0,0,255)
elif j=='g':clr=(0,255,0)
elif j=='b':clr=(255,0,0)
elif j=='y':clr=(0,255,255)
elif j=='o':clr=(175,55,245)
elif j=='w':clr=(255,255,255)
clrs.append(clr)
#Drawing indicator here
for p,q in enumerate(fixedGrid):
cv2.rectangle(frame,(q[0],q[1]+(i*65)),(q[2],q[3]+(i*65)),clrs[p],-1)
cv2.rectangle(frame,(q[0],q[1]+(i*65)-1),(q[2],q[3]+(i*65)+1),(0,0,0),1)
cv2.putText(frame,seq_text[i],(fixedGrid[0][0],(fixedGrid[0][1]-3)+(i*65)),cv2.FONT_HERSHEY_SIMPLEX,0.35,(0,165,255),1)
for i in gridPos:
cv2.rectangle(frame,(i[0],i[1]),(i[2],i[3]),(255,255,255),2)
cv2.imshow("a",frame)
if cv2.waitKey(5)&0xFF == ord('q'):
cam.release()
cv2.destroyAllWindows()
break
return -1
# In[78]:
import cv2
import numpy as np
from rubik_solver import utils
import time
import sys
#NOTE: TRY USING GOOD LIGHTING SOURCE
#===========FEEL FREE TO CHANGE THE RANGES ACCORDING TO YOUR CUBE COLOR=========
GREEN = [np.array([50,90,30]) , np.array([85,255,255])]
BLUE = [np.array([100,90,30]) , np.array([135,255,255])]
YELLOW = [np.array([15,90,30]) , np.array([50,255,255])]
PINK = [np.array([145,90,30]) , np.array([175,255,255])]
RED1 =[np.array([0,90,30]) , np.array([12,255,255])]
RED2 = [np.array([170,90,30]) , np.array([180,255,255])]
WHITE = [np.array([0,0,35]) , np.array([255,90,255])]
#===========================================
COLORS = { 'w':WHITE,'g':GREEN,'b':BLUE,'y':YELLOW,'o':PINK , 'r1':RED1 , 'r2':RED2 } #DO NOT CAHNGE
frame_height,frame_width = 480,640
gridPos = getGrid(frame_height,frame_width)
pattern,faces,sub,counter,previousColor,conf,temp_pattern,actual_pattern,got_color = reset()
texts = ["Show Top Face" ,"Show Bottom Face" , "Show Right Face",
"Show Back Face " , "Show Left Face" ,"Show Front Face"] #045231
seq = [0,4,5,2,3,1]
seq_text = ["Top","Left","Front","Right","Back","Bottom"]
#RUBIK_SOLVER ACCEPTS COLOR BE ARRANGED ACC. TO. FOLLOWING SEQUENCE - TOP,LEFT,FRONT,RIGHT,BACK,BOTTOM
visualGrid = visualCube()
fixedGrid = visualCube(fixed=True)
arrow_length = int(0.15*frame_height)
arrow = arrows(frame_height,frame_width,arrow_length)
if __name__=="__main__":
stage1()
sys.exit()
# In[ ]:
# In[ ]: