A single tool that combines ML-based anomaly detection and thermal hotpoint detection into one annotated image output.
This project analyzes thermal images using two complementary methods:
- ML analysis: an autoencoder-based anomaly detector (red boxes)
- Thermal analysis: hotspot detection using the red channel (yellow boxes)
The results from both methods are combined into a single annotated image.
Create and activate a Python virtual environment named thermal_env and install the project dependencies (see requirements.txt):
.\thermal_env\Scripts\Activate.ps1On Unix-like systems use:
source thermal_env/bin/activateRun the unified analysis on a single image:
.\thermal_env\Scripts\Activate.ps1; python unified_thermal_analysis.py \
Dataset/T1/faulty/T1_faulty_001.jpg \
--threshold 0.5 \
--min-area 200 \
--max-area 5000 \
--max-annotations 3 \
--blue-threshold 30The script produces three files in the unified_results/ directory:
*_combined_annotated.jpg— annotated image containing ML (red) and thermal (yellow) detections*_unified_analysis.png— a 6-panel visualization summarizing the analysis*_unified_report.txt— a text report with detailed detection information
| Detection type | Box color | Label |
|---|---|---|
| ML anomaly | red | ML-1, ML-2, ML-3 |
| Thermal hotspot | yellow | TH-1, TH-2, TH-3 |
--threshold <0-1>: detection threshold (default: 0.5). Lower values produce more detections.--min-area <pixels>: minimum detection area (default: 200).--max-area <pixels>: maximum detection area (default: 5000).--max-annotations <N>: maximum annotations to show (default: 3).--blue-threshold <percent>: maximum allowed percentage of blue pixels in a detection (default: 30).
--thermal-threshold <0-255>: temperature threshold on the red channel (default: 200).--thermal-min-cluster <N>: minimum cluster size for DBSCAN (default: 15).--thermal-epsilon <N>: DBSCAN epsilon value for clustering (default: 20).
--output-dir <path>: output directory (default:unified_results).
Arbit-Transformer-Analysis/
├── unified_thermal_analysis.py # main analysis script
├── requirements.txt # Python dependencies
├── thermal_env/ # suggested virtual environment
├── Dataset/ # thermal image datasets
│ ├── T1/faulty/
│ ├── T2/faulty/
│ └── ...
├── ML_analysis/ # ML anomaly detection module
│ ├── detect_and_annotate.py # ML detection functions
│ ├── model.py # autoencoder model
│ ├── train.py # model training script
│ └── models/ # trained models
│ └── best_model.pth
├── heat_point_analysis/ # thermal hotspot detection module
│ └── thermal_hotpoint_detector.py
└── unified_results/ # output directory
Run the main analysis on a single image:
python unified_thermal_analysis.py Dataset/T1/faulty/T1_faulty_001.jpgShow more detections by lowering the ML threshold and increasing the result limit:
python unified_thermal_analysis.py Dataset/T1/faulty/T1_faulty_001.jpg \
--threshold 0.3 \
--max-annotations 5Adjust both ML and thermal thresholds and save results to a custom folder:
python unified_thermal_analysis.py Dataset/T1/faulty/T1_faulty_001.jpg \
--threshold 0.5 \
--thermal-threshold 180 \
--output-dir my_results- Loads a trained autoencoder model
- Produces a reconstruction error heatmap
- Finds regions with high reconstruction error
- Filters detections by area and blue-channel content
- Returns the top N detections and draws red bounding boxes
- Uses the image red channel as a temperature indicator
- Thresholds hot pixels and clusters them with DBSCAN
- Builds bounding boxes around clusters and filters out border/white noise
- Draws yellow bounding boxes for hotspots
- Combines both sets of detections
- Produces a 6-panel visualization and a single annotated image
- Saves a text report with detection details
Fine-tuning allows you to periodically update the anomaly detection model with new user feedback and recent images, improving accuracy and adapting to new data distributions.
- All images from the latest data folder (by month/year) are included.
- A configurable multiple (default: 5x) of randomly sampled images from older folders (including the base dataset) are added.
- The combined dataset is split into training and validation sets (default: 80% train, 20% validation).
- The best model is saved based on validation loss, not just training loss.
- All training and validation losses, dataset breakdown, and run details are logged for traceability.
python ML_analysis/finetune.py --feedback-data Local_Dataset --weights ML_analysis/models/best_model.pth --output-dir output --old-multiplier 5 --val-split 0.2Options:
--feedback-data: Path to the folder containing all monthly and base datasets--weights: Path to the latest model weights (.pth)--output-dir: Where to save outputs and logs--old-multiplier: How many times more old images to sample compared to latest (default: 5)--val-split: Fraction of data for validation (default: 0.2)--latest-folder: Optionally specify the latest folder (e.g., 07_2025)
- Best fine-tuned model (
best_finetuned_model.pth) saved by validation loss - Training and validation loss plot (
finetune_training_loss.png) - Full run log (
finetune_log.json) with dataset breakdown and losses
The auto_finetune.py script automates the process of updating your anomaly detection model with new data and user feedback.
- Checks if there are enough new images in
Finetune_data/temp_data/normal(default: 10). If not, the script exits. - If enough images are present, moves both
normalandfaultyimages fromFinetune_data/temp_data/to a new folder inFinetune_data/Local_Dataset/YYYY_MM/(organized by year and month). - Clears the temporary folders after moving.
- Runs the fine-tuning script (
ML_analysis/finetune.py) with the updated dataset. - Logs the last fine-tune date, image counts, and run details to
Finetune_data/finetune_status.json.
python auto_finetune.pyYou can customize options such as minimum image count, dataset paths, and fine-tune arguments:
python auto_finetune.py --min-images 20 --finetune-args --feedback-data Finetune_data/Local_Dataset --weights ML_analysis/models/best_model.pth --output-dir Finetune_data/output- New images are organized in
Local_Dataset/YYYY_MM/normalandLocal_Dataset/YYYY_MM/faulty. - Fine-tuning is triggered automatically if enough new data is available.
- Status and summary are saved in
Finetune_data/finetune_status.json.
See auto_finetune.py for full details and options.
You can trigger fine-tuning and check its status using the following FastAPI endpoints:
Endpoint: POST /run-finetune
Description:
Runs the automated fine-tuning workflow (auto_finetune.py) in the active conda environment. No arguments are required; the script uses its default settings.
Example (using curl):
curl -X POST http://localhost:8080/run-finetuneResponse: Returns the stdout, stderr, and return code from the fine-tuning script.
Endpoint: GET /finetune-status
Description:
Returns the last fine-tune date and summary from Finetune_data/finetune_status.json.
Example (using curl):
curl http://localhost:8080/finetune-statusResponse:
{
"last_finetune": "2025-10-22 01:23:45"
}- To get more ML detections, lower
--threshold(for example, 0.3). - To detect more thermal hotspots, lower
--thermal-threshold(for example, 150). - Increase
--max-annotationsto show more results (for example, 5). - Increase
--min-areato reduce small/noisy detections (for example, 500).
If you see import errors, make sure the virtual environment is activated and dependencies are installed.
If the ML model cannot be found, confirm that ML_analysis/models/best_model.pth exists.
If you get no detections, try relaxing the thresholds:
python unified_thermal_analysis.py <image> \
--threshold 0.3 \
--thermal-threshold 150Install dependencies from requirements.txt into your virtual environment. Typical requirements include:
- torch >= 2.0.0
- torchvision >= 0.15.0
- opencv-python >= 4.8.0
- opencv-contrib-python >= 4.8.0
- numpy >= 1.21.0
- matplotlib >= 3.5.0
- scikit-learn >= 1.3.0
- albumentations >= 1.3.0
See QUICKSTART.md for a short setup guide and ML_analysis/README.md for details on the ML components.
Main command example:
.\thermal_env\Scripts\Activate.ps1; python unified_thermal_analysis.py \
Dataset/T1/faulty/T1_faulty_001.jpg \
--threshold 0.5 --min-area 200 --max-area 5000 \
--max-annotations 3 --blue-threshold 30Main output: unified_results/*_combined_annotated.jpg