Skip to content

Instantly share code, notes, and snippets.

@ymduu
Created December 11, 2017 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymduu/c122ba7a54f069902c76a10b983855aa to your computer and use it in GitHub Desktop.
Save ymduu/c122ba7a54f069902c76a10b983855aa to your computer and use it in GitHub Desktop.
#coding:utf-8
import sys
import numpy as np
import cv2
import kociemba
import copy
import urllib
import commands
import random
stage = sys.argv[1]
for cou in range(50):
namelist = ["U","D","L","R","F","B"]
#02c286df1bbd7923d1f7
url_base = "http://qubicrube.pwn.seccon.jp:33654/images/"+stage+"_"
for i in namelist:
url = url_base+i+".png"
name = url.split("/")[-1]
print url,name
urllib.urlretrieve(url,"{0}".format(name))
#img[y,x]
#雑にy=0で横だけ見る、ダメならなおす
def getBgColor(img):
height, width, channels = img.shape
for i in range(height):
color = img[0, i]
black = np.array([0,0,0])
if np.array_equal(black,color):
continue
else:
return color
def getEachPanels(img):
#https://github.com/muodov/kociemba
#において、一面の画像を[1,2,3...9]の形で返します
height, width, channels = img.shape
hdiv3 = height/3
wdiv3 = width/3
ret = []
for j in range(3):
for i in range(3):
#sudo print j,i
clp = img[j*wdiv3:(j+1)*wdiv3, i*hdiv3:(i+1)*hdiv3]
ret.append(clp)
return ret
def color2face(color, table):
if np.array_equal(color,table["B"]):
return "B"
elif np.array_equal(color,table["D"]):
return "D"
elif np.array_equal(color,table["F"]):
return "F"
elif np.array_equal(color,table["L"]):
return "L"
elif np.array_equal(color,table["R"]):
return "R"
elif np.array_equal(color,table["U"]):
return "U"
else:
return "----BAD----"
"""
B[ 0 213 255]
D[186 81 0]
F[ 96 158 0]
L[ 0 88 255]
R[ 58 30 196]
U[255 255 255]
"""
B = cv2.imread(stage+"_B.png")
D = cv2.imread(stage+"_D.png")
F = cv2.imread(stage+"_F.png")
L = cv2.imread(stage+"_L.png")
R = cv2.imread(stage+"_R.png")
U = cv2.imread(stage+"_U.png")
cube = {"U":getEachPanels(U), "R":getEachPanels(R), "F":getEachPanels(F), "D":getEachPanels(D), "L":getEachPanels(L), "B":getEachPanels(B) }
color_B = getBgColor( cube["B"][4])
color_D = getBgColor( cube["D"][4])
color_F = getBgColor( cube["F"][4])
color_L = getBgColor( cube["L"][4])
color_R = getBgColor( cube["R"][4])
color_U = getBgColor( cube["U"][4])
#ライブラリはURFDLBの順番なのでその順に展開
ls = [U,R,F,D,L,B]
colors = {"U":color_U,"R":color_R,"F":color_F,"D":color_D,"L":color_L,"B":color_B}
input_str = ""
for face in ls:
cells = getEachPanels(face)
for cell in cells:
bgcolor = getBgColor(cell)
input_str+=color2face(bgcolor,colors)
print input_str
res = kociemba.solve(input_str)
print res
def rotImg(img_src, deg):
if deg == 90:
ret = cv2.flip(img_src.swapaxes(1,0), 0)
#print "ret",ret.shape ,"def",img_src.shape
return ret
if deg == -90:
ret = cv2.flip(img_src.swapaxes(1,0), 1)
return ret
if deg == 180:
ret = cv2.flip(img_src, 1)
size = tuple([img_src.shape[1], img_src.shape[0]])
# 画像の中心位置(x, y)
#center = tuple([int(size[0]/2), int(size[1]/2)])
center = tuple([size[0]/2, size[1]/2])
# 回転させたい角度(正の値は反時計回り)
angle = deg
# 拡大比率
scale = 1.0
# 回転変換行列の算出
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
# アフィン変換
img_rot = cv2.warpAffine(img_src, rotation_matrix, size, flags=cv2.INTER_CUBIC)
return img_rot
def list2Img(v):
#画像連結
#print v[0].shape, v[1].shape, v[2].shape
r1 = np.concatenate((v[0],v[1],v[2]), axis=1)
r2 = np.concatenate((v[3],v[4],v[5]), axis=1)
r3 = np.concatenate((v[6],v[7],v[8]), axis=1)
res = np.concatenate((r1,r2,r3), axis=0)
return res
def doProc(proc):
global cube
tmp = []
tmp2 = []
if proc == "L\'":
#F左->U左
tmp = copy.deepcopy([cube["U"][0],cube["U"][3],cube["U"][6]])
cube["U"][0] = cube["F"][0]
cube["U"][3] = cube["F"][3]
cube["U"][6] = cube["F"][6]
#U左->B右
tmp2 = copy.deepcopy([cube["B"][2],cube["B"][5],cube["B"][8]])
#0:x軸反転 1:y軸反転 -1:xy反転
cube["B"][2] = cv2.flip(tmp[2], -1)
cube["B"][5] = cv2.flip(tmp[1], -1)
cube["B"][8] = cv2.flip(tmp[0], -1)
#B右->D左
tmp = copy.deepcopy([cube["D"][0],cube["D"][3],cube["D"][6]])
cube["D"][0] = cv2.flip(tmp2[2], -1)
cube["D"][3] = cv2.flip(tmp2[1], -1)
cube["D"][6] = cv2.flip(tmp2[0], -1)
#D左->F左
cube["F"][0] = tmp[0]
cube["F"][3] = tmp[1]
cube["F"][6] = tmp[2]
#左を回転
tmpimg = list2Img(cube["L"])
rotated = rotImg(tmpimg, 90)
cube["L"] = getEachPanels(rotated)
return
if proc == "R\'":
#U右->F右
tmp = copy.deepcopy([cube["F"][2],cube["F"][5],cube["F"][8]])
cube["F"][2] = cube["U"][2]
cube["F"][5] = cube["U"][5]
cube["F"][8] = cube["U"][8]
#F右->D右
tmp2 = copy.deepcopy([cube["D"][2],cube["D"][5],cube["D"][8]])
cube["D"][2] = tmp[0]
cube["D"][5] = tmp[1]
cube["D"][8] = tmp[2]
#D右->B左
tmp = copy.deepcopy([cube["B"][0],cube["B"][3],cube["B"][6]])
cube["B"][0] = cv2.flip(tmp2[2], -1)
cube["B"][3] = cv2.flip(tmp2[1], -1)
cube["B"][6] = cv2.flip(tmp2[0], -1)
#B左->U右
cube["U"][2] = cv2.flip(tmp[2], -1)
cube["U"][5] = cv2.flip(tmp[1], -1)
cube["U"][8] = cv2.flip(tmp[0], -1)
#右を回転
tmpimg = list2Img(cube["R"])
rotated = rotImg(tmpimg, 90)
cube["R"] = getEachPanels(rotated)
return
if proc == "U\'":
#F上->R上
tmp = copy.deepcopy([cube["R"][0],cube["R"][1],cube["R"][2]])
cube["R"][0] = cube["F"][0]
cube["R"][1] = cube["F"][1]
cube["R"][2] = cube["F"][2]
#R上->B上
tmp2 = copy.deepcopy([cube["B"][0],cube["B"][1],cube["B"][2]])
cube["B"][0] = tmp[0]
cube["B"][1] = tmp[1]
cube["B"][2] = tmp[2]
#B上->L上
tmp = copy.deepcopy([cube["L"][0],cube["L"][1],cube["L"][2]])
cube["L"][0] = tmp2[0]
cube["L"][1] = tmp2[1]
cube["L"][2] = tmp2[2]
#L上->F上
cube["F"][0] = tmp[0]
cube["F"][1] = tmp[1]
cube["F"][2] = tmp[2]
#上を回転
tmpimg = list2Img(cube["U"])
rotated = rotImg(tmpimg, 90)
cube["U"] = getEachPanels(rotated)
return
if proc == "D\'":
#F下->L下
tmp = copy.deepcopy([cube["L"][6],cube["L"][7],cube["L"][8]])
cube["L"][6] = cube["F"][6]
cube["L"][7] = cube["F"][7]
cube["L"][8] = cube["F"][8]
#L下->B下
tmp2 = copy.deepcopy([cube["B"][6],cube["B"][7],cube["B"][8]])
cube["B"][6] = tmp[0]
cube["B"][7] = tmp[1]
cube["B"][8] = tmp[2]
#B下->R下
tmp = copy.deepcopy([cube["R"][6],cube["R"][7],cube["R"][8]])
cube["R"][6] = tmp2[0]
cube["R"][7] = tmp2[1]
cube["R"][8] = tmp2[2]
#R下->F下
cube["F"][6] = tmp[0]
cube["F"][7] = tmp[1]
cube["F"][8] = tmp[2]
#下を回転
tmpimg = list2Img(cube["D"])
rotated = rotImg(tmpimg, 90)
cube["D"] = getEachPanels(rotated)
return
if proc == "B\'":
#L->U
tmp = copy.deepcopy([cube["U"][0],cube["U"][1],cube["U"][2]])
cube["U"][2] = rotImg(cube["L"][0] , -90)
cube["U"][1] = rotImg(cube["L"][3] , -90)
cube["U"][0] = rotImg(cube["L"][6] , -90)
#L->D
tmp2 = copy.deepcopy([cube["R"][2],cube["R"][5],cube["R"][8]])
cube["R"][2] = rotImg( tmp[0] , -90)
cube["R"][5] = rotImg( tmp[1] , -90)
cube["R"][8] = rotImg( tmp[2] , -90)
#R->D
tmp = copy.deepcopy([cube["D"][6],cube["D"][7],cube["D"][8]])
cube["D"][8] = rotImg( tmp2[0], -90)
cube["D"][7] = rotImg( tmp2[1], -90)
cube["D"][6] = rotImg( tmp2[2], -90)
#D->L
cube["L"][0] = rotImg( tmp[0], -90)
cube["L"][3] = rotImg( tmp[1], -90)
cube["L"][6] = rotImg( tmp[2], -90)
#Bを回転
tmpimg = list2Img(cube["B"])
rotated = rotImg(tmpimg, 90)
cube["B"] = getEachPanels(rotated)
return
if proc == "F\'":
#0:x軸反転 1:y軸反転 -1:xy反転
#U下->L右
tmp = copy.deepcopy([cube["L"][2],cube["L"][5],cube["L"][8]])
cube["L"][2] = rotImg(cube["U"][8], 90)
cube["L"][5] = rotImg(cube["U"][7], 90)
cube["L"][8] = rotImg(cube["U"][6], 90)
#L右->D上
tmp2 = copy.deepcopy([cube["D"][0],cube["D"][1],cube["D"][2]])
cube["D"][0] = rotImg(tmp[0], 90)
cube["D"][1] = rotImg(tmp[1], 90)
cube["D"][2] = rotImg(tmp[2], 90)
#D上->R左
tmp = copy.deepcopy([cube["R"][0],cube["R"][3],cube["R"][6]])
cube["R"][0] = rotImg(tmp2[2], 90)
cube["R"][3] = rotImg(tmp2[1], 90)
cube["R"][6] = rotImg(tmp2[0], 90)
#R左->U下
cube["U"][6] = rotImg(tmp[0], 90)
cube["U"][7] = rotImg(tmp[1], 90)
cube["U"][8] = rotImg(tmp[2], 90)
#Bを回転
tmpimg = list2Img(cube["F"])
rotated = rotImg(tmpimg, 90)
cube["F"] = getEachPanels(rotated)
return
if len(proc) == 1:
proc+="\'"
doProc(proc)
doProc(proc)
doProc(proc)
return
if proc[1] == "2":
doProc(proc[0])
doProc(proc[0])
return
procList = res.split()
for p in procList:
doProc(p)
for k,v in cube.items():
#print k,v
for deg in range(-1,4):
tmp = copy.deepcopy(v)
tmp[4] = rotImg(tmp[4], 90*deg)
res = list2Img(tmp)
cv2.imwrite(k+".png", res)
ls = commands.getoutput("zbarimg "+k+".png").split("\n")
#print ls
st = commands.getoutput("zbarimg "+k+".png").split("\n")[0]
if "scanned 0 barcode" in st:
pass
else:
commands.getoutput("cp ./"+k+".png"+" ./correct/"+k+".png")
if "http" in st:
stage = st.split("/")[-1]
print st
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment