more comments
[imago.git] / imago_pack / imago.py
index e2c2876..33e6181 100755 (executable)
@@ -1,6 +1,9 @@
 #!/usr/bin/env python
 
 #!/usr/bin/env python
 
-"""Go image recognition."""
+"""Go image recognition.
+
+This is the main UI module of Imago.
+"""
 
 import sys
 import os
 
 import sys
 import os
@@ -18,6 +21,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__)
@@ -42,6 +46,7 @@ def argument_parser():
     return parser
  
 
     return parser
  
 
+# TODO factor this into smaller functions
 def main():
     """Main function of the program."""
     
 def main():
     """Main function of the program."""
     
@@ -81,16 +86,15 @@ def main():
             if os.path.exists(filename):
                 lines, l1, l2, bounds, hough = pickle.load(open(filename))
                 print >> sys.stderr, "using cached results"
             if os.path.exists(filename):
                 lines, l1, l2, bounds, hough = pickle.load(open(filename))
                 print >> sys.stderr, "using cached results"
-                im_h = None
             else:
             else:
-                lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
+                lines, l1, l2, bounds, hough = linef.find_lines(image, show_all, do_something, verbose)
                 if not os.path.isdir(cache_dir):
                     os.makedirs(cache_dir)
                 d_file = open(filename, 'wb')
                 pickle.dump((lines, l1, l2, bounds, hough), d_file)
                 d_file.close()
         else:
                 if not os.path.isdir(cache_dir):
                     os.makedirs(cache_dir)
                 d_file = open(filename, 'wb')
                 pickle.dump((lines, l1, l2, bounds, hough), d_file)
                 d_file.close()
         else:
-            lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
+            lines, l1, l2, bounds, hough = linef.find_lines(image, show_all, do_something, verbose)
 
         grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough,
                                  show_all, do_something)
 
         grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough,
                                  show_all, do_something)
@@ -111,17 +115,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 +156,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)