forked from ParadoxZero/rubiks-cube-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
146 lines (126 loc) · 2.92 KB
/
interface.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
import numpy as np
import cv2
import pickle
import kociemba
import serial
#port = "/dev/ttyACM0"
boundary = [
[0,0], #Red
[0,0], #Blue
[0,0], #orange
[0,0], #White
[0,0], #green
[0,0], #Yellow
]
resolution = X,Y = 640 , 480
sqrs = [
((Y/6)*5,(Y/6)*3),
((Y/6)*4,(Y/6)*3),
((Y/6)*3,(Y/6)*3),
((Y/6)*5,(Y/6)*4),
((Y/6)*4,(Y/6)*4),
((Y/6)*3,(Y/6)*4),
((Y/6)*5,(Y/6)*5),
((Y/6)*4,(Y/6)*5),
((Y/6)*3,(Y/6)*5)
]
sqrWidth = X/14
color_list = []
color_name = ['U','R','F','D','L','B']
#ser = serial.Serial(port,9600)
camera = cv2.VideoCapture(0)
if not(camera.isOpened()):
camera.open()
def reset():
t = 0
print "Show :",color_name[0]
while t < 6:
frame = camera.read()[1]
frame = cv2.flip(frame,1)
high = [0,0,0]
low = [256,256,256]
for i in sqrs :
x = int(i[0])
y = int(i[1])
w = int(sqrWidth)
crop = frame[y:y+w,x:x+w]
color = cv2.mean(crop)
if(high[0] < color[0]):
high = [color[0] + 30,color[1] + 30, color[2] + 30]
if (low[0] > color[0]):
low = [color[0] - 30,color[1] - 30, color[2] - 30]
cv2.rectangle(frame,(x,y),(x+w,y+w),color,thickness=3)
cv2.imshow("Callibration",frame)
if cv2.waitKey(2) & 0xFF == ord('q'):
boundary[t] = [high,low]
t += 1
if(t<6) : print "Show :", color_name[t]
pickle.dump(boundary,open("Data","wb"))
cv2.destroyAllWindows()
while True:
try:
boundary = pickle.load(open("Data",'rb'))
break
except:
reset()
def getCube():
t = 0
side_sequence = ""
print "Scan side : ", color_name[0]
while t < 6:
data = ""
frame = camera.read()[1]
frame = cv2.flip(frame,1)
for i in sqrs :
x = int(i[0])
y = int(i[1])
w = int(sqrWidth)
crop = frame[y:y+w,x:x+w]
color = cv2.mean(crop)
color_list.append(color)
flag = 0
for [high,low] in boundary:
if(low[0]<=color[0]<=high[0]):
if(low[1]<=color[1]<=high[1]):
if(low[2]<=color[2]<=high[2]):
name = color_name[boundary.index([high,low])]
data = data + name
flag = 1
break
if flag == 1 :
cv2.circle(frame,((x+sqrWidth/2),(y+sqrWidth/2)),sqrWidth/3,(285,285,285),thickness=2)
cv2.rectangle(frame,(x,y),(x+w,y+w),color,thickness=3)
cv2.imshow("Scaniing Cube",frame)
if cv2.waitKey(2) & 0xFF == ord('q'):
if(len(data)<6):
print "Side incorrect: scan again"
continue
print data
side_sequence = side_sequence + data
t += 1
if(t<6): print "Scan side : ", color_name[t]
del color_list[:]
return side_sequence
def calibrate():
t = 0
while t < 6 :
ser.write("set value")
k = 0
while(k ==0):
k = input("press '0' to start motor " + str(t)+ " : ")
while True:
side_sequence = getCube()
print "cube : " , side_sequence
try :
solve = kociemba.solve(side_sequence)
break
except :
print "Error in cube, please re-scan"
solve = solve + " "
print solve
k = 0
calibrate()
while k == 1:
k = input("Start solving ? :")
ser.write(solve.encode('ascii','ignore'))
cv2.destroyAllWindows()