Changeset - f6b3d570893c
[Not reviewed]
default
0 2 1
Laman - 8 years ago 2016-11-24 21:16:21

basic logging, basic configuration, little refactoring
3 files changed with 41 insertions and 20 deletions:
0 comments (0 inline, 0 general)
src/config.py
Show inline comments
 
new file 100644
 
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
src/gui.py
Show inline comments
 
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):
 
		self.corners=Corners()
 
		self.boardGrid=None
 
 
		tk.Frame.__init__(self, master)
 
		self.grid(column=0,row=0)
 
		self.createWidgets()
 
 
	def createWidgets(self):
 
		# a captured frame with overlay graphics
 
@@ -27,26 +26,29 @@ class Application(tk.Frame):
 
 
		self.img=ImageTk.PhotoImage(self.imgOrig.resize((int(self.imgView['width']),int(self.imgView['height'])),resample=PIL.Image.BILINEAR))
 
 
		self.imgView.bind('<1>',lambda e: self.addCorner(e.x,e.y))
 
 
		self.imgView.grid(column=0,row=0)
 
 
		# board with detected stones
 
		self.boardView=BoardView(self)
 
		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)
 
		self.scaleTresW.grid(column=0,row=2,columnspan=2)
 
 
		# render everything
 
		self.redrawImgView()
 
 
	## Stores a grid corner located at x,y coordinates.
 
	def addCorner(self,x,y):
 
		self.corners.add(x,y)
 
		if self.corners.canonizeOrder():
 
@@ -60,76 +62,77 @@ class Application(tk.Frame):
 
		self.imgView.create_image(2,2,anchor="nw",image=self.img)
 
 
		origWidth,origHeight=self.imgOrig.size
 
		widthRatio=origWidth/self.img.width()
 
		heightRatio=origHeight/self.img.height()
 
		sizeCoef=max(widthRatio,heightRatio)
 
		tresB=self.scaleTresB.get()
 
		tresW=self.scaleTresW.get()
 
 
		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]
 
				self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
 
			for c in range(19):
 
				a=self.boardGrid.intersections[0][c]
 
				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)))
 
					self.imgView.create_rectangle(r1,c1,r2,c2,outline="#00ffff")
 
 
		self.imgView.grid()
 
 
		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):
 
		self.imgView.create_line(x-3,y-3,x+4,y+4,fill="#00ff00")
 
		self.imgView.create_line(x-3,y+3,x+4,y-4,fill="#00ff00")
 
 
		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):
 
	def __init__(self, master=None):
 
		self.detector=ImageAnalyzer()
 
		self.boardGrid=None
 
 
		tk.Canvas.__init__(self, master)
 
		self.configure(width=360,height=360,background="#ffcc00")
 
 
		self.drawGrid()
 
 
		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):
 
				self.drawStone(r,c,point)
 
 
		self.grid()
 
 
	def drawGrid(self):
 
		for i in range(19):
 
			self.create_line(18,18*(i+1),360-18,18*(i+1),fill="#000000") # rows
 
			self.create_line(18*(i+1),18,18*(i+1),360-18,fill="#000000") # cols
 
 
@@ -152,14 +155,15 @@ class BoardView(tk.Canvas):
 
		elif color==Go.WHITE: hexCode='#ffffff'
 
		else: return False
 
		r+=1
 
		c+=1
 
		self.create_oval(c*18-9,r*18-9,c*18+9,r*18+9,fill=hexCode)
 
 
	def setBoardGrid(self,boardGrid):
 
		self.boardGrid=boardGrid
 
		self.detector.setGrid(boardGrid)
 
 
 
root = tk.Tk()
 
root.title("OneEye {0}.{1}".format(*config.misc.version))
 
app = Application(master=root)
 
app.mainloop()
src/image_analyzer.py
Show inline comments
 
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)
 
 
		for y in range(y1,y2+1):
 
			for x in range(x1,x2+1):
 
				red,green,blue=image.getpixel((x,y))
 
 
				I=(red+green+blue)/255/3
 
				m=min(red,green,blue)
 
				S=1-m/I
 
				if 100*I<tresB: b+=1
 
				elif 100*I>tresW: w+=1
 
				if 100*I<self.tresB: b+=1
 
				elif 100*I>self.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
 
 
	def relevantRect(self,imageCoords,stoneWidth,stoneHeight):
 
		x=int(imageCoords.x)
 
		y=int(imageCoords.y)
 
		xmax=max(int(stoneWidth*2//7), 2) # !! optimal parameters subject to further research
 
		ymax=max(int(stoneHeight*2//7), 2)
 
 
		return ((x-xmax,y-ymax), (x+xmax,y+ymax))
 
0 comments (0 inline, 0 general)