Skip to content

Commit 255138f

Browse files
committed
wrap prints in parentheses
1 parent a074532 commit 255138f

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

tensorflow_chessbot.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,17 @@ def loadImage(img_file):
229229
def resizeAsNeeded(img):
230230
"""Resize if image larger than 2k pixels on a side"""
231231
if img.size[0] > 2000 or img.size[1] > 2000:
232-
print "Image too big (%d x %d)" % (img.size[0], img.size[1])
232+
print("Image too big (%d x %d)" % (img.size[0], img.size[1]))
233233
new_size = 500.0 # px
234234
if img.size[0] > img.size[1]:
235235
# resize by width to new limit
236236
ratio = new_size / img.size[0]
237237
else:
238238
# resize by height
239239
ratio = new_size / img.size[1]
240-
print "Reducing by factor of %.2g" % (1./ratio)
240+
print("Reducing by factor of %.2g" % (1./ratio))
241241
img = img.resize(img.size * ratio, PIL.Image.ADAPTIVE)
242-
print "New size: (%d x %d)" % (img.size[0], img.size[1])
242+
print("New size: (%d x %d)" % (img.size[0], img.size[1]))
243243
return img
244244

245245
def getTiles(img_arr):
@@ -284,15 +284,15 @@ def getTiles(img_arr):
284284
if is_match:
285285
break
286286
else:
287-
print "Trying %d%% of threshold" % (100*percentage)
287+
print("Trying %d%% of threshold" % (100*percentage))
288288
lines_x, lines_y, is_match = getChessLines(hdx, hdy,
289289
hdx_thresh * percentage, hdy_thresh * percentage)
290290

291291
# Get the tileset
292292
if is_match:
293293
return getChessTiles(img_arr, lines_x, lines_y)
294294
else:
295-
print "\tNo Match, lines found (dx/dy):", lines_x, lines_y
295+
print("\tNo Match, lines found (dx/dy):", lines_x, lines_y)
296296
return [] # No match, no tiles
297297

298298
def saveTiles(tiles, img_save_dir, img_file):
@@ -328,44 +328,44 @@ def generateTileset(input_chessboard_folder, output_tile_folder):
328328
num_skipped = 0
329329

330330
for i, img_path in enumerate(img_files):
331-
print "#% 3d/%d : %s" % (i+1, len(img_files), img_path)
331+
print("#% 3d/%d : %s" % (i+1, len(img_files), img_path))
332332
# Strip to just filename
333333
img_file = img_path[len(input_chessboard_folder)+1:-4]
334334

335335
# Create output save directory or skip this image if it exists
336336
img_save_dir = "%s/tiles_%s" % (output_tile_folder, img_file)
337337

338338
if os.path.exists(img_save_dir):
339-
print "\tSkipping existing"
339+
print("\tSkipping existing")
340340
num_skipped += 1
341341
continue
342342

343343
# Load image
344-
print "---"
345-
print "Loading %s..." % img_path
344+
print("---")
345+
print("Loading %s..." % img_path)
346346
img_arr = loadImage(img_path)
347347

348348
# Get tiles
349-
print "\tGenerating tiles for %s..." % img_file
349+
print("\tGenerating tiles for %s..." % img_file)
350350
tiles = getTiles(img_arr)
351351

352352
# Save tiles
353353
if len(tiles) > 0:
354-
print "\tSaving tiles %s" % img_file
354+
print("\tSaving tiles %s" % img_file)
355355
saveTiles(tiles, img_save_dir, img_file)
356356
num_success += 1
357357
else:
358-
print "\tNo Match, skipping"
358+
print("\tNo Match, skipping")
359359
num_failed += 1
360360

361-
print "\t%d/%d generated, %d failures, %d skipped." % (num_success,
362-
len(img_files) - num_skipped, num_failed, num_skipped)
361+
print("\t%d/%d generated, %d failures, %d skipped." % (num_success,
362+
len(img_files) - num_skipped, num_failed, num_skipped))
363363

364364
class ChessboardPredictor(object):
365365
"""ChessboardPredictor using saved model"""
366366
def __init__(self, model_path='saved_models/model_10000.ckpt'):
367367

368-
print "Setting up CNN TensorFlow graph..."
368+
print("Setting up CNN TensorFlow graph...")
369369
def weight_variable(shape, name=""):
370370
initial = tf.truncated_normal(shape, stddev=0.1)
371371
return tf.Variable(initial, name)
@@ -439,9 +439,9 @@ def max_pool_2x2(x, name=""):
439439
self.sess = tf.Session()
440440

441441
# Restore model from checkpoint
442-
print "Loading model '%s'" % model_path
442+
print("Loading model '%s'" % model_path)
443443
saver.restore(self.sess, model_path)
444-
print "Model restored."
444+
print("Model restored.")
445445

446446
def getPrediction(self,img):
447447
"""Run trained neural network on tiles generated from image"""
@@ -452,7 +452,7 @@ def getPrediction(self,img):
452452
# Use computer vision to get the tiles
453453
tiles = getTiles(img_arr)
454454
if tiles is None or len(tiles) == 0:
455-
print "Couldn't parse chessboard"
455+
print("Couldn't parse chessboard")
456456
return None, 0.0
457457

458458
# Reshape into Nx1024 rows of input data, format used by neural network
@@ -463,7 +463,7 @@ def getPrediction(self,img):
463463

464464
# Prediction bounds
465465
a = np.array(map(lambda x: x[0][x[1]], zip(guess_prob, guessed)))
466-
print "Certainty range [%g - %g], Avg: %g, Overall: %g" % (a.min(), a.max(), a.mean(), a.prod())
466+
print("Certainty range [%g - %g], Avg: %g, Overall: %g" % (a.min(), a.max(), a.mean(), a.prod()))
467467

468468
# Convert guess into FEN string
469469
# guessed is tiles A1-H8 rank-order, so to make a FEN we just need to flip the files from 1-8 to 8-1
@@ -480,7 +480,7 @@ def makePrediction(self,image_url):
480480
img = helper_functions.loadImageURL(image_url)
481481

482482
if img == None:
483-
print "Couldn't load image url: %s" % image_url
483+
print("Couldn't load image url: %s" % image_url)
484484
return None, 0.0
485485

486486
# Make prediction
@@ -496,7 +496,7 @@ def makePrediction(self,image_url):
496496
if __name__ == '__main__':
497497
predictor = ChessboardPredictor()
498498
fen, certainty = predictor.makePrediction('http://imgur.com/u4zF5Hj.png')
499-
print "Predicted FEN: %s" % fen
500-
print "Certainty: %.1f%%" % (certainty*100)
499+
print("Predicted FEN: %s" % fen)
500+
print("Certainty: %.1f%%" % (certainty*100))
501501

502-
print "Done"
502+
print("Done")

0 commit comments

Comments
 (0)