Changeset - c842bba99503
[Not reviewed]
default
0 9 1
Laman - 8 years ago 2016-11-23 22:18:22

rectangle representation of input intersections
10 files changed with 80 insertions and 22 deletions:
0 comments (0 inline, 0 general)
.hgignore
Show inline comments
 
modified file chmod 100644 => 100755
 
^src/__pycache__/
 
^\.idea/
 
\ No newline at end of file
 
^\.idea/
 
^images/
license.txt
Show inline comments
 
modified file chmod 100644 => 100755
project.md
Show inline comments
 
new file 100755
 
OneEye
 
======
 
 
Program to extract moves of a go game from video and save them or broadcast online.
 
 
Modules:
 
  - Video: grabbing still frames from a video file / stream. Using FFmpeg.
 
  - Graphic: extracting game position from an image. Using Pillow.
 
  - Watcher: interpreting sequence of game positions as a sequence of moves.
 
  - Broadcaster: interfacing with a go server.
 
 
 
Graphic
 
-------
 
Interpolating the board grid from specified corner points. Using vanishing points and horizon in projective geometry.
 
 
Determining the point status based on majority voting of pixels in its neighbourhood (deciding by HSI intensity).
 
 
Autodetection of the board?
 
 
 
Watcher
 
-------
 
Base case: we have two correctly recognized positions differing by a single move (single added stone). Easy.
 
 
Issues:
 
  - illegal positions -> ignorable
 
  - positions unreachable from the previous state
 
    - reachable from any past state. (Incorrect states inbetween). How to pick the correct leaf of such a tree?
 
    - reachable by more than one move. Issues with branching factor.
 
  - board shifts -> repaired manually (or automatically), further positions have to be reevaluated
 
  - stone shifts
 
    - stone stops being recognized -> fixable manually and even ignorable
 
    - stone is recognized at an empty intersection. It can be occupied later for real. What do?
 
readme.md
Show inline comments
 
modified file chmod 100644 => 100755
src/corners.py
Show inline comments
 
modified file chmod 100644 => 100755
src/epoint.py
Show inline comments
 
modified file chmod 100644 => 100755
src/go.py
Show inline comments
 
modified file chmod 100644 => 100755
src/grid.py
Show inline comments
 
modified file chmod 100644 => 100755
 
@@ -60,6 +60,15 @@ class Grid:
 
        affineIntersection=(rowStart*(boardSize-1-c)+rowEnd*c) / (boardSize-1)
 
        self.intersections[r][c]=EPoint.fromProjective(transformPoint(affineIntersection.toProjective(),rectiMatrixInv))
 
 
  def stoneSizeAt(self,r,c,sizeCoef):
 
    intersection=self.intersections[r][c]
 
 
    if c>0: width=sizeCoef*(intersection.x-self.intersections[r][c-1].x)
 
    else: width=sizeCoef*(self.intersections[r][c+1].x-intersection.x)
 
    if r>0: height=sizeCoef*(intersection.y-self.intersections[r-1][c].y)
 
    else: height=sizeCoef*(self.intersections[r+1][c].y-intersection.y)
 
 
    return (width,height)
 
 
 
# from corners import Corners
src/gui.py
Show inline comments
 
modified file chmod 100644 => 100755
 
import tkinter as tk
 
from PIL import ImageTk
 
import PIL
 
import math
 
from epoint import *
 
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):
 
@@ -57,10 +58,18 @@ class Application(tk.Frame):
 
  ## 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:
 
    if self.boardGrid!=None and showGrid:
 
      for r in range(19):
 
        a=self.boardGrid.intersections[r][0]
 
        b=self.boardGrid.intersections[r][-1]
 
@@ -70,14 +79,13 @@ class Application(tk.Frame):
 
        b=self.boardGrid.intersections[-1][c]
 
        self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
 
    
 
    self.imgView.grid()
 
    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")
 
    
 
    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()
 
    self.imgView.grid()
 
    
 
    shift=EPoint(origWidth-self.img.width()*sizeCoef,origHeight-self.img.height()*sizeCoef)/2
 
    
 
@@ -109,6 +117,9 @@ class BoardView(tk.Canvas):
 
  # @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):
src/image_analyzer.py
Show inline comments
 
modified file chmod 100644 => 100755
 
@@ -14,22 +14,16 @@ class ImageAnalyzer:
 
      for c in range(19):
 
        intersection=self.grid.intersections[r][c]
 
        
 
        if c>0: stoneWidth=sizeCoef*(intersection.x-self.grid.intersections[r][c-1].x)
 
        else: stoneWidth=sizeCoef*(self.grid.intersections[r][c+1].x-intersection.x)
 
        if r>0: stoneHeight=sizeCoef*(intersection.y-self.grid.intersections[r-1][c].y)
 
        else: stoneHeight=sizeCoef*(self.grid.intersections[r+1][c].y-intersection.y)
 
        
 
        self.board[r][c]=self.analyzePoint(image,intersection*sizeCoef+shift,stoneWidth,stoneHeight,tresB,tresW)
 
        self.board[r][c]=self.analyzePoint(image,intersection*sizeCoef+shift,*(self.grid.stoneSizeAt(r,c,sizeCoef)),tresB,tresW)
 
  
 
  def analyzePoint(self,image,imageCoords,stoneWidth,stoneHeight,tresB,tresW):
 
    b=w=e=0
 
    
 
    cmax=max(int(stoneWidth*2//7),2) # !! optimal parameters subject to further research
 
    rmax=max(int(stoneHeight*2//7),2)
 
    ((x1,y1),(x2,y2))=self.relevantRect(imageCoords,stoneWidth,stoneHeight)
 
    
 
    for r in range(-rmax,rmax+1):
 
      for c in range(-cmax,cmax+1):
 
        red,green,blue=image.getpixel((imageCoords.x+c,imageCoords.y+r))
 
    for y in range(y1,y2+1):
 
      for x in range(x1,x2+1):
 
        red,green,blue=image.getpixel((x,y))
 
        
 
        I=(red+green+blue)/255/3
 
        m=min(red,green,blue)
 
@@ -42,5 +36,13 @@ class ImageAnalyzer:
 
    if w>=b and w>=e: return Go.WHITE
 
    return Go.EMPTY
 
    
 
  def relevantRect(self,imageCoords,stoneWidth,stoneHeight):
 
    x=int(imageCoords.x)
 
    y=int(imageCoords.y)
 
    xmax=max(int(stoneWidth*2//7), 2) # !! optimal parameters subject to further research
 
    ymax=max(int(stoneHeight*2//7), 2)
 
 
    return ((x-xmax,y-ymax), (x+xmax,y+ymax))
 
    
 
  def setGrid(self,grid):
 
    self.grid=grid
0 comments (0 inline, 0 general)