def set_board_from_screen(self): """ Set the instance chessboard if found on the screen. """ self.logger.debug('START set_board_from_screen') screenshot = pyautogui.screenshot() tiles = None tiles, corners = chessboard_finder.findGrayscaleTilesInImage( screenshot) if (tiles is not None): fen, tile_certainties = self.predictor.getPrediction(tiles) fen = shortenFEN(fen) self.chess_board.set_board_fen(fen) self.board_corners = corners self.logger.info('SUCCESS Got board.') self.logger.debug('FEN is ' + fen) self.logger.debug('Corners are ' + str(corners)) certainty = tile_certainties.min() self.logger.debug('Per-tile certainty:') self.logger.debug(tile_certainties) self.logger.debug("Certainty range [%g - %g], Avg: %g" % (tile_certainties.min(), tile_certainties.max(), tile_certainties.mean())) self.logger.debug("Final Certainty: %.1f%%" % (certainty * 100)) else: self.logger.info('FAIL No tiles detected.') self.logger.debug('END set_board_from_screen')
def makePrediction(self, url): """Try and return a FEN prediction and certainty for URL, return Nones otherwise""" img, url = helper_image_loading.loadImageFromURL(url) result = [None, None, None] # Exit on failure to load image if img is None: print(('Couldn\'t load URL: "%s"' % url)) return result # Resize image if too large img = helper_image_loading.resizeAsNeeded(img) # Look for chessboard in image, get corners and split chessboard into tiles tiles, corners = chessboard_finder.findGrayscaleTilesInImage(img) # Exit on failure to find chessboard in image if tiles is None: print('Couldn\'t find chessboard in image') return result # Make prediction on input tiles fen, tile_certainties = self.getPrediction(tiles) # Use the worst case certainty as our final uncertainty score certainty = tile_certainties.min() # Get visualize link visualize_link = helper_image_loading.getVisualizeLink(corners, url) # Update result and return result = [fen, certainty, visualize_link] return result
def get_fen_from_board(args): #Namespace(active='w', filepath='board.png', unflip=False, url='http://imgur.com/u4zF5Hj.png') # Load image from filepath or URL parser = argparse.ArgumentParser() parser.add_argument("--filepath", type=str, default="board.png") parser.add_argument('--unflip', default=False, action='store_true', help='revert the image of a flipped chessboard') parser.add_argument('--active', default='w') args = parser.parse_args() if args.filepath: # Load image from file img = helper_image_loading.loadImageFromPath(args.filepath) args.url = None # Using filepath. else: img, args.url = helper_image_loading.loadImageFromURL(args.url) # Exit on failure to load image if img is None: raise Exception('Couldn\'t load URL: "%s"' % args.url) # Resize image if too large # img = helper_image_loading.resizeAsNeeded(img) # Look for chessboard in image, get corners and split chessboard into tiles tiles, corners = chessboard_finder.findGrayscaleTilesInImage(img) # Exit on failure to find chessboard in image if tiles is None: raise Exception('Couldn\'t find chessboard in image') # Create Visualizer url link if args.url: viz_link = helper_image_loading.getVisualizeLink(corners, args.url) #print(('---\nVisualize tiles link:\n %s\n---' % viz_link)) # Initialize predictor, takes a while, but only needed once predictor = ChessboardPredictor() fen, tile_certainties = predictor.getPrediction(tiles) predictor.close() #if args.unflip: #fen = unflipFEN(fen) short_fen = shortenFEN(fen) # Use the worst case certainty as our final uncertainty score certainty = tile_certainties.min() #print('Per-tile certainty:') #print(tile_certainties) #print(("Certainty range [%g - %g], Avg: %g" % ( # tile_certainties.min(), tile_certainties.max(), tile_certainties.mean()))) active = args.active #print(("---\nPredicted FEN:\n%s %s - - 0 1" % (short_fen, active))) #print(("Final Certainty: %.1f%%" % (certainty*100))) return short_fen, certainty
def main(args): # Load image from filepath or URL if args.filepath: # Load image from file img = helper_image_loading.loadImageFromPath(args.filepath) args.url = None # Using filepath. else: img, args.url = helper_image_loading.loadImageFromURL(args.url) # Exit on failure to load image if img is None: raise Exception('Couldn\'t load URL: "%s"' % args.url) # Resize image if too large # img = helper_image_loading.resizeAsNeeded(img) # Look for chessboard in image, get corners and split chessboard into tiles tiles, corners = chessboard_finder.findGrayscaleTilesInImage(img) # Exit on failure to find chessboard in image if tiles is None: raise Exception('Couldn\'t find chessboard in image') # Create Visualizer url link if args.url: viz_link = helper_image_loading.getVisualizeLink(corners, args.url) print('---\nVisualize tiles link:\n %s\n---' % viz_link) if args.url: print("\n--- Prediction on url %s ---" % args.url) else: print("\n--- Prediction on file %s ---" % args.filepath) # Initialize predictor, takes a while, but only needed once predictor = ChessboardPredictor() fen, tile_certainties = predictor.getPrediction(tiles) predictor.close() short_fen = shortenFEN(fen) global FEN global FLIPFEN FEN = str(short_fen) FLIPFEN = str(short_fen[-1::-1]) print 'test', FEN, FLIPFEN # Use the worst case certainty as our final uncertainty score certainty = tile_certainties.min() print('Per-tile certainty:') print(tile_certainties) print("Certainty range [%g - %g], Avg: %g" % (tile_certainties.min(), tile_certainties.max(), tile_certainties.mean())) print("---\nPredicted FEN: %s" % "https://www.lichess.org/analysis/{}".format(short_fen)) print("---\nPredicted FEN flip board %s" % "https://lichess.org/analysis/{}_b".format(str(short_fen)[-1::-1])) print("Final Certainty: %.1f%%" % (certainty * 100))
def main(args): # Load image from filepath or URL if args.filepath: # Load image from file img = helper_image_loading.loadImageFromPath(args.filepath) else: img, args.url = helper_image_loading.loadImageFromURL(args.url) # Exit on failure to load image if img is None: raise Exception('Couldn\'t load URL: "%s"' % args.url) # Resize image if too large # img = helper_image_loading.resizeAsNeeded(img) # Look for chessboard in image, get corners and split chessboard into tiles tiles, corners = chessboard_finder.findGrayscaleTilesInImage(img) # Exit on failure to find chessboard in image if tiles is None: raise Exception('Couldn\'t find chessboard in image') # Create Visualizer url link if args.url: viz_link = helper_image_loading.getVisualizeLink(corners, args.url) print(('---\nVisualize tiles link:\n %s\n---' % viz_link)) if args.url: print(("\n--- Prediction on url %s ---" % args.url)) else: print(("\n--- Prediction on file %s ---" % args.filepath)) # Initialize predictor, takes a while, but only needed once predictor = ChessboardPredictor() fen, tile_certainties = predictor.getPrediction(tiles) predictor.close() short_fen = shortenFEN(fen) # Use the worst case certainty as our final uncertainty score certainty = tile_certainties.min() print('Per-tile certainty:') print(tile_certainties) print(("Certainty range [%g - %g], Avg: %g" % ( tile_certainties.min(), tile_certainties.max(), tile_certainties.mean()))) print(("---\nPredicted FEN: %s" % short_fen)) print(("Final Certainty: %.1f%%" % (certainty*100))) file = open("C:/Users/Recursor/Desktop/BACKUP/FEN/fen.txt", 'w') file.write(short_fen) file.close()