This project implements a complete pipeline for detecting regions of interest (ROIs) in nanosensor images, extracting color-based features, and predicting heavy metal ion (HMI) concentrations using advanced machine learning techniques. The system integrates:
- YOLOv8 for ROI detection
- Dataset augmentation for robustness
- XGBoost regression models with Bayesian hyperparameter tuning for accurate prediction
- A secure, user-friendly Flask web interface for image upload, processing, and result visualization
Designed for robustness, scalability, and ease of use by researchers and practitioners in environmental heavy metal detection.
- Overview
- Project Structure
- Setup and Installation
- Generating Self-Signed SSL Certificates
- Sample Preparation
- How to Take Images for Analysis
- Example Images
- File Descriptions
- Usage
- Contributing
- References
- Licensing
.
├── app.py
├── requirements.txt
├── models/
│ ├── cd_xgboost_v1.json
│ ├── cd_scaler.pkl
│ ├── cd_y_scaler.pkl
│ ├── hg_xgboost_v2.json
│ ├── hg_scaler_v2.pkl
│ ├── hg_y_scaler_v2.pkl
│ └── yolov8n_roi_extraction_v2.pt
├── static/
│ ├── uploads/
│ └── outputs/
├── templates/
│ ├── index.html
│ ├── result.html
│ └── error.html
├── certs/
│ ├── cert.pem
│ └── key.pem
Training/
├── RGBTraining/
│ ├── augment_dataset.py
│ ├── cd_augmented_dataset_v1.csv
│ ├── XG_BOOST.py
│ └── hg_augmented_dataset_v2.csv
├── YoloTraining/
│ ├── images/
│ │ ├── train/
│ │ ├── val/
│ │ └── test/
│ ├── labels/
│ │ ├── train/
│ │ ├── val/
│ │ └── test/
│ └── roi.yaml
├── experiment.log
└── README.md
- Python 3.8 or higher
- Git
- Virtual environment tool (
venv) - Recommended: PyCharm IDE for Windows users
git clone https://github.com/fahadarefin/smartdetectai.git
cd smartdetectai
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/macOS
source venv/bin/activate
pip install -r requirements.txt
python app.pyAccess the app at:
http://127.0.0.1:5000
- Find your IPv4 address via
ipconfig(Windows) orifconfig(Linux/macOS). - Run the Flask app (same as above).
- Access from other devices on the same network via:
http://<your-local-ip>:5000
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app:appNginx configuration example:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Restart Nginx:
sudo systemctl restart nginxdocker build -t smartdetectai .
docker run -p 5000:5000 smartdetectaiFor HTTPS support during development or internal testing:
openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes \
-keyout certs/key.pem -out certs/cert.pem \
-subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"Note: Browsers will warn for self-signed certs; use Let's Encrypt or other CA for production.
Before capturing images for analysis, prepare your samples carefully to ensure accurate results:
- Place your sample in one test tubes (or two test tubes if you are testing multiple samples for the same target heavy metal ion).
- Add the appropriate AgNS (Silver Nanosensor) to each test tube. Choose the nanosensor type based on the target heavy metal ion you want to detect.
- Prepare a control test tube by adding pure water along with the same type of AgNS nanosensor.
- Arrange the test tubes inside the SmartDetect chamber, positioning the control tube with pure water on the left side and the sample tubes on the right side.
- Allow the colorimetric reaction to develop for approximately 20 to 30 minutes before proceeding to image capture.
High-quality images are essential for accurate concentration prediction.
- Use consistent, good lighting; avoid shadows and glare.
- Position the camera directly in front of the nanosensor sample for a flat view.
- Keep the image sharp and in focus (use tripod if necessary).
- Ensure the sample region occupies a significant part of the frame.
- Use high resolution and avoid heavy compression artifacts.
- Accepted formats: PNG, JPG, JPEG
- Max file size: 10 MB
- Upload images by drag-and-drop or Select a File option.
- Use the Take a Photo button (mobile devices preferred) to capture live images.
- Adjust contrast, brightness, and threshold sliders if needed.
- Preview images before submission.
Important: Modern smartphones require HTTPS-secured web pages to grant access to the device camera. To use the Take Photo feature within the web interface, ensure you are accessing the app via HTTPS (secure connection). Self-signed certificates or valid SSL certificates are needed to enable camera access.
Screenshot of the upload interface showing drag-and-drop and camera capture button.
Example of a well-captured nanosensor test tube image used for analysis.
Screenshot of the result page displaying the concentration prediction output.
| File/Folder | Purpose |
|---|---|
app.py |
Flask backend: handles uploads, ROI detection, feature extraction, prediction, and UI rendering |
XG_BOOST.py |
Model training script using XGBoost with Bayesian hyperparameter tuning |
augment_dataset.py |
Dataset augmentation script to improve model generalization |
YOLO.py |
Script for training YOLOv8 model on ROI dataset |
requirements.txt |
Lists all Python dependencies |
models/ |
Stores pretrained YOLO and XGBoost models and scalers |
static/uploads/ |
Temporary storage for user-uploaded images |
static/outputs/ |
Stores processed result images |
templates/ |
HTML templates for Flask web UI |
certs/ |
SSL certificates for HTTPS support |
python app.py- Runs with HTTPS if certificates are present in
certs/. - Otherwise, runs HTTP on port 5000.
- Access via
https://localhost:5000orhttp://localhost:5000.
python YOLO.py- Trains ROI detection model on custom dataset.
python XG_BOOST.py- Trains regression models for heavy metal concentration prediction.
python augment_dataset.py- Performs dataset augmentation to improve model robustness.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new feature branch.
- Implement your changes adhering to PEP 8 style.
- Add tests where applicable.
- Submit a pull request with detailed description.
- Ultralytics YOLOv8 Documentation
- scikit-optimize (Bayesian Optimization)
- Flask Web Framework
- OpenSSL Documentation
This project is licensed under the MIT License, allowing free use, modification, and distribution.
Note: All intellectual property rights related to research data, experimental results, and nanosensor technology belong to the Microsystems and Nano Engineering Group, University of Dhaka. Use for academic and research purposes is encouraged with proper citation. Commercial use requires prior permission from the group.
This system aims to advance nanosensor data analysis by combining cutting-edge image processing and machine learning, making environmental heavy metal detection more accessible and interpretable.


