Files
@ c842bba99503
Branch filter:
Location: OneEye/src/gui.py
c842bba99503
5.4 KiB
text/x-python
rectangle representation of input intersections
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | import tkinter as tk
from PIL import ImageTk
import PIL
from corners import *
from grid import *
from image_analyzer import *
from go import *
showBigPoints=True
showGrid=False
class Application(tk.Frame):
def __init__(self, master=None):
self.corners=Corners()
self.boardGrid=None
tk.Frame.__init__(self, master)
self.grid(column=0,row=0)
self.createWidgets()
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
self.scaleTresB=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=lambda x: self.redrawImgView())
self.scaleTresW=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=lambda x: self.redrawImgView())
self.scaleTresB.set(30.0)
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.redrawImgView()
## Redraws the current image and its overlay.
def redrawImgView(self):
self.imgView.create_image(2,2,anchor="nw",image=self.img)
origWidth,origHeight=self.imgOrig.size
widthRatio=origWidth/self.img.width()
heightRatio=origHeight/self.img.height()
sizeCoef=max(widthRatio,heightRatio)
tresB=self.scaleTresB.get()
tresW=self.scaleTresW.get()
for corner in self.corners.corners:
self.markPoint(corner.x,corner.y)
if self.boardGrid!=None and 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 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)))
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,tresB,tresW)
## 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()
## Handles and presents the game state as detected by the program.
class BoardView(tk.Canvas):
def __init__(self, master=None):
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.
#
# @param tresB upper intensity treshold for a pixel to be considered black, [0-100]
# @param tresW lower intensity treshold for a pixel to be considered white, [0-100]
def redrawState(self,img,sizeCoef,shift,tresB,tresW):
self.create_rectangle(0,0,360,360,fill="#ffcc00")
self.drawGrid()
self.detector.analyze(img,tresB,tresW,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)
root = tk.Tk()
app = Application(master=root)
app.mainloop()
|