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')
示例#2
0
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()
示例#5
0
    def makePrediction(self, image_url):
        """Return FEN prediction, and certainty for a URL"""
        # Try to load image url
        img = loadImageURL(image_url)

        if img == None:
            print "Couldn't load image url: %s" % image_url
            return None, 0.0

        # Make prediction
        fen, certainty = self.getPrediction(img)
        if fen:
            fen = helper_functions.shortenFEN(fen)
            return fen, certainty
        else:
            return None, 0.0
  def makePrediction(self,image_url):
    """Return FEN prediction, and certainty for a URL"""
    # Try to load image url
    img = helper_functions.loadImageURL(image_url)

    if img == None:
      print "Couldn't load image url: %s" % image_url
      return None, 0.0
    
    # Make prediction
    fen, certainty = self.getPrediction(img)
    if fen:
      fen = helper_functions.shortenFEN(fen)
      return fen, certainty
    else:
      return None, 0.0
示例#7
0
def generateResponseMessage(submission, predictor):
    print("\n---\nImage URL: %s" % submission.url)

    # Use CNN to make a prediction
    fen, certainty, visualize_link = predictor.makePrediction(submission.url)

    if fen is None:
        print("> %s - Couldn't generate FEN, skipping..." % datetime.now())
        print("\n---\n")
        return None

    fen = shortenFEN(fen)  # ex. '111pq11r' -> '3pq2r'
    print("Predicted FEN: %s" % fen)
    print("Certainty: %.4f%%" % (certainty * 100))

    # Get side from title or fen
    side = getSideToPlay(submission.title, fen)
    # Generate response message
    msg = generateMessage(fen, certainty, side, visualize_link)
    print("fen: %s\nside: %s\n" % (fen, side))
    return msg