Changeset - 5fe83c3dfb92
[Not reviewed]
default
1 4 2
Laman - 8 years ago 2017-01-04 21:11:43

splitting core from gui
6 files changed with 182 insertions and 57 deletions:
0 comments (0 inline, 0 general)
src/config.py
Show inline comments
 
import logging
 

	
 

	
 
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.DEBUG)
 

	
 
class misc:
 
	version=(0,0)
 
	version=(0,0,0)
 

	
 
class gui:
 
	showBigPoints=True
 
	showGrid=False
src/core.py
Show inline comments
 
new file 100644
 
import multiprocessing
 
import PIL
 
from corners import Corners
 
from gui import gui
 

	
 

	
 
class Core:
 
	corners=Corners()
 
	tresW=60.0
 
	tresB=30.0
 

	
 
	@staticmethod
 
	def setCorners(corners):
 
		Core.corners=corners
 

	
 
	@staticmethod
 
	def setTresholds(tresB=None,tresW=None):
 
		if tresB is not None: Core.tresB=tresB
 
		if tresW is not None: Core.tresW=tresW
 

	
 

	
 
msgQueue=multiprocessing.Queue()
 
guiQueue=multiprocessing.Queue()
 
incomingEvent=multiprocessing.Event()
 
guiEvent=multiprocessing.Event()
 

	
 
frame=PIL.Image.open("../images/7.jpg")
 

	
 
guiProc=multiprocessing.Process(name="gui",target=gui,args=(guiQueue,guiEvent,msgQueue,incomingEvent))
 
guiProc.start()
 
guiQueue.put(("setCurrentFrame",(frame,None),dict()))
 
guiEvent.set()
 

	
 
while True:
 
	incomingEvent.wait()
 
	msg=msgQueue.get()
 
	if msgQueue.empty():
 
		incomingEvent.clear()
 
	print(msg)
 

	
 

	
 
# !! always pass object copies, don't share instances
 
"""
 
core
 
====
 
corners
 
grid
 
go
 
imageAnalyzer
 

	
 

	
 
gui
 
===
 
a) keeps references to important objects and uses them
 
b) gets and sets all relevant data through method calls with core
 

	
 
GUI
 
<- addCorner(corner)
 
-> redrawImgView(img,grid)
 
<- refreshTresholds(tresB,tresW)
 

	
 
BoardView
 
-> redrawState(go)
 
"""
 
\ No newline at end of file
src/corners.py
Show inline comments
 
from epoint import *
 
from epoint import EPoint
 
 
 
class Corners:
 
	def __init__(self):
 
		self.corners=[]
 
 
	## Adds a new corner if there are less than four, replaces the closest otherwise.
 
	def add(self,x,y):
 
		a=EPoint(x,y)
 
		# for i,c in enumerate(self.corners): # move an improperly placed point
 
			# if a.dist(c)<20:
 
				# self.corners[i]=a
 
				# return
 
 
		if len(self.corners)<4: # add a new corner
 
			self.corners.append(a)
 
 
		if len(self.corners)<4:
 
			return
 
 
		index,minDist=0,float('inf') # replace the corner closest to the clicked point
 
		for i,c in enumerate(self.corners):
 
			if a.dist(c)<minDist:
 
				index,minDist=i,a.dist(c)
 
 
		self.corners[index]=a
 
 
 
	## Computes twice the area of the triangle formed by points a,b,c.
 
	#
 
	#  @return positive value for points oriented counter-clockwise, negative for clockwise, zero for degenerate cases.
 
	def _doubleTriangleArea(a,b,c):
 
		return (a.x-b.x)*(c.y-a.y)-(c.x-a.x)*(a.y-b.y)
 
 
 
	def _slope(a,b):
 
		if(b.x==a.x): return float("inf")
 
		return (b.y-a.y)/(b.x-a.x)
 
 
 
	## Order the corners (0,1,2,3) so they make a quadrangle with vertices KLMN in counter-clockwise order, K being in the upper left.
 
	#
 
	#  For four points ABCD, there are 24 possible permutations corresponding to the desired KLMN.
 
	#  When we relax the condition of K being the upper left one, we get six groups of four equivalent permutations. KLMN ~ LMNK ~ MNKL ~ NKLM.
 
	#
 
	#  We determine which of the points' triplets are oriented clockwise and which counter-clockwise (minus/plus in the table below)
 
	#  and swap them so that all triangles turn counter-clockwise.
 
	#
 
	#  xxxx -> KLMN | ABC | ABD | ACD | BCD | index | swap
 
	#  ------------ | :-: | :-: | :-: | :-: | ----: | ----
 
	#  A BCD        |  +  |  +  |  +  |  +  |    15 | 0
 
	#  A BDC        |  +  |  +  |  -  |  -  |    12 | CD
 
	#  A CBD        |  -  |  +  |  +  |  -  |     6 | BC
 
	#  A CDB        |  -  |  -  |  +  |  +  |     3 | AB
 
	#  A DBC        |  +  |  -  |  -  |  +  |     9 | AD
 
	#  A DCB        |  -  |  -  |  -  |  -  |     0 | BD
 
	#
 
	#  For every non-degenerate quadrangle, there must be 1-3 edges going right-left (from a higher to a lower x coordinate).
 
	#  From these pick the one with the lowest slope (dy/dx) and declare its ending point the upper left corner. For the same slope pick the one further left.
 
	#
 
	#  @return True for a convex quadrangle, False for concave and degenerate cases.
 
	def canonizeOrder(self):
 
		if len(self.corners)!=4: return False # erroneus call
 
 
		a,b,c,d=self.corners
 
		abc=Corners._doubleTriangleArea(a,b,c)
 
		abd=Corners._doubleTriangleArea(a,b,d)
 
		acd=Corners._doubleTriangleArea(a,c,d)
 
		bcd=Corners._doubleTriangleArea(b,c,d)
 
 
		if any(x==0 for x in (abc,abd,acd,bcd)): return False # collinear degenerate
 
 
		swaps=[(1,3),(0,1),(1,2),(0,3),(2,3),(0,0)]
 
		index=(8 if abc>0 else 0)|(4 if abd>0 else 0)|(2 if acd>0 else 0)|(1 if bcd>0 else 0)
 
		if index%3!=0: return False # concave degenerate
 
		swap=swaps[index//3]
 
 
		self.corners[swap[0]], self.corners[swap[1]] = self.corners[swap[1]], self.corners[swap[0]] # counter-clockwise order
 
 
		kIndex=None
 
		lowestSlope=float("inf")
 
 
		for i,corner in enumerate(self.corners): # find the NK edge: going right-left with the lowest slope, secondarily the one going down
 
			ii=(i+1)%4
 
			slope=abs(Corners._slope(corner,self.corners[ii]))
 
			if corner.x>self.corners[ii].x and (slope<lowestSlope or (slope==lowestSlope and corner.y<self.corners[ii].y)):
 
				kIndex=ii
 
				lowestSlope=slope
 
 
		self.corners=self.corners[kIndex:]+self.corners[:kIndex] # rotate the upper left corner to the first place
 
 
		return True # success
src/grid.py
Show inline comments
 
import numpy
 
from epoint import *
 
from epoint import EPoint
 
 
 
## Projective transformation of a point with a matrix A.
 
#  
 
#  Takes a point as a horizontal vector and multiplies it transposed with A from left.
 
#  
 
#  @return transformed point as a numpy.array
 
def transformPoint(point,A):
 
	return (A*numpy.matrix(point).transpose()).getA1()
 
 
 
class Grid:
 
	## Creates a Grid from the provided Corners object.
 
	#
 
	#  Finds the vanishing points of the board lines (corner points define perspectively transformed parallel lines). The vanishing points define the image horizon.
 
	#
 
	#  The horizon can be used to construct a matrix for affine rectification of the image (restoring parallel lines parallelism). We transform the corner points by this matrix,
 
	#  interpolate them to get proper intersections' coordinates and then transform these back to get their placement at the original image.
 
	#
 
	#  The result is stored in grid.intersections, a boardSize*boardSize list with [row][column] coordinates.
 
	#
 
	#  @param corners a properly initialized Corners object. !! Needs a check for the proper initialization.
 
	def __init__(self,corners):
 
		# ad
 
		# bc
 
		a,b,c,d=[c.toProjective() for c in corners.corners]
 
 
		p1=numpy.cross(a,b)
 
		p2=numpy.cross(c,d)
 
		vanish1=numpy.cross(p1,p2)
 
		# !! 32 bit int can overflow. keeping it reasonably small. might want to use a cleaner solution
 
		vanish1=EPoint.fromProjective(vanish1).toProjective() # !! EPoint fails with point in infinity
 
 
		p3=numpy.cross(a,d)
 
		p4=numpy.cross(b,c)
 
		vanish2=numpy.cross(p3,p4)
 
		vanish2=EPoint.fromProjective(vanish2).toProjective()
 
 
		horizon=numpy.cross(vanish1,vanish2)
 
 
		horizon=EPoint.fromProjective(horizon).toProjective()
 
 
		rectiMatrix=numpy.matrix([horizon,[0,1,0],[0,0,1]])
 
		rectiMatrixInv=numpy.linalg.inv(rectiMatrix)
 
 
 
		affineCorners=[EPoint.fromProjective(transformPoint(x,rectiMatrix)) for x in (a,b,c,d)]
 
		x=[affineCorners[0]-affineCorners[3],affineCorners[1]-affineCorners[2],affineCorners[0]-affineCorners[1],affineCorners[3]-affineCorners[2]]
 
 
		self.intersections=[]
 
		boardSize=19
 
		for r in range(boardSize):
 
			self.intersections.append([None]*boardSize)
 
			rowStart=(affineCorners[0]*(boardSize-1-r)+affineCorners[1]*r) / (boardSize-1)
 
			rowEnd=(affineCorners[3]*(boardSize-1-r)+affineCorners[2]*r) / (boardSize-1)
 
 
			for c in range(boardSize):
 
				affineIntersection=(rowStart*(boardSize-1-c)+rowEnd*c) / (boardSize-1)
 
				self.intersections[r][c]=EPoint.fromProjective(transformPoint(affineIntersection.toProjective(),rectiMatrixInv))
 
 
	def stoneSizeAt(self,r,c,sizeCoef):
 
		intersection=self.intersections[r][c]
 
 
		if c>0: width=sizeCoef*(intersection.x-self.intersections[r][c-1].x)
 
		else: width=sizeCoef*(self.intersections[r][c+1].x-intersection.x)
 
		if r>0: height=sizeCoef*(intersection.y-self.intersections[r-1][c].y)
 
		else: height=sizeCoef*(self.intersections[r+1][c].y-intersection.y)
 
 
		return (width,height)
src/gui/__init__.py
Show inline comments
 
file renamed from src/gui.py to src/gui/__init__.py
 
import tkinter as tk
 
import threading
 
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 *
 
from corners import Corners
 
import image_analyzer
 
from go import Go
 
 
 
class Application(tk.Frame):
 
	def __init__(self, master=None):
 
class MainWindow(tk.Frame):
 
	def __init__(self,parent,master=None):
 
		self.parent=parent
 
		self.corners=Corners()
 
 
		self.currentFrame=None
 
		self.boardGrid=None
 
		self.gameState=None
 
 
		self.img=None
 
 
		tk.Frame.__init__(self, master)
 
		self.grid(column=0,row=0)
 
		self.createWidgets()
 
		self._createWidgets()
 
 
	def setCurrentFrame(self,frame,grid):
 
		self.currentFrame=frame
 
		self.boardGrid=grid
 
 
	def createWidgets(self):
 
		w=int(self.imgView['width'])
 
		h=int(self.imgView['height'])
 
		self.img=ImageTk.PhotoImage(frame.resize((w,h),resample=PIL.Image.BILINEAR))
 
 
	def setGameState(self,gameState):
 
		pass
 
 
	def setCallbacks(self,setCorners,setTresholds):
 
		self.cornersCallback=setCorners
 
		self.tresholdsCallback=setTresholds
 
 
	def _createWidgets(self):
 
		# a captured frame with overlay graphics
 
		self.imgView=tk.Canvas(self)
 
		self.imgView.configure(width=480,height=360)
 
		self.imgOrig=PIL.Image.open("../images/7.jpg")
 
 
		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
 
		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.scaleTresB=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=self.refreshTresholds)
 
		self.scaleTresW=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=self.refreshTresholds)
 
		self.scaleTresB.set(30.0) # !! proper defaults
 
		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():
 
			self.boardGrid=Grid(self.corners)
 
			self.boardView.setBoardGrid(self.boardGrid)
 
 
			self.parent.sendMsg("setCorners",(self.corners,))
 
		# 	self.boardGrid=Grid(self.corners)
 
		# 	self.boardView.setBoardGrid(self.boardGrid)
 
		#
 
		self.redrawImgView()
 
 
	## Redraws the current image and its overlay.
 
	def redrawImgView(self):
 
		if self.currentFrame and self.img:
 
		self.imgView.create_image(2,2,anchor="nw",image=self.img)
 
 
		origWidth,origHeight=self.imgOrig.size
 
			origWidth,origHeight=self.currentFrame.size
 
		widthRatio=origWidth/self.img.width()
 
		heightRatio=origHeight/self.img.height()
 
		sizeCoef=max(widthRatio,heightRatio)
 
		tresB=self.scaleTresB.get()
 
		tresW=self.scaleTresW.get()
 
 
			# shift=EPoint(origWidth-self.img.width()*sizeCoef,origHeight-self.img.height()*sizeCoef)/2
 
 
		for corner in self.corners.corners:
 
			self.markPoint(corner.x,corner.y)
 
 
		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 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)))
 
					((r1,c1),(r2,c2))=image_analyzer.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)
 
 
	## 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()
 
	def refreshTresholds(self,_):
 
		self.parent.sendMsg("setTresholds",tuple(),{"tresB":self.scaleTresB.get(), "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.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.
 
	def redrawState(self,img,sizeCoef,shift):
 
		self.create_rectangle(0,0,360,360,fill="#ffcc00")
 
		self.drawGrid()
 
 
		self.detector.analyze(img,sizeCoef,shift)
 
 
		for r,row in enumerate(self.detector.board):
 
	def redrawState(self,gameState):
 
		for r,row in enumerate(gameState):
 
			for c,point in enumerate(row):
 
				self.drawStone(r,c,point)
 
 
		self.grid()
 
 
	## Redraws and reananalyzes the board view.
 
	# def redrawState(self,img,sizeCoef,shift):
 
	# 	self.create_rectangle(0,0,360,360,fill="#ffcc00")
 
	# 	self.drawGrid()
 
	#
 
	# 	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
 
 
		self.drawStars()
 
 
	def drawStars(self):
 
		for r in range(4,19,6):
 
			for c in range(4,19,6):
 
				self.create_oval(r*18-2,c*18-2,r*18+2,c*18+2,fill='#000000')
 
 
	## Draws a stone at provided coordinates.
 
	#
 
	#  For an unknown color draws nothing and returns False.
 
	#
 
	#  @param r row coordinate, [0-18], counted from top
 
	#  @param c column coordinate, [0-18], counted from left
 
	#  @param color color indicator, Go.BLACK or Go.WHITE
 
	def drawStone(self,r,c,color):
 
		if color==Go.BLACK: hexCode='#000000'
 
		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)
 
 
class GUI:
 
	def __init__(self):
 
		self.root = tk.Tk()
 
		self.root.title("OneEye {0}.{1}.{2}".format(*config.misc.version))
 
 
		self.outcomingQueue=None
 
		self.outcomingEvent=None
 
 
		self.mainWindow = MainWindow(self,master=self.root)
 
 
		self.root.bind("<<redrawImgView>>", lambda e: self.mainWindow.redrawImgView())
 
		self.root.bind("<<receiveState>>", lambda e: print("fired receiveState"))
 
 
	def __call__(self,incomingQueue,incomingEvent,outcomingQueue,outcomingEvent):
 
		self.outcomingQueue=outcomingQueue
 
		self.outcomingEvent=outcomingEvent
 
 
		self.listenerThread=threading.Thread(target=lambda: self._listen(incomingQueue,incomingEvent))
 
		self.listenerThread.start()
 
 
		self.mainWindow.mainloop()
 
 
root = tk.Tk()
 
root.title("OneEye {0}.{1}".format(*config.misc.version))
 
app = Application(master=root)
 
app.mainloop()
 
	def sendMsg(self,actionName,args=tuple(),kwargs=dict()):
 
		self.outcomingQueue.put((actionName,args,kwargs))
 
		self.outcomingEvent.set()
 
 
	def _listen(self,incomingQueue,incomingEvent):
 
		while True:
 
			incomingEvent.wait()
 
			msg=incomingQueue.get()
 
			if incomingQueue.empty():
 
				incomingEvent.clear()
 
			print(msg)
 
			self._handleEvent(msg)
 
 
	def _handleEvent(self,e):
 
		actions={"setCurrentFrame": self._frameHandler}
 
		(actionName,args,kwargs)=e
 
 
		return actions[actionName](*args,**kwargs)
 
 
	def _frameHandler(self,newFrame,grid):
 
		self.mainWindow.setCurrentFrame(newFrame,grid)
 
		self.root.event_generate("<<redrawImgView>>")
 
 
	def _stateHandler(self,e):
 
		pass
 
 
gui=GUI()
src/image_analyzer.py
Show inline comments
 
import logging
 
from go import *
 
from go import Go
 
 
 
class ImageAnalyzer:
 
 
	def __init__(self,tresB=30,tresW=60):
 
		self.board=[[Go.EMPTY]*19 for r in range(19)]
 
		self.grid=None
 
 
		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,r,c,intersection*sizeCoef+shift,*(self.grid.stoneSizeAt(r,c,sizeCoef)))
 
 
	def analyzePoint(self,image,row,col,imageCoords,stoneWidth,stoneHeight):
 
		b=w=e=0
 
 
		((x1,y1),(x2,y2))=self.relevantRect(imageCoords,stoneWidth,stoneHeight)
 
		((x1,y1),(x2,y2))=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<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):
 
	def setGrid(self,grid):
 
		self.grid=grid
 
 
 
def relevantRect(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))
 
 
	def setGrid(self,grid):
 
		self.grid=grid
0 comments (0 inline, 0 general)