diff --git a/src/config.py b/src/config.py new file mode 100644 --- /dev/null +++ b/src/config.py @@ -0,0 +1,11 @@ +import logging + + +logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.DEBUG) + +class misc: + version=(0,0) + +class gui: + showBigPoints=True + showGrid=False diff --git a/src/gui.py b/src/gui.py --- a/src/gui.py +++ b/src/gui.py @@ -1,14 +1,13 @@ import tkinter as tk from PIL import ImageTk import PIL + +import config from corners import * from grid import * from image_analyzer import * from go import * -showBigPoints=True -showGrid=False - class Application(tk.Frame): def __init__(self, master=None): @@ -36,8 +35,11 @@ class Application(tk.Frame): self.boardView.grid(column=1,row=0) # more controls below the board - self.scaleTresB=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=lambda x: self.redrawImgView()) - self.scaleTresW=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=lambda x: self.redrawImgView()) + def _refreshTresholds(_): + self.refreshTresholds() + self.redrawImgView() + self.scaleTresB=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=_refreshTresholds) + self.scaleTresW=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=_refreshTresholds) self.scaleTresB.set(30.0) self.scaleTresW.set(60.0) self.scaleTresB.grid(column=0,row=1,columnspan=2) @@ -69,7 +71,7 @@ class Application(tk.Frame): for corner in self.corners.corners: self.markPoint(corner.x,corner.y) - if self.boardGrid!=None and showGrid: + if self.boardGrid!=None and config.gui.showGrid: for r in range(19): a=self.boardGrid.intersections[r][0] b=self.boardGrid.intersections[r][-1] @@ -79,7 +81,7 @@ class Application(tk.Frame): b=self.boardGrid.intersections[-1][c] self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00') - if self.boardGrid!=None and showBigPoints: + if self.boardGrid!=None and config.gui.showBigPoints: for r in range(19): for c in range(19): ((r1,c1),(r2,c2))=self.boardView.detector.relevantRect(self.boardGrid.intersections[r][c],*(self.boardGrid.stoneSizeAt(r,c,sizeCoef))) @@ -89,7 +91,7 @@ class Application(tk.Frame): shift=EPoint(origWidth-self.img.width()*sizeCoef,origHeight-self.img.height()*sizeCoef)/2 - self.boardView.redrawState(self.imgOrig,sizeCoef,shift,tresB,tresW) + self.boardView.redrawState(self.imgOrig,sizeCoef,shift) ## Marks a point at the image with a green cross. Used for corners. def markPoint(self,x,y): @@ -98,6 +100,10 @@ class Application(tk.Frame): self.imgView.grid() + def refreshTresholds(self): + self.boardView.detector.tresB=self.scaleTresB.get() + self.boardView.detector.tresW=self.scaleTresW.get() + ## Handles and presents the game state as detected by the program. class BoardView(tk.Canvas): @@ -113,14 +119,11 @@ class BoardView(tk.Canvas): self.grid() ## Redraws and reananalyzes the board view. - # - # @param tresB upper intensity treshold for a pixel to be considered black, [0-100] - # @param tresW lower intensity treshold for a pixel to be considered white, [0-100] - def redrawState(self,img,sizeCoef,shift,tresB,tresW): + def redrawState(self,img,sizeCoef,shift): self.create_rectangle(0,0,360,360,fill="#ffcc00") self.drawGrid() - self.detector.analyze(img,tresB,tresW,sizeCoef,shift) + self.detector.analyze(img,sizeCoef,shift) for r,row in enumerate(self.detector.board): for c,point in enumerate(row): @@ -161,5 +164,6 @@ class BoardView(tk.Canvas): root = tk.Tk() +root.title("OneEye {0}.{1}".format(*config.misc.version)) app = Application(master=root) app.mainloop() diff --git a/src/image_analyzer.py b/src/image_analyzer.py --- a/src/image_analyzer.py +++ b/src/image_analyzer.py @@ -1,22 +1,26 @@ -from go import * +import logging +from go import * class ImageAnalyzer: - def __init__(self): + def __init__(self,tresB=30,tresW=60): self.board=[[Go.EMPTY]*19 for r in range(19)] self.grid=None - def analyze(self,image,tresB,tresW,sizeCoef,shift): + self.tresB=tresB + self.tresW=tresW + + def analyze(self,image,sizeCoef,shift): if self.grid==None: return False for r in range(19): for c in range(19): intersection=self.grid.intersections[r][c] - self.board[r][c]=self.analyzePoint(image,intersection*sizeCoef+shift,*(self.grid.stoneSizeAt(r,c,sizeCoef)),tresB,tresW) + self.board[r][c]=self.analyzePoint(image,r,c,intersection*sizeCoef+shift,*(self.grid.stoneSizeAt(r,c,sizeCoef))) - def analyzePoint(self,image,imageCoords,stoneWidth,stoneHeight,tresB,tresW): + def analyzePoint(self,image,row,col,imageCoords,stoneWidth,stoneHeight): b=w=e=0 ((x1,y1),(x2,y2))=self.relevantRect(imageCoords,stoneWidth,stoneHeight) @@ -28,10 +32,12 @@ class ImageAnalyzer: I=(red+green+blue)/255/3 m=min(red,green,blue) S=1-m/I - if 100*ItresW: w+=1 + if 100*Iself.tresW: w+=1 else: e+=1 + logging.debug("(%d,%d) ... (b=%d,w=%d,e=%d)", row, col, b, w, e) + if b>=w and b>=e: return Go.BLACK if w>=b and w>=e: return Go.WHITE return Go.EMPTY