@@ -229,17 +229,17 @@ def loadImage(img_file):
229
229
def resizeAsNeeded (img ):
230
230
"""Resize if image larger than 2k pixels on a side"""
231
231
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 ]) )
233
233
new_size = 500.0 # px
234
234
if img .size [0 ] > img .size [1 ]:
235
235
# resize by width to new limit
236
236
ratio = new_size / img .size [0 ]
237
237
else :
238
238
# resize by height
239
239
ratio = new_size / img .size [1 ]
240
- print "Reducing by factor of %.2g" % (1. / ratio )
240
+ print ( "Reducing by factor of %.2g" % (1. / ratio ) )
241
241
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 ]) )
243
243
return img
244
244
245
245
def getTiles (img_arr ):
@@ -284,15 +284,15 @@ def getTiles(img_arr):
284
284
if is_match :
285
285
break
286
286
else :
287
- print "Trying %d%% of threshold" % (100 * percentage )
287
+ print ( "Trying %d%% of threshold" % (100 * percentage ) )
288
288
lines_x , lines_y , is_match = getChessLines (hdx , hdy ,
289
289
hdx_thresh * percentage , hdy_thresh * percentage )
290
290
291
291
# Get the tileset
292
292
if is_match :
293
293
return getChessTiles (img_arr , lines_x , lines_y )
294
294
else :
295
- print "\t No Match, lines found (dx/dy):" , lines_x , lines_y
295
+ print ( "\t No Match, lines found (dx/dy):" , lines_x , lines_y )
296
296
return [] # No match, no tiles
297
297
298
298
def saveTiles (tiles , img_save_dir , img_file ):
@@ -328,44 +328,44 @@ def generateTileset(input_chessboard_folder, output_tile_folder):
328
328
num_skipped = 0
329
329
330
330
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 ) )
332
332
# Strip to just filename
333
333
img_file = img_path [len (input_chessboard_folder )+ 1 :- 4 ]
334
334
335
335
# Create output save directory or skip this image if it exists
336
336
img_save_dir = "%s/tiles_%s" % (output_tile_folder , img_file )
337
337
338
338
if os .path .exists (img_save_dir ):
339
- print "\t Skipping existing"
339
+ print ( "\t Skipping existing" )
340
340
num_skipped += 1
341
341
continue
342
342
343
343
# Load image
344
- print "---"
345
- print "Loading %s..." % img_path
344
+ print ( "---" )
345
+ print ( "Loading %s..." % img_path )
346
346
img_arr = loadImage (img_path )
347
347
348
348
# Get tiles
349
- print "\t Generating tiles for %s..." % img_file
349
+ print ( "\t Generating tiles for %s..." % img_file )
350
350
tiles = getTiles (img_arr )
351
351
352
352
# Save tiles
353
353
if len (tiles ) > 0 :
354
- print "\t Saving tiles %s" % img_file
354
+ print ( "\t Saving tiles %s" % img_file )
355
355
saveTiles (tiles , img_save_dir , img_file )
356
356
num_success += 1
357
357
else :
358
- print "\t No Match, skipping"
358
+ print ( "\t No Match, skipping" )
359
359
num_failed += 1
360
360
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 ))
363
363
364
364
class ChessboardPredictor (object ):
365
365
"""ChessboardPredictor using saved model"""
366
366
def __init__ (self , model_path = 'saved_models/model_10000.ckpt' ):
367
367
368
- print "Setting up CNN TensorFlow graph..."
368
+ print ( "Setting up CNN TensorFlow graph..." )
369
369
def weight_variable (shape , name = "" ):
370
370
initial = tf .truncated_normal (shape , stddev = 0.1 )
371
371
return tf .Variable (initial , name )
@@ -439,9 +439,9 @@ def max_pool_2x2(x, name=""):
439
439
self .sess = tf .Session ()
440
440
441
441
# Restore model from checkpoint
442
- print "Loading model '%s'" % model_path
442
+ print ( "Loading model '%s'" % model_path )
443
443
saver .restore (self .sess , model_path )
444
- print "Model restored."
444
+ print ( "Model restored." )
445
445
446
446
def getPrediction (self ,img ):
447
447
"""Run trained neural network on tiles generated from image"""
@@ -452,7 +452,7 @@ def getPrediction(self,img):
452
452
# Use computer vision to get the tiles
453
453
tiles = getTiles (img_arr )
454
454
if tiles is None or len (tiles ) == 0 :
455
- print "Couldn't parse chessboard"
455
+ print ( "Couldn't parse chessboard" )
456
456
return None , 0.0
457
457
458
458
# Reshape into Nx1024 rows of input data, format used by neural network
@@ -463,7 +463,7 @@ def getPrediction(self,img):
463
463
464
464
# Prediction bounds
465
465
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 () ))
467
467
468
468
# Convert guess into FEN string
469
469
# 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):
480
480
img = helper_functions .loadImageURL (image_url )
481
481
482
482
if img == None :
483
- print "Couldn't load image url: %s" % image_url
483
+ print ( "Couldn't load image url: %s" % image_url )
484
484
return None , 0.0
485
485
486
486
# Make prediction
@@ -496,7 +496,7 @@ def makePrediction(self,image_url):
496
496
if __name__ == '__main__' :
497
497
predictor = ChessboardPredictor ()
498
498
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 ) )
501
501
502
- print "Done"
502
+ print ( "Done" )
0 commit comments