SGF output for multiple images
authorTomas Musil <tomik.musil@gmail.com>
Thu, 17 Oct 2013 23:14:40 +0000 (01:14 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Thu, 17 Oct 2013 23:14:40 +0000 (01:14 +0200)
TODO
imago_pack/imago.py
imago_pack/output.py

diff --git a/TODO b/TODO
index f74b253..3c4e5ad 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,4 +1,7 @@
 documentation
 documentation
+better color clustering
+auto-calibration (too much new stones -> smaller clusters)
+use information from previous board
 faster (and more realiable) grid search
 stone finding
 better user interface
 faster (and more realiable) grid search
 stone finding
 better user interface
index e2c2876..7c6c1f2 100755 (executable)
@@ -18,6 +18,7 @@ import linef
 import manual
 import intrsc
 import gridf
 import manual
 import intrsc
 import gridf
+import output
 
 def argument_parser():
     parser = argparse.ArgumentParser(description=__doc__)
 
 def argument_parser():
     parser = argparse.ArgumentParser(description=__doc__)
@@ -111,17 +112,25 @@ def main():
             print board
     
     else:
             print board
     
     else:
+        game = output.Game(19, board) #TODO size parameter
         for f in args.files[1:]:
             try:
                 image = Image.open(f)
             except IOError, msg:
                 print >> sys.stderr, msg
                 continue
         for f in args.files[1:]:
             try:
                 image = Image.open(f)
             except IOError, msg:
                 print >> sys.stderr, msg
                 continue
+            if verbose:
+                print >> sys.stderr, "Opening", f
             if image.mode == 'P':
                 image = image.convert('RGB')
             board = intrsc.board(image, lines, show_all, do_something)
             if image.mode == 'P':
                 image = image.convert('RGB')
             board = intrsc.board(image, lines, show_all, do_something)
-            for line in board:
-                print ' '.join(line)
+            if args.sgf_output:
+                game.addMove(board)
+            else:
+                print board
+
+        if args.sgf_output:
+            print game.asSGF()
 
     return 0
 
 
     return 0
 
@@ -144,5 +153,5 @@ if __name__ == '__main__':
     try:
         sys.exit(main())
     except KeyboardInterrupt: #TODO does this work?
     try:
         sys.exit(main())
     except KeyboardInterrupt: #TODO does this work?
-        print "Interrupted."
+        print >> sys.stderr, "Interrupted."
         sys.exit(1)
         sys.exit(1)
index ba43ebd..bc13280 100644 (file)
@@ -23,6 +23,13 @@ class Board:
     def asSGFsetPos(self):
         """Returns SGF (set position) representation of the position."""
 
     def asSGFsetPos(self):
         """Returns SGF (set position) representation of the position."""
 
+        #TODO version numbering
+        sgf = "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
+        sgf += self.SGFpos()
+        sgf += ")"
+        return sgf
+
+    def SGFpos(self):
         black = []
         white = []
 
         black = []
         white = []
 
@@ -33,22 +40,44 @@ class Board:
                     black.append((i, j))
                 elif stone == 'W':
                     white.append((i, j))
                     black.append((i, j))
                 elif stone == 'W':
                     white.append((i, j))
-
         sgf = ""
         sgf = ""
-        #TODO version numbering
-        sgf += "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
-        sgf += "AB" + ''.join('[' + COORDS[j] + COORDS[i] + ']'
+        if len(black) > 0:
+            sgf += "AB" + ''.join('[' + COORDS[j] + COORDS[i] + ']'
                               for (i, j) in black) + "\n"
                               for (i, j) in black) + "\n"
-        sgf += "AW" + ''.join('[' + COORDS[j] + COORDS[i] + ']' 
+        if len(white) > 0:
+            sgf += "AW" + ''.join('[' + COORDS[j] + COORDS[i] + ']' 
                               for (i, j) in white) + "\n"
                               for (i, j) in white) + "\n"
-        sgf += ")"
         return sgf
 
         return sgf
 
+    def getMove(self, board):
+        for i in range(self.size):
+            for j in range(self.size):
+                if (self.stones[self.size * i + j] == "."):
+                    if (board.stones[self.size * i + j] == "W"):
+                        return ("W", COORDS[i] + COORDS[j])
+                    elif (board.stones[self.size * i + j] == "B"):
+                        return ("B", COORDS[i] + COORDS[j])
+        return None
+
+
 class Game:
 class Game:
-    def __init__(self, size):
-        self.board =  (size * size) * "."
+    def __init__(self, size, board=None):
+        self.init_board = board or Board(size, (size * size) * ".")
+        self.board = self.init_board
         self.moves = []
         self.moves = []
+        self.size = size
+
+    def addMove(self, board):
+        self.moves.append(self.board.getMove(board))
+        self.board = board
+
+    def asSGF(self):
+        sgf = "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
+        sgf += self.init_board.SGFpos()
+        for m in self.moves:
+            if m:
+                sgf += ";" + m[0] + "[" + m[1] + "]\n"
+        sgf += ")"
+        return sgf
 
 
-    def add(self, board):
-        self