Loading repository data…
Loading repository data…
fatimxox / repository
A web application powered by a deep learning model built with TensorFlow and Keras that recognizes individual chess pieces from images. The project includes a Jupyter Notebook for data preparation, preprocessing, model training, and evaluation, and a Flask web application for a user-friendly image upload and prediction interface.
To run this project locally, you need to have Python installed on your system.
pip install Flask tensorflow tensorflow-keras numpy opencv-python werkzeug Pillow matplotlib seaborn scikit-learn
Flask: To build the web application.tensorflow, tensorflow-keras: For building and loading the deep learning model.numpy: For numerical operations.opencv-python (cv2): For image processing.werkzeug: Used by Flask for file uploads (secure_filename).Pillow: For image handling (used by Matplotlib and others).matplotlib, seaborn, scikit-learn: For data exploration and evaluation in the notebook.Chessman-image-dataset organized into a specific directory structure with subfolders for each piece type is required. This dataset is commonly available on platforms like Kaggle. The notebook expects the path /kaggle/input/chessman-image-dataset/Chessman-image-dataset/Chess. You should place the Chessman-image-dataset folder in the project's root directory for local use and update the path in the notebook and app.py if necessary.Clone the repository:
git clone https://github.com/ChessVision_Chess_Piece_Recognition_AI.git
cd ChessVision_Chess_Piece_Recognition_AI
Set up a virtual environment (recommended):
python -m venv venv
# On Windows
.\venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate
Install dependencies: With your virtual environment activated, install the required libraries:
pip install -r requirements.txt # If you create a requirements.txt
# OR manually install if you don't have requirements.txt
pip install Flask tensorflow tensorflow-keras numpy opencv-python werkzeug Pillow matplotlib seaborn scikit-learn
(You can generate a requirements.txt file after installing dependencies using pip freeze > requirements.txt)
Obtain the dataset: Download the Chessman-image-dataset and place it in the project's root directory. Ensure the path in the notebook and app.py matches the location of the /Chess subfolder (e.g., Chessman-image-dataset/Chess).
Run the Notebook: Execute all cells in computer-vision copy.ipynb. This notebook loads and preprocesses the data, trains a simple neural network, evaluates its performance, and saves the trained model as model22.h5. Ensure the model22.h5 file is saved in the same directory as app.py.
computer-vision copy.ipynb) loads the dataset, converts images to grayscale, resizes them, and applies a sequence of preprocessing steps: blurring, histogram equalization, adaptive thresholding, and Sobel edge detection. It then flattens the processed images into 1D vectors and normalizes them for input into the neural network.model22.h5.app.py script loads the pre-trained Keras model (model22.h5) into memory when the Flask application starts. It includes basic error handling for model loading failure.preprocess_image): This function in app.py replicates the exact sequence of preprocessing steps performed in the notebook's training pipeline. It takes the path to an uploaded image, reads it, converts to grayscale, resizes, applies the blur, equalization, adaptive thresholding, and Sobel edge detection, then flattens and normalizes the image data. This ensures that images presented to the loaded model have the same format and features as the images it was trained on.index.html provides the user interface with a drag-and-drop area or file input, an image preview area, a loading spinner, and a display area for the prediction result.index.html handles frontend interactions:
fetch (AJAX).resultContainer to display the prediction, including an appropriate chess piece icon and some basic information./ with POST):
index_post function handles POST requests to the root URL ().The notebook computer-vision copy.ipynb includes evaluation steps for the trained model. The print(classification_report(y , pred)) output provides metrics like Precision, Recall, F1-score, and Support for each class, as well as overall accuracy, macro average, and weighted average. This report indicates how well the model performed on the data it was trained on. Note: It's common practice to evaluate on a separate test set derived from the original data split to get a better estimate of performance on unseen data.
python app.py in your terminal. Ensure your virtual environment is activated and you are in the project's root directory.http://127.0.0.1:5000/ (or the address specified by Flask) in your web browser./uploads directory (created by app.py if it doesn't exist).preprocess_image function is called to prepare the image data.predict method is called with the preprocessed image data.np.argmax function is used to find the index of the class with the highest predicted probability.categ list defined in app.py.