Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to evaluate chess position in 2 seconds

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you how to use Python to evaluate the position of chess in 2 seconds. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Step 1: import the required modules

Use OpenCV (open source computer vision library) and NumPy library to intercept the chessboard, visualize the checkerboard, and compare the differences between images.

If you are not familiar with OpenCV, you are encouraged to learn more about it. Next, use the PIL (Pillow) library to modify the image (for example, crop). The CompareImages module uses OpenCV to find and highlight the differences between the two images.

The mss library is a quick and easy way to take screenshots of the monitor and save the image as a PNG file. Next, use the chess library for mobile generation, validation, and visualization.

Finally, use the stockfish engine to evaluate the position of chess and determine the best way to move.

Use Stockfish 14.1 (AVX2) in Windows for this project.

Import cv2 # OpenCV library import numpy as npimport time from PIL import Image # Pillow library will be used to open and crop imagesfrom CompareImages import compare_images # This code compares and detects differences # between images.from mss import mss # will be used for grabbing screenshotssct=mss () import chess # used for chess baord visualization, move generation And move validation.from stockfish import Stockfish # The Stockfish chess engine will be used to evaluate # a given position and identify the top moves.stockfish = Stockfish (r "Enter Your Path Here//stockfish.exe") step 2: make sure the chessboard is captured correctly.

Next, a function is created to locate the chessboard on the monitor. This function takes three inputs: the X and Y coordinates of the upper left corner of the chessboard and the width of each square on the chessboard.

On the monitor, the X and Y coordinates are pixels (585, 163), and each square is 90 pixels wide (figure 1). You need to configure these inputs for your monitor.

Picture the direction of the chessboard on the 1:lichess.org. The upper left corner is located at the pixels of my screen (585163), and each square on the chessboard is 90 pixels wide.

When you run the "capture_board" function, it opens a window that displays a real-time view of the monitor based on the input parameters. It will also draw a 8x8 grid. Make sure the blue box is closely aligned with the checkerboard (figure 2).

The image on the left may result in a poor evaluation of the position or no effect at all.

Instead, make sure that the blue line is aligned with the checkerboard to accurately capture the pieces (right).

Def capture_board: global y_coord, x_coord, box_width y_coord, x_coord, box_width = y_coords, x_coords, box_widths''This functions grabs a screenshot of your monitor based on the specified parameters. It then draws an 8x8 grid based on the specified box width parameter. Make sure that each blue square aligns closely with the chessboard squares. X_coord: This x coordinate is for the top left corner of the board. You will need to modify it for your monitor. Y_coord: This y coordinate is for the top left corner of the board. You will need to modify it for your monitor. Box_width: This is the width of each square on the chessboard. You may need to modify it based on the size of your board. '' With mss () as sct: monitor = {"top": y_coord, "left": x_coord, "width": (box_width) * 8 "height": box_width*8} while True: screenshot = np.array (sct.grab (monitor)) for i in range (1 Draw 8): # Draw 7 vericle blue lines with thickness of 3 px cv2.line (screenshot, ((box_width) * iL0), ((box_width) * I (box_width) * 8), (255 box_width 0), 3) # Draw 7 horizontal blue lines with thickness of 3 px cv2.line (screenshot, (0, (box_width) * I), (Xerocoord+ (box_width) * 8) (box_width) * I), (255 Chess Board', screenshot 0), 3) cv2.imshow ('Chess Board', screenshot) if cv2.waitKey (1) = = ord (' q'): # press any key to quit. Cv2.destroyAllWindows () break # # Run the above functioncapture_board step 3: create a function to take a screenshot of the chessboard and crop it according to the input above.

In the third step, you create a function that takes a single screenshot of the chessboard position based on the input of the capture_board function. This screenshot will be saved in the local directory and processed in the next steps.

Def take_screenshot (filename): # # Screen shot and then crop image filename1 = sct.shot (output=filename) im = Image.open (filename1) # (left, upper, right, lower) im1 = im.crop ((x_coord, y_coord, x_coord+box_width*8 Y_coord+box_width*8)) # Saves image in local directory im1.save (filename1) [] (optional) step 4: determine whether the chessboard is flipped.

The following function detects whether the chessboard is flipped. It works by detecting the car (black or white) in the upper left corner of the chessboard. If the game starts and the car has moved from its original position, this function may not work properly.

You can override this function by uncommenting "is_board_flipped=True" when the board is flipped or "is_board_flipped=False" when the board is not flipped (that is, the bottom of the board is white).

Def flipped_board (): # Take a screenshot of the chessboard and save the image in local library. Take_screenshot ('InitialBoard.png') im = Image.open (' InitialBoard.png') # Crop the upper left-hand sqaure and process it. Im1 = im.crop ((5,5,70,70)) im1.save ('CurrentRook.png') image = cv2.imread (' CurrentRook.png') gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) # Assume the rook is white if the upper left-hand square has more than 300 white pixels. # otherwise, the rook is black and the board is not flipped. Is_board_flipped=True if np.sum (gray==255) > 300 else False return is_board_flipped is_board_flipped=flipped_board () # is_board_flipped=True# is_board_flipped=False print ('Is the board flipped?', is_board_flipped) step 5: the direction of the chessboard.

The following array will be arranged according to whether the chessboard is flipped. I use this array in the next step to detect the pieces on which square on the chessboard. If the chessboard is not flipped, square A1 will be in the lower left corner; otherwise, A1 square will be in the upper right corner.

MyList= [list (range (56)), list (range (48)), list (range (40)), list (range (32)), list (range (24)), list (range (16)), list (range (8), list (range)] if is_board_flipped: myList.reverse () for i in range (8): myList [I]. Reverse () myList step 6: identify chess pieces.

This step is only run once (assuming the size of the board does not change from game to game). After determining the number of pixels for each piece, you can delete this section.

Want to find a simple, efficient and consistent method to detect and identify chess pieces. It works well to calculate the number of black / white pixels on each checkerboard.

The following code set arranges black and white blocks according to the number of black and white pixels that make up each block; note that I performed this step manually, but your order should be the same. You will notice that the Black King has the least number of black pixels, while the Black Knight has the most black pixels. White elephants have the least number of white pixels, while white knights have the most white pixels. BPieceType and WPieceType are consistent with the type of chess pieces assigned by the Chess library.

Chess piece type

Pawn = 1

Knight = 2

Bisphop = 3

Rook = 4

Queen = 5

King = 6

# The pieces are arranged by the total number of black pixels. The black king has # the fewest number of black pixels, and the black knight has the largest number # of black pixels.BPieces= ['BlackKing','BlackBishop','BlackPawn','BlackRook','BlackQueen','BlackKnight'] BPieceType= [6, 3, 1, 4, 5, 2] # The pieces are arranged by the total number of white pixels. The white bishop has # the smallest number of white pixels, and the white knight has the largest number # of white pixels.WPieces= ['WhiteBishop','WhiteQueen','WhiteRook','WhitePawn','WhiteKing','WhiteKnight'] WPieceType= [3, 5, 4, 1, 6, 2]

The following for loop will crop and save 64 images from the provided chessboard image. Each of the 64 images will be aligned with a specific square in the direction specified in step 5. Be sure to create a Pieces folder in the home directory; the cropped images will be saved in this folder.

BlackPixelList, WhitePixelList= [], [] nn=0;n=0 # Take a screenshot of the entire chessboard and save the imagetake_screenshot ('image1.png') # Read the chessboard into memoryimage = cv2.imread (' image1.png') # Convert the color image to a gray scale image and save it.gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) cv2.imwrite ('gray image.png', gray) im = Image.open (' gray image.png') # Crop each squre of the chessboard and save the 64 gray scale images. Each image # will align with a specific square on the chessboard.for i in range (8): for j in range (8): im1=im.crop ((j* (box_width), i* (box_width), (jumb1) * (box_width-0)) (iTun1) * (box_width-0)) im1.save ('pieces//'+str (nn) +' .png') n=myList [I] [j] image= cv2.imread ('pieces//'+str (nn) +' .png') # count the number of black pixels per image BlackPiece=np.sum (image==0) BlackPixelList. Append (BlackPiece) # count the number of white pixels per image WhitePiece=np.sum (image==255) WhitePixelList.append (WhitePiece) nn+=1

The following code is designed to identify the number of black and white pixels for each piece. I have created a list from above that contains the number of black and white pixels.

For each color, I do the following:

Retain unique pixel valu

Sort the list in ascending order and retain the six largest pixel values

Each pixel value will be aligned with the part specified in BPieces / Wpieces

# Filter out values equal 0 and keep unique pixel values# sort the list in ascending order# keep the six largest pixel valuesBlackPixelList=set (list (filter (lambda x: X > 0, BlackPixelList)) BlackPixelList=list (BlackPixelList) BlackPixelList.sort () BlackPixelList=BlackPixelList [- 6:] WhitePixelList=set (list (filter (lambda x: X > 0, WhitePixelList)) WhitePixelList=list (WhitePixelList) WhitePixelList.sort () WhitePixelList=WhitePixelList [- 6:] # Create a dictionary for each color and assign # pixel value to each chess piece, piece type,# and piece color (False=Black, True=White) .BlackPieces = {} for I Val in enumerate (BPieces): BlackPieces [val] = (BlackPixelList [I], BPieceType [I], False) WhitePieces= {} for I, val in enumerate (WPieces): WhitePieces [val] = (WhitePixelList [I], WPieceType [I], True) step 7: assign the number of pixels to each chess piece.

If this is the first time you run the code, use the step 6 output to assign black pixels to "BlackPieces" and white pixels to "WhitePieces". The following code takes my monitor as an example.

BlackPieces=\ {'BlackKing': (5028, 6, False),' BlackBishop': (5052, 3, False), 'BlackPawn': (5679, 1, False),' BlackRook': (6489, 4, False), 'BlackQueen': (6495, 5, False),' BlackKnight': (7623, 2, False)} WhitePieces=\ {WhiteBishop': (2520, 3, True), 'WhiteQueen': (3039, 5, True),' WhiteRook': (3741, 4, True) 'WhitePawn': (3933, 1, True),' WhiteKing': (4410, 6, True), 'WhiteKnight': (6057, 2, True)} BlackPixelValues= [I [0] for i in list (BlackPieces.values ())] WhitePixelValues= [I [0] for i in list (WhitePieces.values ())] step 8: set the function to evaluate the chessboard.

We have completed all the setup steps. We first set the level of stockfish to 3000 ELO and the depth to 15. You can set the depth to 26, but this will significantly increase the processing time. Then we create the "evaluate_position" function. This function will:

Screenshot

Convert a color image to grayscale and save the image

Determine the position of each piece on the board and use the chess library to place the pieces in the appropriate position on the board

Assigns the turn to the specified color. By default, it's White's turn.

Assign the FEN position in stockfish according to the location in the chess library

Generate the requested output

Stockfish.set_elo_rating (3000) stockfish.set_depth (15) def evaluate_position (white_turn=True, board_flipped=is_board_flipped): start_time = time.time () # take a screenshot of the board take_screenshot ('image1.png') image = cv2.imread (' image1.png') gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) cv2.imwrite ('gray image.png') Gray) im = Image.open ('gray image.png') # Set up the board from the Chess library board = chess.Board () # Remove all the pieces because they will be specified in the next step board.clear () # Set the turn to White to play, unless otherwise specified board.turn = white_turn nn=0 For j in range 0 for i in range (8): for j in range (8): im1=im.crop ((j* (box_width), i* (box_width), (jumb1) * (box_width-0)) (pieces//'+str 1) * (box_width-0)) im1.save ('pieces//'+str (nn) +' .png') n=myList [I] [j] # Read each cropped chess square and identify the chess piece and color. Image= cv2.imread ('pieces//'+str (nn) +' .png') BlackPiece=np.sum (image==0) WhitePiece=np.sum (image==255) res_val= if BlackPiece > min (BlackPixelValues)-50: # Identify the chess piece based on he number of black pixels closest to the # values from the BlackPieces dictionary. Col_key, res_val = min (BlackPieces.items (), key=lambda x: abs (BlackPiece-x [1] [0])) if WhitePiece > min (WhitePixelValues)-50: # Identify the chess piece based on he number of white pixels closest to the # values from the BlackPieces dictionary. Col_key, res_val = min (WhitePieces.items (), key=lambda x: abs (WhitePiece-x [1] [0])) # set the chess piece on the board based on it position, piece type, and color. Board.set_piece_at (n PieceSecretchess.Piece (piece_type=res_val [1]) Color=res_ vala [2]) nn+=1 # create the FEN based on the current position turn_fen=' w-- 10'if board.turn = = True else'b-- 01 'current_fen=board.board_fen () + turn_fen # Assign the postion in Stockfish based on FEN stockfish.set_fen_position (current_fen) # Get the top moves top_moves=stockfish.get_top_moves () # Get the best move my _ move=top_moves [0] ['Move'] # Move the chess piece based on the best move board.push (chess.Move.from_uci (my_move)) # Create output print (' Time:') Time.time ()-start_time) print ('Best Move:', my_move) print (' Evaluation:') Stockfish.get_evaluation () print ('Top moves:') for i in top_moves: print (I) if board_flipped: board.apply_transform (chess.flip_vertical) board.apply_transform (chess.flip_horizontal) display (board) else: display (board) step 9: run the chessboard evaluation.

Congratulations, you're done! Specify whether it is white (set white_turn=True) or black (white_turn=False) and run the evaluation function. This function will output:

Time: the time spent running the function

Best Move: the wisest step

Evaluation: assessment of the current location

Top moves: best Mobility

Visual effects of the current position and the next best move

If the location changes, rerun the evaluate_postition function; you don't need to rerun the previous steps.

Evaluate_position (white_turn=True)

Sample evaluate_position output.

These are all the contents of the article "how to use Python to evaluate chess position in 2 seconds". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report