Files @ 176c678923b7
Branch filter:

Location: OneEye/src/core.py

Laman
get stones samples
import os
import multiprocessing
import threading
import logging

import PIL

from util import MsgQueue
from gui import gui
from analyzer import ImageAnalyzer
from analyzer.framecache import FrameCache
from go.core import Go, isLegalPosition
from statebag import StateBag
import config as cfg
import util

log=logging.getLogger(__name__)


class Core:
	def __init__(self):
		self.grid=None
		self.go=Go()
		self.detector=ImageAnalyzer()
		self._cache=FrameCache()
		self.states=StateBag()

		self._ownMessages=MsgQueue(self._handleEvent)
		self._guiMessages=MsgQueue()

		self._imgs=sorted(filter(lambda s: s.endswith(".jpg"), os.listdir(cfg.misc.imgDir)))
		self._imgIndex=cfg.misc.defaultImage
		imgPath=os.path.join(cfg.misc.imgDir,self._imgs[self._imgIndex])
		self._frame=PIL.Image.open(imgPath)

		self._guiProc=multiprocessing.Process(name="gui", target=gui, args=(self._guiMessages,self._ownMessages))
		self._guiProc.start()
		self.relativeFrame(0)

	def setCorners(self,corners):
		self.detector.setGridCorners(corners)
		self.analyze()

	def setTresholds(self,tresB=None,tresW=None):
		if tresB is not None: self.detector.tresB=tresB
		if tresW is not None: self.detector.tresW=tresW
		self.preview()

	def relativeFrame(self,step):
		self._imgIndex=(self._imgIndex+step)%len(self._imgs)
		filename=self._imgs[self._imgIndex]
		imgPath=os.path.join(cfg.misc.imgDir,filename)
		self._frame=PIL.Image.open(imgPath)
		self._guiMessages.send("setCurrentFrame",(self._frame.copy(),gui.PREVIEW,filename))
		self.preview()

	def sendParams(self):
		params={
			"tresB": self.detector.tresB,
			"tresW": self.detector.tresW
		}
		self._guiMessages.send("setParams",tuple(),params)

	def setParams(self,tresB=0,tresW=0):
		self.detector.tresB=tresB
		self.detector.tresW=tresW

	def preview(self):
		if self.detector.analyze(self._frame):
			self._guiMessages.send("setGameState", (self.detector.board,[]))

	def analyze(self):
		if self.detector.analyze(self._frame):
			self._cache.put(self._frame)
			if isLegalPosition(self.detector.board):
				state=self.states.pushState(self.detector.board)
				rec=[]
				if state:
					rec=state.exportRecord()
					log.debug("progressive game record: %s",rec)
				self._guiMessages.send("setGameState", (self.detector.board,rec))

				self.go.transitionMove(self.detector.board)
				log.debug("conservative game record: %s",self.go._record)
			else:
				log.info("illegal position detected")

	def listen(self):
		listenerThread=threading.Thread(target=lambda: self._ownMessages.listen())
		listenerThread.start()

	def joinGui(self):
		self._guiProc.join()
		self._ownMessages.send("!kill",("core",))

	def _handleEvent(self,e):
		actions={
			"setCorners": self.setCorners,
			"setTresholds": self.setTresholds,
			"prevFrame": lambda: self.relativeFrame(-1),
			"nextFrame": lambda: self.relativeFrame(1),
			"fetchParams": self.sendParams,
			"setParams": self.setParams
		}
		(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)
"""