Python Recursive maze solver never backtracks all the way back - python

I have a recursive maze solver with one image (PNG, 200x200) I have tried using try/except to print an exception but nothing prints.
Recursive function
def solveRecursiveMaze(arr,x,y):
successful = False
if (x,y) == getExitPoint():
successful = True
elif isValid(arr,x,y):
arr[x][y] = "V" #set to V to show it's a visited path
successful = solveRecursiveMaze(arr, x-1, y)
if not successful:
successful = solveRecursiveMaze(arr, x, y+1)
if not successful:
successful = solveRecursiveMaze(arr, x+1, y)
if not successful:
successful = solveRecursiveMaze(arr, x, y-1)
if successful:
arr[x][y] = "P" #Mark as P to show it's a valid pa
return successful
isValid checks to see if the 2D array has a "W" (for White pixel) at the position passed in, and it is within bounds of the array
def isValid(arr,x,y):
if x < len(arr) and y < len(arr) and x >= 0 and y >= 0:
if arr[x][y] == "W":
return True
return False
getExitPoint returns an x,y (pixel) of the point that is found for the exit of the maze
def getExitPoint():
x = crop.size[0] - 1
for y in range(0, crop.size[1]):
if(crop.getpixel((x,y)) == (255,255,255)):
return x,y
if(crop.getpixel((y,x)) == (255,255,255)):
return y,x
this is how I convert my image to a 2D array
maze = []
width,height = crop.size
for x in range(0, width):
mazeX = []
for y in range(0, height):
if(crop.getpixel((x,y)) == (0,0,0)):
mazeX.append("B")
elif(crop.getpixel((x,y)) == (255,255,255)):
mazeX.append("W")
maze.append(mazeX)
What it is supposed to do, is convert the image into a 2D array, which gets traversed recursively (using backtracking) looking for a successful path, then draws a red line over the path.
The script does not work on this image, it stops at pixel X=76, y=153 every time, and i am not sure what to do/what I'm doing wrong
The borders are 1px and the path is 1px. There are not any errors, stack-traces, exceptions, or anything thrown. The recursion just stops and the program quits. Any Ideas?

There are some various problems with your code, but I think what is happening is that you are running over the recursion limit. Your maze is fairly large and complex. I needed to use 4000 (For me, 1000 is the default, and 3000 wasn't large enough).
Not sure what image library you're using; I used PIL.Image
Your input image is actually 203x203, and there is a challenge to locate the entrance and exit. I assumed the entrance is at the top or on the left side, and the exit is on the right side or along the bottom.
import sys
import argparse
import PIL.Image
import os
colors = {
'white' : (255, 255, 255),
'black' : (0, 0, 0),
'red' : (128, 0, 0),
'green' : (0, 255, 0) }
def isValid(image, x, y):
if x < image.size[0] and y < image.size[1] and x >= 0 and y >= 0:
if image.getpixel((x, y)) == colors['white']:
return True
return False
def getEntryPoint(image):
# Search along top.
for x in range(1, image.size[0] - 1):
if image.getpixel((x, 1)) == colors['white']:
return x, 1
# Search along left side.
for y in range(1, image.size[1] - 1):
if image.getpixel((1, y)) == colors['white']:
return 1, y
# Maze is invalid if there is no entry point.
raise Exception('No entry point found!')
def getExitPoint(image):
# Search along bottom.
for x in range(1, image.size[0] - 1):
if image.getpixel((x, image.size[1] - 2)) == colors['white']:
return x, image.size[1] - 2
# Search along right side.
for y in range(1, image.size[1] - 1):
if image.getpixel((image.size[0] - 2, y)) == colors['white']:
return image.size[0] - 2, y
# Maze is invalid if there is no exit point.
raise Exception('No exit point found!')
def solveRecursiveMaze(image, x, y):
successful = False
if (x, y) == getExitPoint(image):
successful = True
elif isValid(image, x, y):
# set to show it's a visited path
image.putpixel((x, y), colors['red'])
successful = solveRecursiveMaze(image, x-1, y)
if not successful:
successful = solveRecursiveMaze(image, x, y+1)
if not successful:
successful = solveRecursiveMaze(image, x+1, y)
if not successful:
successful = solveRecursiveMaze(image, x, y-1)
if successful:
# Mark to show it's a valid path.
image.putpixel((x, y), colors['green'])
return successful
def main(options):
solved = False
if options.depth:
sys.setrecursionlimit(options.depth)
try:
image = PIL.Image.open(options.filename)
except:
print('ERROR: Could not open %s' % (options.filename))
else:
image = image.convert('RGB')
x, y = getEntryPoint(image)
print('Entering maze at x = %d, y = %d' % (x, y))
solved = solveRecursiveMaze(image, x, y)
if not solved:
print('No solution exists.')
else:
print('Solved maze.')
basename = os.path.splitext(options.filename)[0]
image.save(basename + '_solution' + '.png', 'PNG')
return 0 if solved else 1
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--d',
'-depth',
dest='depth',
type=int,
help='Set Python recursion limit with sys.setrecursionlimit()')
parser.add_argument(
'filename',
help='Image containing the maze.')
options = parser.parse_args()
sys.exit(main(options))

Related

problems with unpacking returned values in tuples

I have a function that gets arguments of the file, this function looks for the coordinates of this file on the screen and then returns them.
I know that this question could be very stupid but please help.
redx,redy = somefunc(Ui_Ellements[9], 12)
def somefunc(file,index):
.....
x=int((pt[0]*2+w)/2)
y=int((pt[1]*2+h)/2)
print("Found "+file,x,y)
if index==12:
return (x,y)
All should be working correctly, but I have such a problem
redx,redy = somefunc(Ui_Ellements[9], 12) TypeError: cannot unpack non-iterable int object
I tried:
1.
def move(index):
print(index)
for i in index:
print(i)
red = somefunc(Ui_Ellements[9], 12)
move(red)
def somefunc(file,index):
.....
x=int((pt[0]*2+w)/2)
y=int((pt[1]*2+h)/2)
print("Found "+file,x,y)
if index==12:
return (x,y)
red = somefunc(Ui_Ellements[9], 12)
print(red[0])
red = somefunc(Ui_Ellements[9], 12)
print(red[-1])
P.s all data is correct and if I try
print(red)
it outputs data
(1205, 306)
def somefunc(file,index):
global sens
global top
global left
global Resolution
time.sleep(speed)
if index ==7 or index ==12 and file != chekers[8] :
img = cv2.imread('files/'+Resolution+'/part.png')
else:
screen()
img = cv2.imread('files/'+Resolution+'/screen.png') # картинка, на которой ищем объект
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # преобразуем её в серуюш
template = cv2.imread('files/'+Resolution+'/'+file,cv2.IMREAD_GRAYSCALE) # объект, который преобразуем в серый, и ищем его на gray_img
w, h = template.shape[::-1] # инвертируем из (y,x) в (x,y)`
result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= sens)
if index =='battletag':
print(loc)
if len(loc[0]) != 0:
j=0
for pt in zip(*loc[::-1]):
Flag=False
for num in range(3):
if pt[0] < enemywiz[num] + 10 and pt[0] > enemywiz[num] - 10:
Flag=True
pass
if not Flag:
for num in range(3):
if enemywiz[num] == 0 :
enemywiz[num]=pt[0]
x = (pt[0] * 2 + w) / 2
y = (pt[1] * 2 + h) / 2
enemywizF[j]=x,y
j+=1
break
for i in range(2):
enemywiz[i]=0
return 0
if len(loc[0]) !=0:
for pt in zip(*loc[::-1]):
pt[0] + w
pt[1] + h
x=int((pt[0]*2+w)/2)
y=int((pt[1]*2+h)/2)
print("Found "+file,x,y)
if index==12:
return (x,y)
if (index==6 or file == Ui_Ellements[5] or file == chekers[7]):
global xm
global ym
xm=x
ym=y
return True
if file == chekers[8]:
if index ==7:
xm+=left
ym+=top
ahk.mouse_move(xm, ym, speed=2)
time.sleep(0.5)
ahk.mouse_drag(x, y, relative=False)
return True
if file == chekers[5]:
ahk.mouse_move(x, y+70, speed=3) # Moves the mouse instantly to absolute screen position
ahk.click()
return True
if file ==buttons[5]:
ahk.mouse_move(x, y, speed=5)
return True
if index == 1:
return True
if index == 7:
xm = x
ym = y
return True
ahk.mouse_move(x, y, speed=5) # Moves the mouse instantly to absolute screen position
ahk.click() # Click the primary mouse button
if file ==buttons[7]:
return True
if file == Ui_Ellements[3]:
group_create()
else:
print("Not found "+file)
if index == 12:
return 0
You need keep a function has basically same type of output in most of common situation.
you can replace return 0 to return 0,0
or you can replace it to raise Exception(f"Not found {file}")
I don't know what you exactly want to return when you return True, you can also replace it with same style, such as raise Exception(f"can't get redx and redy").
example:
def test(x,y):
index = x + y
if index == 12:
return (x,y)
else:
raise Exception("Not found file")
# return 0,0
try:
a,b = test(2,3)
print(a,b)
except Exception as e:
print(e)
try:
a,b = test(7,5)
print(a,b)
except Exception as e:
print(e)
result:
Not found file
7 5

Python Canvas bbox returns None

so i am making a game (Minecraft 2D (scuffed edition)) and if you are underground in a cave, i want the background to be dark, not that you can see the sun if you are in a cave, so what i do is i want to check for the lowest grass block on screen (witch is ground level)
and then place the cave background atthat location, but when i trie to get the bbox or the coords of a block object on the canvas, it just gives me None.
So i have this now:
(the camera function gets run every time the player moves)
def camera(self):
for i in self.block_sprites:
self.canvas.delete(i.get_image())
for i in self.sprites:
self.canvas.delete(i)
self.draw_current_pos()
self.draw_background()
def add_block_sprite(self, x, y, block_type):
self.current_sprite = Block(x, y, self, block_type)
self.block_sprites.append(self.current_sprite)
def draw_current_pos(self):
self.sprites = []
self.block_sprites = []
for x in range(0, 20):
for y in range(0, 21):
if self.world.world[self.posx+x][self.posy+y] == 0 :
continue
else:
self.add_block_sprite(x, y, self.world.world[self.posx + x][self.posy + y])
def draw_background(self):
grass_y = 1000
test_for_grass = False
for block in self.block_sprites:
if block.get_type() == 1:
print(self.canvas.bbox(block)) #prints "None"
if self.canvas.bbox(block)[1] > grass_y: ####error here####
grass_y = self.canvas.coords(block)[1]
test_for_grass = True
if not test_for_grass and self.posy < 30:
grass_y = 1000 - grass_y
cave = self.canvas.create_image(0, grass_y, image=self.cavebg, anchor="nw")
self.sprites.append(cave)
Do you know what the problem is?
I hope you can help!
(if this it to little code for the problem tell me)

Global variable set and then reset in Python Canvas

Good afternoon,
I'm building out (suppose to be) a relatively simple q & a experiment for a psychology experiment. I'm using Pythons canvas for drawing and painting but have hit a bit of brick wall and a classic update scenario, I think. Here's the code:
# Replace with 60000 for 1 minute a question
screen_timeout = 10000
start_time = clock.time()
# Create a canvas, mouse & keyboard object
canvas = canvas()
mouse = mouse()
kb = keyboard(timeout=0)
question = '1) Which two chemicals are discussed?'
answer = ''
show_circle = False
global circle_clicked
circle_clicked = False
def draw_question_and_answer(c, a):
c.text('%s<br />(Just start typing; press enter to submit)<br /><br />%s' % (question, a))
def draw_mouse(c, (x, y)):
c.fixdot(x, y)
def draw_circle(c):
c['circle'] = Circle(0, 0, 50, fill=True, color='red')
def paint(c, a, s, (x, y)):
c.clear()
# show_circle_every(s, 2500)
# NOTE Drawing order matters here
if s:
draw_question_and_answer(c, a)
draw_circle(c)
draw_mouse(c, (x, y))
if (x, y) in c['circle']:
circle_clicked = True
else:
draw_question_and_answer(c, a)
c.show()
def game_loop(c, m, a, s):
while True:
if clock.time() - start_time >= screen_timeout:
break
# if clock.time() - start_time >= 2500 and s == False:
# s = True
response, timestamp_kb = kb.get_key()
(x, y), timestamp_m = m.get_pos()
# TODO Extrapolate to function
if s == False:
if response == 'return':
var.gq1 = a
log.write_vars()
break
if response != None and response != 'right shift' and response != 'left shift':
if response == 'space':
a += ' '
elif response == 'backspace':
a = a[:-1]
else:
a += response
paint(c, a, s, (x, y))
# If the user enters the circle it should disappear
print circle_clicked
if clock.time() - start_time >= 2500 and circle_clicked == False:
s = True
game_loop(canvas, mouse, answer, show_circle)
What I'm trying to do here is show a red circle every 2.5 seconds and keep the circle there until the users mouse enters the boundary of the circle. In these lines here:
if clock.time() - start_time >= 2500 and circle_clicked == False:
s = True
I'm setting the s variable (show) to True to show the circle which works. And in this line:
if (x, y) in c['circle']:
circle_clicked = True
if the user enters the circle I'm setting clicked to true. But if I print the values I can see circle_clicked changing from True to back False - How come? Does the loop get it's own version of circle_clicked? If so how?
What am I doing wrong here as I think it's quite a simple problem? Coming from a purely Javascript background also complicates things as I'm trying to learn Python to do it.
Thanks
Change your paint function to this
def paint(c, a, s, (x, y)):
global circle_clicked
c.clear()
# show_circle_every(s, 2500)
# NOTE Drawing order matters here
if s:
draw_question_and_answer(c, a)
draw_circle(c)
draw_mouse(c, (x, y))
if (x, y) in c['circle']:
circle_clicked = True
else:
draw_question_and_answer(c, a)
c.show()

Python/OpenCV - Not detecting grid

The following script comes from http://projectproto.blogspot.co.uk/2014/07/opencv-python-2048-game-solver.html
import cv2
import numpy as np
import win32api, win32gui, win32ui, win32con, win32com.client
from PIL import Image, ImageFont, ImageDraw, ImageOps
# create training model based on the given TTF font file
# http://projectproto.blogspot.com/2014/07/opencv-python-digit-recognition.html
def createDigitsModel(fontfile, digitheight):
font = ImageFont.truetype(fontfile, digitheight)
samples = np.empty((0,digitheight*(digitheight/2)))
responses = []
for n in range(10):
pil_im = Image.new("RGB", (digitheight, digitheight*2))
ImageDraw.Draw(pil_im).text((0, 0), str(n), font=font)
pil_im = pil_im.crop(pil_im.getbbox())
pil_im = ImageOps.invert(pil_im)
#pil_im.save(str(n) + ".png")
# convert to cv image
cv_image = cv2.cvtColor(np.array( pil_im ), cv2.COLOR_RGBA2BGRA)
gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
roi = cv2.resize(thresh,(digitheight,digitheight/2))
responses.append( n )
sample = roi.reshape((1,digitheight*(digitheight/2)))
samples = np.append(samples,sample,0)
samples = np.array(samples,np.float32)
responses = np.array(responses,np.float32)
model = cv2.KNearest()
model.train(samples,responses)
return model
class Board(object):
UP, DOWN, LEFT, RIGHT = 1, 2, 3, 4
FONT = "font/ClearSans-Bold.ttf"
def __init__(self, clientwindowtitle):
self.hwnd = self.getClientWindow(clientwindowtitle)
if not self.hwnd:
return
self.hwndDC = win32gui.GetWindowDC(self.hwnd)
self.mfcDC = win32ui.CreateDCFromHandle(self.hwndDC)
self.saveDC = self.mfcDC.CreateCompatibleDC()
self.cl, self.ct, right, bot = win32gui.GetClientRect(self.hwnd)
self.cw, self.ch = right-self.cl, bot-self.ct
self.cl += win32api.GetSystemMetrics(win32con.SM_CXSIZEFRAME)
self.ct += win32api.GetSystemMetrics(win32con.SM_CYSIZEFRAME)
self.ct += win32api.GetSystemMetrics(win32con.SM_CYCAPTION)
self.ch += win32api.GetSystemMetrics(win32con.SM_CYSIZEFRAME)*2
self.saveBitMap = win32ui.CreateBitmap()
self.saveBitMap.CreateCompatibleBitmap(self.mfcDC, self.cw, self.ch)
self.saveDC.SelectObject(self.saveBitMap)
self.tiles, self.tileheight, self.contour = self.findTiles(self.getClientFrame())
if not len(self.tiles):
return
self.digitheight = self.tileheight / 2
self.digitsmodel = createDigitsModel(self.FONT, self.digitheight)
self.update()
def getClientWindow(self, windowtitle):
toplist, winlist = [], []
def enum_cb(hwnd, results):
winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
win32gui.EnumWindows(enum_cb, toplist)
window = [(hwnd, title) for hwnd, title in winlist if windowtitle.lower() in title.lower()]
if not len(window):
return 0
return window[0][0]
def getClientFrame(self):
self.saveDC.BitBlt((0, 0), (self.cw, self.ch),
self.mfcDC, (self.cl, self.ct), win32con.SRCCOPY)
bmpinfo = self.saveBitMap.GetInfo()
bmpstr = self.saveBitMap.GetBitmapBits(True)
pil_img = Image.frombuffer( 'RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX', 0, 1)
array = np.array( pil_img )
cvimage = cv2.cvtColor(array, cv2.COLOR_RGBA2BGRA)
return cvimage
def findTiles(self, cvframe):
tiles, avgh = [], 0
gray = cv2.cvtColor(cvframe,cv2.COLOR_BGRA2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
def findBoard(contours): # get largest square
ww, sqcnt = 10, None
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if w>ww and abs(w-h)<w/10:
ww = w
sqcnt = cnt
return sqcnt
board = findBoard(contours)
if board==None:
print 'board not found!'
return tiles, avgh, board
bx,by,bw,bh = cv2.boundingRect(board)
#cv2.rectangle(cvframe,(bx,by),(bx+bw,by+bh),(0,255,0),2)
#cv2.imshow('board',cvframe)
#cv2.waitKey(0)
#cv2.destroyWindow( 'board' )
maxh = bh/4
minh = (maxh*4)/5
count = 0
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
if y>by and w>minh and w<maxh and h>minh and h<maxh:
avgh += h
count += 1
if not count:
print 'no tile found!'
return tiles, avgh, board
avgh = avgh / count
margin = (bh-avgh*4)/5
for row in range(4):
for col in range(4):
x0 = bx + avgh*col + margin*(col+1)
x1 = x0 + avgh
y0 = by + avgh*row + margin*(row+1)
y1 = y0 + avgh
tiles.append([x0, y0, x1, y1])
#cv2.rectangle(cvframe,(x0,y0),(x1,y1),(0,255,0),2)
#cv2.imshow('tiles',cvframe)
#cv2.waitKey(0)
#cv2.destroyWindow( 'tiles' )
return tiles, avgh, board
def getTileThreshold(self, tileimage):
gray = cv2.cvtColor(tileimage,cv2.COLOR_BGR2GRAY)
row, col = gray.shape
tmp = gray.copy().reshape(1, row*col)
counts = np.bincount(tmp[0])
sort = np.sort(counts)
modes, freqs = [], []
for i in range(len(sort)):
freq = sort[-1-i]
if freq < 4:
break
mode = np.where(counts==freq)[0][0]
modes.append(mode)
freqs.append(freq)
bg, fg = modes[0], modes[0]
for i in range(len(modes)):
fg = modes[i]
#if abs(bg-fg)>=48:
if abs(bg-fg)>32 and abs(fg-150)>4: # 150?!
break
#print bg, fg
if bg>fg: # needs dark background ?
tmp = 255 - tmp
bg, fg = 255-bg, 255-fg
tmp = tmp.reshape(row, col)
ret, thresh = cv2.threshold(tmp,(bg+fg)/2,255,cv2.THRESH_BINARY)
return thresh
def getTileNumbers(self, cvframe):
numbers = []
outframe = np.zeros(cvframe.shape,np.uint8)
def guessNumber(digits):
for i in range(1,16):
nn = 2**i
ss = str(nn)
dd = [int(c) for c in ss]
if set(digits) == set(dd):
return nn
return 0
for tile in self.tiles:
x0,y0,x1,y1 = tile
tileimage = cvframe[y0:y1,x0:x1]
cv2.rectangle(cvframe,(x0,y0),(x1,y1),(0,255,0),2)
cv2.rectangle(outframe,(x0,y0),(x1,y1),(0,255,0),1)
thresh = self.getTileThreshold(tileimage)
contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
dh = self.digitheight
digits = []
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if h>w and h>(dh*1)/5 and h<(dh*6)/5:
cv2.rectangle(cvframe,(x0+x,y0+y),(x0+x+w,y0+y+h),(0,0,255),1)
roi = thresh[y:y+h,x:x+w]
roi = cv2.resize(roi,(dh,dh/2))
roi = roi.reshape((1,dh*(dh/2)))
roi = np.float32(roi)
retval, results, neigh_resp, dists = self.digitsmodel.find_nearest(roi, k=1)
digit = int((results[0][0]))
string = str(digit)
digits.append(digit)
cv2.putText(outframe,string,(x0+x,y0+y+h),0,float(h)/24,(0,255,0))
numbers.append(guessNumber(digits))
return numbers, outframe
def getWindowHandle(self):
return self.hwnd
def getBoardContour(self):
return self.contour
def update(self):
frame = self.getClientFrame()
self.tilenumbers, outframe = self.getTileNumbers(frame)
return self.tilenumbers, frame, outframe
def copyTileNumbers(self):
return self.tilenumbers[:]
def getCell(self, tiles, x, y):
return tiles[(y*4)+x]
def setCell(self, tiles, x, y, v):
tiles[(y*4)+x] = v
return tiles
def getCol(self, tiles, x):
return [self.getCell(tiles, x, i) for i in range(4)]
def setCol(self, tiles, x, col):
for i in range(4):
self.setCell(tiles, x, i, col[i])
return tiles
def getLine(self, tiles, y):
return [self.getCell(tiles, i, y) for i in range(4)]
def setLine(self, tiles, y, line):
for i in range(4):
self.setCell(tiles, i, y, line[i])
return tiles
def validMove(self, tilenumbers, direction):
if direction == self.UP or direction == self.DOWN:
for x in range(4):
col = self.getCol(tilenumbers, x)
for y in range(4):
if(y < 4-1 and col[y] == col[y+1] and col[y]!=0):
return True
if(direction == self.DOWN and y > 0 and col[y] == 0 and col[y-1]!=0):
return True
if(direction == self.UP and y < 4-1 and col[y] == 0 and col[y+1]!=0):
return True
if direction == self.LEFT or direction == self.RIGHT:
for y in range(4):
line = self.getLine(tilenumbers, y)
for x in range(4):
if(x < 4-1 and line[x] == line[x+1] and line[x]!=0):
return True
if(direction == self.RIGHT and x > 0 and line[x] == 0 and line[x-1]!=0):
return True
if(direction == self.LEFT and x < 4-1 and line[x] == 0 and line[x+1]!=0):
return True
return False
def moveTileNumbers(self, tilenumbers, direction):
def collapseline(line, direction):
if (direction==self.LEFT or direction==self.UP):
inc = 1
rg = xrange(0, 4-1, inc)
else:
inc = -1
rg = xrange(4-1, 0, inc)
pts = 0
for i in rg:
if line[i] == 0:
continue
if line[i] == line[i+inc]:
v = line[i]*2
line[i] = v
line[i+inc] = 0
pts += v
return line, pts
def moveline(line, directsion):
nl = [c for c in line if c != 0]
if directsion==self.UP or directsion==self.LEFT:
return nl + [0] * (4 - len(nl))
return [0] * (4 - len(nl)) + nl
score = 0
if direction==self.LEFT or direction==self.RIGHT:
for i in range(4):
origin = self.getLine(tilenumbers, i)
line = moveline(origin, direction)
collapsed, pts = collapseline(line, direction)
new = moveline(collapsed, direction)
tilenumbers = self.setLine(tilenumbers, i, new)
score += pts
elif direction==self.UP or direction==self.DOWN:
for i in range(4):
origin = self.getCol(tilenumbers, i)
line = moveline(origin, direction)
collapsed, pts = collapseline(line, direction)
new = moveline(collapsed, direction)
tilenumbers = self.setCol(tilenumbers, i, new)
score += pts
return score, tilenumbers
# AI based on "term2048-AI"
# https://github.com/Nicola17/term2048-AI
class AI(object):
def __init__(self, board):
self.board = board
def nextMove(self):
tilenumbers = self.board.copyTileNumbers()
m, s = self.nextMoveRecur(tilenumbers[:],3,3)
return m
def nextMoveRecur(self, tilenumbers, depth, maxDepth, base=0.9):
bestMove, bestScore = 0, -1
for m in range(1,5):
if(self.board.validMove(tilenumbers, m)):
score, newtiles = self.board.moveTileNumbers(tilenumbers[:], m)
score, critical = self.evaluate(newtiles)
newtiles = self.board.setCell(newtiles,critical[0],critical[1],2)
if depth != 0:
my_m,my_s = self.nextMoveRecur(newtiles[:],depth-1,maxDepth)
score += my_s*pow(base,maxDepth-depth+1)
if(score > bestScore):
bestMove = m
bestScore = score
return bestMove, bestScore
def evaluate(self, tilenumbers, commonRatio=0.25):
maxVal = 0.
criticalTile = (-1, -1)
for i in range(8):
linearWeightedVal = 0
invert = False if i<4 else True
weight = 1.
ctile = (-1,-1)
cond = i%4
for y in range(4):
for x in range(4):
if cond==0:
b_x = 4-1-x if invert else x
b_y = y
elif cond==1:
b_x = x
b_y = 4-1-y if invert else y
elif cond==2:
b_x = 4-1-x if invert else x
b_y = 4-1-y
elif cond==3:
b_x = 4-1-x
b_y = 4-1-y if invert else y
currVal=self.board.getCell(tilenumbers,b_x,b_y)
if(currVal == 0 and ctile == (-1,-1)):
ctile = (b_x,b_y)
linearWeightedVal += currVal*weight
weight *= commonRatio
invert = not invert
if linearWeightedVal > maxVal:
maxVal = linearWeightedVal
criticalTile = ctile
return maxVal, criticalTile
def solveBoard(self, moveinterval=500):
boardHWND = self.board.getWindowHandle()
if not boardHWND:
return False
bx, by, bw, bh = cv2.boundingRect(self.board.getBoardContour())
x0, x1, y0, y1 = bx, bx+bw, by, by+bh
win32gui.SetForegroundWindow(boardHWND)
shell = win32com.client.Dispatch('WScript.Shell')
print 'Set the focus to the Game Window, and the press this arrow key:'
keymove = ['UP', 'DOWN', 'LEFT', 'RIGHT']
delay = moveinterval / 3 # milliseconds delay to cancel board animation effect
prev_numbers = []
while True:
numbers, inframe, outframe = self.board.update()
if numbers != prev_numbers:
cv2.waitKey(delay)
numbers, inframe, outframe = self.board.update()
if numbers == prev_numbers: # recheck if has changed
continue
prev_numbers = numbers
move = ai.nextMove()
if move:
key = keymove[move-1]
shell.SendKeys('{%s}'%key)
print key
cv2.waitKey(delay)
cv2.imshow('CV copy',inframe[y0:y1,x0:x1])
cv2.imshow('CV out', outframe[y0:y1,x0:x1])
cv2.waitKey(delay)
cv2.destroyWindow( 'CV copy' )
cv2.destroyWindow( 'CV out' )
# http://gabrielecirulli.github.io/2048/
# http://ov3y.github.io/2048-AI/
board = Board("2048 - Google Chrome")
#board = Board("2048 - Mozilla Firefox")
ai = AI(board)
ai.solveBoard(360)
print 'stopped.'
I have Google Chrome open with the example URL http://ov3y.github.io/2048-AI/ open, running the script has the following error:
20.py:109: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
if board==None:
no tile found!
Set the focus to the Game Window, and the press this arrow key:
Then nothing, it just sits there. So the part I'm concerned with most is no tile found!. Un commenting the lines:
#cv2.rectangle(cvframe,(bx,by),(bx+bw,by+bh),(0,255,0),2)
#cv2.imshow('board',cvframe)
#cv2.waitKey(0)
#cv2.destroyWindow( 'board' )
Shows the following window on screen:
Can anyone explain why OpenCV is failing to detect the grid, or how to go about debugging this?
Most likely it is not a problem with detecting grid, but with capturing browser window - you are trying to find grid on an empty image which of course fails. First make sure that you have grabbed firefox/chrome/opera screen window correctly - in function getClientFrame(self) put this code:
cv2.imshow('browser window', cvimage)
cv2.waitKey(10000)
just before the final return cvimage. It should show you the browser window for 10 seconds. If it doesn't than it will 100% sure that problem is with capturing browser window, not with detecting grid. To check what's wrong with capturing browser window use win32api.GetLastError() function (you can check error codes here).
Of course there is a chance that i'm wrong and it's a problem with detecting grid - if so, please provide a sample image (just save the image displayed by the code i provided) so we can test it.
\\edit:
I've just noticed the second part of your post - so most likely i'm wrong, but you can test it anyway. It seems that you are capturing one chrome window and part of some other window - try to make your browser window fullscreen.
\\edit2:
After closer look at you image i realised strange thing - capture image has vertical lines and has width (without repeated part onthe right side) smaller than the original window(but height seems to be fine). Width seems to be 75% of original width so i guess that PIL treats every 4 bytes as one pixel, but it should use only 3 bytes per pixel. It's hard to test it, because on my system (win 8.1 64bit) it's working fine. Possible solutions (i can't test them, so you need to check which one will work.. sory :) ):
Try to change this line:
pil_img = Image.frombuffer( 'RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
to something like this:
pil_img = Image.frombuffer( 'RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGR', 0, 1)
generally you need to change value of fifth parameter from BGRX to something else - most likely to 'BGR', full list of options is here. If it won't work try to play with different values of first and fifth parameters.
On the screenshot it looks like you have some quite old version of Windows or at least you are using old gui (which is great btw!). If - except for setting gui style to "old style" - you've set (or windows've done it for you) your color quality to something else than "Highest (32bit)" it may cause your problem as well. Try to set it to "Highest (32 bit)". To be clear - i'm talking about settings from this window:
(on the right side, near bottom and color palette).
If you have 2 (or more) screens, test you program while using only one. Also if you are using some alternative window manager (or some other weird extension like something for multiple desktops) turn it off and try again.

PIL saving only the first image

I'm trying to do some batch image processing, but I'm having trouble saving the images once they are created. Here is all of the code:
import Image
import os
import random
training_images = []
training_path = 'cropped'
background_images = []
background_path = 'background'
training_file = 'train'
def get_image_list(file_path):
return os.listdir(file_path)
def rotate_randomely(im):
number = random.randint(1, 6)
if number == 1:
return im.transpose(Image.FLIP_LEFT_RIGHT)
elif number == 2:
return im.transpose(Image.FLIP_TOP_BOTTOM)
elif number == 3:
return im.transpose(Image.ROTATE_90)
elif number == 4:
return im.transpose(Image.ROTATE_180)
elif number == 5:
return im.transpose(Image.ROTATE_270)
else:
return im
def get_random_point(maxX, maxY):
x = random.randint(0, maxX)
y = random.randint(0, maxY)
return x, y
def insert_image(from_image, onto_image):
from_image = resize_smaller(from_image, onto_image.size)
x, y = get_random_point(onto_image.size[0] - from_image.size[0], onto_image.size[1] - from_image.size[0])
onto_image.paste(from_image, (x, y))
width = from_image.size[0]
height = from_image.size[1]
return x, y, width, height
def resize_smaller(image, maxXY):
if image.size[0] > maxXY[0] or image.size[1] > maxXY[1]:
image = image.resize((image.size[0] / 2, image.size[1] / 2))
if image.size[0] > maxXY[0] or image.size[1] > maxXY[1]:
resize_smaller(image, maxXY)
else:
return image
training_images = get_image_list(training_path)
background_images = get_image_list(background_path)
print('training_images size', len(training_images))
print('background_images size', len(background_images))
for training_image in training_images:
index = 0
for background_image in background_images:
name = background_image
training_image = Image.open(training_path + '/' + training_image)
background_image = Image.open(background_path + '/' + background_image)
training_image = rotate_randomely(training_image)
x, y, width, height = insert_image(training_image, background_image)
background_image.save('images/' + str(index) + name)
index = index + 1
The output:
('training_images size', 7)
('background_images size', 1). So it's finding the images correctly, but when I look at the results there is only one image saved, and it only has a 0 pre-pended to the image name. Yet I know it went through each image so there should be seven of them.
I've been looking at this for a while, and I just don't see where I went wrong. Is there something weird about pil's save method that I'm not aware of?
put the index = 0 outside the upper for loop otherwise it will become 0 every iteration and save over the top of old files.

Categories