Changeset - 5a1d87ee0f8a
[Not reviewed]
default
0 4 1
Laman - 6 years ago 2018-12-07 16:58:56

splitting setup mode and recording mode
5 files changed with 86 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/core.py
Show inline comments
 
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
 

	
 
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(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.analyze()
 

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

	
 
	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)
 
		}
 
		(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)
 
"""
 
\ No newline at end of file
src/gui/__init__.py
Show inline comments
 
import threading
 
import tkinter as tk
 

	
 
import config
 
from .mainwindow import MainWindow
 
from .boardview import BoardView
 
from .settings import Settings
 

	
 

	
 
class GUI:
 
	SETUP=PREVIEW=0
 
	RECORDING=REAL=1
 

	
 
	def __init__(self):
 
		self.root = tk.Tk()
 
		self.root.title("OneEye {0}.{1}.{2}".format(*config.misc.version))
 
		self.root.option_add('*tearOff',False) # for menu
 

	
 
		self._ownMessages=None
 
		self._coreMessages=None
 

	
 
		self._state=GUI.SETUP
 

	
 
		self.mainWindow = MainWindow(self, master=self.root)
 
		self.root.columnconfigure(0,weight=1)
 
		self.root.rowconfigure(0,weight=1)
 

	
 
		self.root.bind("<<redrawImgView>>", lambda e: self.mainWindow.redrawImgView())
 
		self.root.bind("<Left>",lambda e: self.sendMsg("prevFrame"))
 
		self.root.bind("<Right>",lambda e: self.sendMsg("nextFrame"))
 
		self.root.bind("<<setUp>>", lambda e: self.setUp())
 
		self.root.bind("<<setRecording>>", lambda e: self.setRecording())
 
		self.root.bind("<F12>",lambda e: Settings(self))
 
		self.mainWindow.bind("<Destroy>",lambda e: self._ownMessages.send("!kill",("gui",)))
 

	
 
		self.setUp()
 

	
 
	def __call__(self,ownMessages,coreMessages):
 
		self._ownMessages=ownMessages
 
		self._coreMessages=coreMessages
 

	
 
		self.listenerThread=threading.Thread(target=lambda: ownMessages.listen(self._handleEvent))
 
		self.listenerThread.start()
 

	
 
		self.mainWindow.mainloop()
 

	
 
	def sendMsg(self,actionName,args=tuple(),kwargs=None):
 
		self._coreMessages.send(actionName,args,kwargs)
 

	
 
	def setUp(self):
 
		self.mainWindow.setUp()
 
		self.root.bind("<Left>",lambda e: self.sendMsg("prevFrame"))
 
		self.root.bind("<Right>",lambda e: self.sendMsg("nextFrame"))
 

	
 
	def setRecording(self):
 
		self.mainWindow.setRecording()
 
		self.root.bind("<Left>",lambda e: None)
 
		self.root.bind("<Right>",lambda e: None)
 

	
 
	def _handleEvent(self,e):
 
		actions={"setCurrentFrame":self._frameHandler, "setGameState":self._stateHandler}
 
		(actionName,args,kwargs)=e
 

	
 
		return actions[actionName](*args,**kwargs)
 

	
 
	def _frameHandler(self,newFrame):
 
	def _frameHandler(self,newFrame,type):
 
		if self._state!=type: return
 
		self.mainWindow.setCurrentFrame(newFrame)
 
		self.root.event_generate("<<redrawImgView>>")
 

	
 
	def _stateHandler(self,gameState,moves):
 
		labels={(row,col):(i+1) for (i,(c,row,col)) in enumerate(moves)}
 
		self.mainWindow.boardView.redrawState(gameState,labels)
 

	
 
gui=GUI()
 

	
 
"""
 
# setup #
 
* we can click around the ImgView
 
* we can walk through the frames back and forth
 
* BoardView is showing what the reading of ImgView _would_ be
 
* core is reading and analyzing frames, pushing results to StateBag, but not showing them
 

	
 
# recording #
 
* ImgView is showing the current picture, is not clickable
 
* BoardView is showing last detected position
 
* on switch to recording (if parameters have changed):
 
	* feed analyzer new parameters and start using them
 
	* in the background reanalyze cached frames with the new parameters and merge them into StateBag
 
"""
src/gui/imgview.py
Show inline comments
 
import logging
 

	
 
from PIL import ImageTk
 

	
 
import config
 
from .resizablecanvas import ResizableCanvas
 
from analyzer.corners import Corners
 
from analyzer.epoint import EPoint
 
from analyzer.grid import Grid
 
import analyzer
 

	
 
log=logging.getLogger(__name__)
 

	
 

	
 
class ImgView(ResizableCanvas):
 
	def __init__(self,master=None,parent=None):
 
		super().__init__(master)
 

	
 
		self._parent=parent
 
		self._corners=Corners()
 
		self._boardGrid=None
 

	
 
		self._img=None
 
		self._tkImg=None
 

	
 
		self.configure(width=480,height=360)
 
		self.bind('<1>',lambda e: self.addCorner(e.x,e.y))
 

	
 
	## Redraws the current image and its overlay.
 
	def redraw(self):
 
		self.delete("all")
 

	
 
		if self._img:
 
			img=self._img.resize((int(self._width),int(self._height)))
 
			self._tkImg=ImageTk.PhotoImage(img) # just to save the image from garbage collector
 
			self.create_image(self._width//2, self._height//2, anchor="center", image=self._tkImg)
 

	
 
		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.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.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))=analyzer.relevantRect(self._boardGrid.intersections[r][c], *(self._boardGrid.stoneSizeAt(r, c)))
 
					self.create_rectangle(r1,c1,r2,c2,outline="#00ffff")
 

	
 
	def setImg(self,img):
 
		self._img=img
 

	
 
	## Stores a grid corner located at x,y coordinates.
 
	def addCorner(self,x,y):
 
		self._corners.add(x,y)
 
		log.debug("click on %d,%d",x,y)
 
		if self._corners.canonizeOrder():
 
			# transform corners from show coordinates to real coordinates
 
			log.debug(self._corners.corners)
 
			self._boardGrid=Grid(self._corners.corners)
 
			corners=[self._transformPoint(c) for c in self._corners.corners]
 
			self._parent.sendMsg("setCorners",(corners,))
 

	
 
		self.redraw()
 

	
 
	## Marks a point at the image with a green cross. Used for corners.
 
	def markPoint(self,x,y):
 
		self.create_line(x-3,y-3,x+4,y+4,fill="#00ff00")
 
		self.create_line(x-3,y+3,x+4,y-4,fill="#00ff00")
 

	
 
	def setUp(self):
 
		self.bind('<1>',lambda e: self.addCorner(e.x,e.y))
 

	
 
	def setRecording(self):
 
		self.bind('<1>',lambda e: None)
 

	
 
	def _onResize(self,event):
 
		w=self._width
 
		super()._onResize(event)
 
		self._corners.scale(self._width/w)
 
		if len(self._corners.corners)==4:
 
			self._boardGrid=Grid(self._corners.corners)
 
		self.redraw()
 

	
 
	def _transformPoint(self,point):
 
		w=int(self._width)
 
		h=int(self._height)
 
		wo,ho=self._img.size # o for original
 
		widthRatio=wo/w
 
		heightRatio=ho/h
 
		self._imgSizeCoef=max(widthRatio,heightRatio)
 
		# shift compensates possible horizontal or vertical empty margins from unmatching aspect ratios
 
		self._imgShift=EPoint(wo-w*self._imgSizeCoef,ho-h*self._imgSizeCoef)/2
 
		return EPoint(self.canvasx(point.x),self.canvasy(point.y)) * self._imgSizeCoef + self._imgShift
src/gui/mainwindow.py
Show inline comments
 
import tkinter as tk
 
from tkinter import N,S,E,W
 

	
 
from .util import MsgMixin
 
from .menu import MainMenu
 
from .boardview import BoardView
 
from .imgview import ImgView
 
from .statusbar import StatusBar
 

	
 

	
 
class MainWindow(tk.Frame,MsgMixin):
 
	def __init__(self,parent,master=None):
 
		self.parent=parent
 

	
 
		tk.Frame.__init__(self, master)
 
		self.grid(column=0,row=0,sticky=(N,S,E,W))
 
		self._createWidgets()
 

	
 
	def setCurrentFrame(self,frame):
 
		self.imgView.setImg(frame)
 

	
 
	def _createWidgets(self):
 
		# menu
 
		self.parent.root.option_add('*tearOff',False)
 
		self._menu=MainMenu(self.parent,self.parent.root)
 

	
 
		# a captured frame with overlay graphics
 
		self._imgWrapper=tk.Frame(self,width=480,height=360)
 
		self.imgView=ImgView(self._imgWrapper,self)
 

	
 
		self._imgWrapper.grid(column=0,row=0,sticky=(N,S,E,W))
 

	
 
		# board with detected stones
 
		self._boardWrapper=tk.Frame(self,width=360,height=360)
 
		self.boardView=BoardView(self._boardWrapper)
 
		self._boardWrapper.grid(column=1,row=0,sticky=(N,S,E,W))
 

	
 
		self.columnconfigure(0,weight=1)
 
		self.columnconfigure(1,weight=1)
 
		self.rowconfigure(0,weight=1)
 

	
 
		self._statusBar=StatusBar(self)
 
		self._statusBar.grid(column=0,row=1,columnspan=2,sticky=(S,E,W))
 

	
 
		self.rowconfigure(1,weight=0)
 

	
 
		# render everything
 
		self.imgView.redraw()
 

	
 
	## Redraws the current image and its overlay.
 
	def redrawImgView(self):
 
		self.imgView.redraw()
 

	
 
	def setUp(self):
 
		self._statusBar.setUp()
 
		self.imgView.setUp()
 

	
 
	def setRecording(self):
 
		self._statusBar.setRecording()
 
		self.imgView.setRecording()
src/gui/statusbar.py
Show inline comments
 
new file 100644
 
import tkinter as tk
 
from tkinter import LEFT,DISABLED,NORMAL
 

	
 

	
 
class StatusBar(tk.Frame):
 
	def __init__(self,parent):
 
		super().__init__(parent,width=480,height=20,borderwidth=1,relief="sunken")
 
		self._parent=parent
 
		self._createWidgets()
 

	
 
	def _createWidgets(self):
 
		self._setupButton=tk.Button(self,text="[] Set up",command=lambda: self.event_generate("<<setUp>>"),state=DISABLED)
 
		self._setupButton.pack(side=LEFT)
 

	
 
		self._recordButton=tk.Button(self,text="> Record",command=lambda: self.event_generate("<<setRecording>>"))
 
		self._recordButton.pack(side=LEFT)
 

	
 
	def setUp(self):
 
		self._setupButton.config(state=DISABLED)
 
		self._recordButton.config(state=NORMAL)
 

	
 
	def setRecording(self):
 
		self._setupButton.config(state=NORMAL)
 
		self._recordButton.config(state=DISABLED)
0 comments (0 inline, 0 general)