Files @ 8af597b8f819
Branch filter:

Location: OneEye/src/core.py - annotation

Laman
further refactoring, got processes passing messages, restored computing grid and analyzing image
import multiprocessing
import logging as log
import PIL
from gui import gui
from image_analyzer 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._msgQueue=multiprocessing.Queue()
		self._guiQueue=multiprocessing.Queue()
		self._incomingEvent=multiprocessing.Event()
		self._guiEvent=multiprocessing.Event()

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

		self._guiProc=multiprocessing.Process(name="gui", target=gui, args=(self._guiQueue,self._guiEvent,self._msgQueue,self._incomingEvent))
		self._guiProc.start()
		self._guiQueue.put(("setCurrentFrame",(self._frame,),dict()))
		self._guiEvent.set()

	def setCorners(self,corners):
		self.detector.setGridCorners(corners)
		self.detector.analyze(self._frame)
		for r in self.detector.board: log.info(r)

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

	def listen(self):
		while True:
			self._incomingEvent.wait()
			msg=self._msgQueue.get()
			if self._msgQueue.empty():
				self._incomingEvent.clear()
			log.info(msg)
			self._handleEvent(msg)

	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
====
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)
"""