Py-Image-Classifier is a Computer Vision project applied to predictive maintenance in an industrial setting.
It is designed to automatically detect and classify defects in electric motors (overheating, wear, etc.) using images (specifically thermal images), enabling failure anticipation.
This modular pipeline allows for training high-performance models using two distinct approaches:
- Transfer Learning: Feature extraction via pre-trained deep neural networks (such as MobileNetV2 and ResNet50) coupled with classical classifiers.
- CNN from Scratch: End-to-end training of a custom convolutional neural network.
The project also integrates Data Augmentation tools to enhance model robustness against image variability.
Py-Image-Classifier/
│
├── dataset/ # Place your original images here
│
├── features/ # Automatically created - stores processed features
│ ├── X_train.npy
│ ├── X_test.npy
│ └── y.npy
│
├── augmented/ # Automatically created - stores augmented images
│
├── models/ # Automatically created - trained ML models (.joblib)
│
├── results/ # Automatically created - reports and charts
│ ├── report_....json
│ └── confusion_matrix_....png
│
├── preprocess_images.py # Script 1
├── extract_features.py # Script 2
├── train_classifiers.py # Script 3
├── train_cnn_from_scratch.py # Script 3 (Alternative)
├── evaluate_results.py # Script 4
│
├── app.py # Streamlit App
│
├── README.md
└── requirements.txt # Dependencies file- Python 3.9 or higher
-
Download the files into a folder.
-
Open a terminal at the project root.
-
Create a virtual environment (recommended to isolate dependencies):
python -m venv venv
-
Activate the virtual environment:
- On Windows:
.\venv\Scripts\activate
- On Windows:
-
Install all required libraries in a single command:
pip install -r requirements.txt
The pipeline execution follows sequential steps. The order is crucial.
This script prepares your images and multiplies their number to enrich the dataset.
- Prepare your data: Place your original images in the
dataset/folder, organized into subfolders per class. - Run the script from the terminal (with the virtual environment activated):
python preprocess_images.py
- A window will open. Select the
datasetfolder. - Result: A
features/augmented/folder is created, containing multiple augmented versions of each original image.
There are two approaches in this project:
Uses pre-trained models to extract features, then trains classical classifiers.
-
Feature Extraction:
python extract_features.py
Generates
.npyfiles infeatures/. -
Training Classifiers:
# For MobileNetV2 python train_classifiers.py --features mobilenetv2 # For ResNet50 python train_classifiers.py --features resnet50
Generates models in
models/. -
Evaluation:
# For MobileNetV2 python evaluate_results.py --features mobilenetv2 # For ResNet50 python evaluate_results.py --features resnet50
Generates reports and matrices in
results/.
Trains a convolutional neural network directly on augmented images.
- Start Training:
python train_cnn_from_scratch.py
- Results:
- Saves the model:
models/cnn_from_scratch.keras - Saves history:
results/history_cnn_from_scratch.png - Saves confusion matrix:
results/confusion_matrix_cnn_from_scratch.png
- Saves the model:
To test your models interactively on new images:
- Launch the application:
streamlit run app.py
- Features:
- Model Selection: Choose "CNN From Scratch" or "Transfer Learning".
- Upload: Upload an image (JPG, PNG).
- Prediction: View the predicted class and confidence level.
preprocess_images.py: Loads raw images, applies augmentations, and saves new images.extract_features.py: Extracts features via pre-trained CNNs (MobileNetV2, ResNet50).train_classifiers.py: Trains ML classifiers (LDA, kNN, SVM) on extracted features.train_cnn_from_scratch.py: Trains a custom CNN directly on images.evaluate_results.py: Evaluates hybrid ML models and generates reports.app.py: Streamlit Web Application for interactive inference.
- TRIBAK Mohamed