This project implements a simpler machine learning-based system for identifying chess pieces from an image of a chessboard, generating the corresponding FEN (Forsyth-Edwards Notation), and determining the best move using a chess engine. The primary focus lies in recognizing individual pieces on the board, constructing a board array for the Python Chess library, and leveraging it to compute the best move.
- Chess Piece Detection: Uses a trained machine learning model to identify individual pieces (both white and black) on the board.
- FEN Generation: Converts the identified board state into a valid FEN string.
- Best Move Prediction: Integrates with a chess engine to compute the best move based on the generated FEN.
- Web Application: A user-friendly web interface where users can upload chessboard images, specify whose turn it is (white or black), and receive the best move.
- Customizable Dataset: Train the model on other chessboard formats using the provided code and dataset generation tools.
This folder contains all code and resources related to creating the dataset, training the model, and making predictions:
- Dataset Creation: Scripts for generating datasets and CSV headers from chessboard images.
- Model Training: Code for training a machine learning model using the provided dataset.
- Board Prediction: Scripts to predict the board state using the trained model.
- Best Move Prediction: Logic to determine the best move using the predicted board state and a chess engine.
- Source: The dataset was created using screenshots of chess board taken from chess.com.
- Statistics:
- 120 board images were used for training.
- Extracted 3,439 white piece images and 3,437 black piece images.
- Customization: You can expand the dataset by adding more images of chess pieces and retrain the model using the provided scripts.
This folder contains the web application built using Flask and Python:
- Web Interface: Upload an image of the chessboard, specify whose turn it is (white or black), and get the best move.
- Chess Engine Integration: Requires the user to specify the path to the chess engine executable.
- Requirements: Includes a
requirements.txt
file to simplify dependency installation.
- Python 3.8+
- NumPy
- Pandas
- Flask
- OpenCV
- Scikit-learn
- Pillow
- A compatible chess engine (e.g., Stockfish)
-
Clone the repository:
git clone https://github.com/LovejeetM/Chess_ML_Algorithm.git cd Chess_ML_Algorithm
-
Navigate to the
Trained Model Webapp
folder:cd "Trained Model Webapp"
-
Install dependencies:
pip install -r requirements.txt
-
Set up the chess engine path in the web application:
- Edit the
ml.py
file (or the appropriate script) to include the path to your chess engine executable.
- Edit the
-
Run the Flask application:
python app.py
-
Open the web app in your browser at
http://127.0.0.1:5000
.
Additionally, you can use virtual environment if it does not run on your system and for testing.
- Upload Image: Upload a chessboard image (e.g., a screenshot from chess.com).
- Specify Turn: Indicate whether it is White's or Black's turn.
- Generate Best Move: The app processes the image, generates the FEN, and displays the best move.
- Add new chessboard images to the dataset.
- Use the scripts in the
Dataset and Machine Learning
folder to generate piece images and prepare a CSV file. - Retrain the model using the provided training scripts.
- Update the model file in the web app folder to use the new model.
- Current Model Accuracy: Achieves 100% accuracy on the provided dataset of chess board images.
- Custom Datasets: Accuracy may vary based on the quality and diversity of the training dataset.
- Light Weight Model: This codebase uses simple method for classification of pieces board.
- Support for Other Chessboard Styles: Expand the dataset to include different board formats.
- Real-Time Detection: Implement live video feed detection for real-time chess analysis.
- Enhanced Web Features: Add more functionality to the web app, such as game history tracking and multi-move prediction.
This project is licensed under a custom license. Usage of this code is restricted to non-commercial purposes only. Modifications are allowed, but commercial redistribution is prohibited without permission.
LinkedIn: https://www.linkedin.com/in/lovejeet-singh-matharu-975679213/
Feel free to ask any questions too.