import os
import multiprocessing
import threading
import logging as log
import PIL
from util import MsgQueue
from gui import gui
from imageanalyzer import ImageAnalyzer
from go import Go
class Core:
def __init__(self):
self.grid=None
self.go=Go()
self.detector=ImageAnalyzer()
self.tresW=60.0
self.tresB=30.0
self._ownMessages=MsgQueue(self._handleEvent)
self._guiMessages=MsgQueue()
imgPath=os.path.join(os.path.dirname(__file__), "..","images","5.jpg")
self._frame=PIL.Image.open(imgPath)
self._guiProc=multiprocessing.Process(name="gui", target=gui, args=(self._guiMessages,self._ownMessages))
self._guiProc.start()
self._guiMessages.send("setCurrentFrame",(self._frame.copy(),))
def setCorners(self,corners):
self.detector.setGridCorners(corners)
self.detector.analyze(self._frame)
self._guiMessages.send("setGameState",(self.detector.board,))
def setTresholds(self,tresB=None,tresW=None):
if tresB is not None: self.tresB=tresB
if tresW is not None: self.tresW=tresW
if self.detector.analyze(self._frame):
self._guiMessages.send("setGameState",(self.detector.board,))
def listen(self):
listenerThread=threading.Thread(target=lambda: self._ownMessages.listen())
listenerThread.start()
def joinGui(self):
self._guiProc.join()
self._ownMessages.send("!kill")
def _handleEvent(self,e):
actions={"setCorners":self.setCorners, "setTresholds":self.setTresholds}
(actionName,args,kwargs)=e
return actions[actionName](*args,**kwargs)
core=Core()
core.listen()
core.joinGui()
"""
core
====
grid
go
imageAnalyzer
gui
===
corners
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)
core-gui: just pass messages with relevant data (!! always pass object copies, don't share instances)
"""