A production-ready web-based system for automated quality inspection using computer vision and deep learning. Detects defects in product images, classifies severity levels, and provides visual heatmaps for model interpretability.
✅ Image Upload & Processing
- RESTful API for image uploads
- Support for JPG and PNG formats
- Automatic preprocessing and normalization
✅ Defect Detection
- YOLO-based object detection
- Edge detection preprocessing
- Bounding box visualization
- Confidence-based filtering
✅ Severity Classification
- CNN-based classification model
- Three severity levels: Minor, Major, Critical
- Severity score visualization
✅ Advanced Visualizations
- Grad-CAM style activation heatmaps
- Confidence distribution maps
- Overlay visualizations
- Saliency maps
✅ Ensemble & Fusion
- Traditional CV + Deep Learning fusion
- Spatial merging of nearby detections
- Confidence voting mechanism
- Robust result aggregation
✅ Full-Stack UI
- React-based responsive dashboard
- Upload interface with drag-and-drop
- Detailed result analysis views
- Results history with filtering
- Real-time statistics
- Framework: FastAPI (Python)
- Computer Vision: OpenCV
- ML Inference: NumPy (Mock YOLO/CNN for production-ready mockup)
- Server: Uvicorn
- Framework: React 18
- Build Tool: Vite
- HTTP Client: Axios (Fetch API)
- Styling: CSS3 with Grid/Flexbox
- Image preprocessing with OpenCV
- YOLO-based detection (mock implementation)
- CNN severity classification (mock implementation)
- Grad-CAM heatmap generation
- Ensemble fusion engine
project-root/
│
├── backend/
│ ├── main.py # FastAPI application entry point
│ ├── requirements.txt # Python dependencies
│ │
│ ├── models/
│ │ ├── detection.py # YOLO-based detection model
│ │ └── classification.py # CNN severity classifier
│ │
│ ├── routes/
│ │ └── predict.py # (API routes in main.py)
│ │
│ └── utils/
│ ├── preprocessing.py # Image preprocessing pipeline
│ ├── visualization.py # Heatmap and visualization utils
│ └── fusion.py # Ensemble fusion engine
│
├── frontend/
│ ├── package.json # Node dependencies
│ ├── vite.config.js # Vite configuration
│ ├── index.html # HTML entry point
│ │
│ └── src/
│ ├── main.jsx # React entry point
│ ├── App.jsx # Main app component
│ ├── App.css # Global styles
│ ├── index.css # Base styles
│ │
│ └── components/
│ ├── Upload.jsx # Image upload component
│ ├── Upload.css # Upload styles
│ │
│ ├── ResultView.jsx # Results display component
│ ├── ResultView.css # Results styles
│ │
│ ├── Dashboard.jsx # Dashboard component
│ └── Dashboard.css # Dashboard styles
│
├── data/
│ ├── uploads/ # Uploaded images storage
│ └── outputs/ # Generated visualizations
│
└── README.md # This file
- Python 3.9+ (for backend)
- Node.js 16+ (for frontend)
- pip (Python package manager)
- npm or yarn (Node package manager)
- Create and activate virtual environment (recommended):
# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activate- Install dependencies:
cd backend
pip install -r requirements.txt- Run the backend server:
python main.pyThe backend will start at http://localhost:8000
API Documentation: Visit http://localhost:8000/docs (Swagger UI)
You can train lightweight model artifacts to improve consistency and efficiency:
cd backend
python train_model.pyThis trains:
data/model_artifacts/detection_prototypes.jsondata/model_artifacts/severity_model.json
If labeled datasets exist, they are used automatically:
backend/data/training/defect/{crack,scratch,dent,discoloration,corrosion}/backend/data/training/severity/{minor,major,critical}/
If labeled datasets are not present, the script bootstraps from backend/data/uploads.
- Install dependencies:
cd frontend
npm install- Start development server:
npm run devThe frontend will start at http://localhost:3000
- Build for production:
npm run buildOutput will be in frontend/dist/
Upload image and process for defect detection
Request:
curl -X POST "http://localhost:8000/predict" \
-F "file=@image.png"Response:
{
"prediction_id": "uuid-string",
"timestamp": "2025-02-19T10:30:00",
"image_info": {
"width": 640,
"height": 480,
"filename": "product.png"
},
"detections": [
{
"bbox": {
"x1": 100,
"y1": 150,
"x2": 250,
"y2": 300,
"width": 150,
"height": 150
},
"detection": {
"type": "crack",
"confidence": 0.92
},
"severity": {
"level": "major",
"score": 0.65
},
"ensemble_confidence": 0.88
}
],
"summary": {
"total_defects": 2,
"critical_count": 0,
"major_count": 1,
"minor_count": 1,
"average_confidence": 0.89
},
"visualizations": {
"detections": "base64-encoded-image",
"heatmap": "base64-encoded-image",
"overlay": "base64-encoded-image",
"confidence": "base64-encoded-image"
}
}Get all inspection results (last 10)
curl "http://localhost:8000/results"Get specific result by ID
curl "http://localhost:8000/results/abc-123-def-456"Delete specific result
curl -X DELETE "http://localhost:8000/results/abc-123-def-456"Get specific visualization
viz_type:detections,heatmap,overlay,confidence
curl "http://localhost:8000/visualization/abc-123/heatmap" > heatmap.pngGet system statistics
curl "http://localhost:8000/stats"Health check endpoint
curl "http://localhost:8000/health"# Terminal 1: Backend
cd backend
python main.py
# Terminal 2: Frontend
cd frontend
npm run devOpen browser: http://localhost:3000
- Click "New Inspection"
- Upload a product image (JPG or PNG < 10MB)
- Click "Analyze Image"
- Overview: See all visualizations
- Detections: Detailed list of found defects with confidence scores
- Heatmap: Model activation visualization
- Analysis: Statistical breakdown and quality assessment
- View all past inspections in Dashboard
- Sort by date, defect count, or severity
- Delete results as needed
Type: YOLO-based object detection Input: 416×416 normalized image Output: Bounding boxes with class labels and confidence Classes: crack, scratch, dent, discoloration, corrosion
Preprocessing:
- Bilateral filtering for denoising
- Resize with padding to maintain aspect ratio
- ImageNet normalization (mean/std)
- Channels-first format (CHW)
Type: CNN severity classifier Input: 224×224 cropped defect region Output: Severity level (minor/major/critical) with score
Features Extracted:
- Sharpness (Laplacian variance)
- Contrast (standard deviation)
- Area coverage (percentage)
- Edge density (Canny edges)
Fusion: Weighted combination of features → severity score
Grad-CAM Style:
- Gaussian peaks at detection centers
- Spatial smoothing with GaussianBlur
- JET colormap application
- Overlay blending (40% heatmap, 60% original)
Saliency Map:
- Canny edge detection
- Morphological dilation
- Gaussian smoothing
- Normalized to 0-255
Edit backend/models/detection.py:
# Replace mock detection with real YOLO
from ultralytics import YOLO
class DefectDetectionModel:
def __init__(self, model_path: str = None):
self.model = YOLO(model_path or 'yolov8m.pt')Edit backend/models/classification.py:
SEVERITY_THRESHOLDS = {
SeverityLevel.MINOR: (0.0, 0.35), # Adjust ranges
SeverityLevel.MAJOR: (0.35, 0.65),
SeverityLevel.CRITICAL: (0.65, 1.0)
}Edit backend/utils/fusion.py:
def __init__(self):
self.detection_weight = 0.6 # Change weights
self.classification_weight = 0.4Edit frontend/src/App.css:
/* Change primary color */
:root {
--primary: #667eea;
--secondary: #764ba2;
}-
Use GPU inference (if available):
- Install CUDA-compatible PyTorch
- Set
device='cuda'in models
-
Enable caching:
- Cache preprocessed images
- Store frequently accessed results
-
Parallel processing:
- Use FastAPI's async endpoints
- Process multiple images concurrently
-
Build for production:
npm run build
-
Enable compression on web server
-
Use CDN for static assets
# Windows
netstat -ano | findstr :8000
taskkill /PID <PID> /F
# macOS/Linux
lsof -i :8000
kill -9 <PID>Ensure backend is running and CORS is enabled in main.py:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)- Check file format (JPG, PNG only)
- Verify file size < 10MB
- Ensure
data/uploads/directory exists
- Ensure image quality is good (well-lit, clear)
- Check if defects are in trained classes
- Adjust confidence threshold in
detection.py
# Backend
FROM python:3.9-slim
WORKDIR /app
COPY backend/requirements.txt .
RUN pip install -r requirements.txt
COPY backend .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]# Frontend
FROM node:18-alpine
WORKDIR /app
COPY frontend/package.json .
RUN npm install
COPY frontend .
RUN npm run build
CMD ["npm", "run", "preview"]- Backend: AWS Lambda, Google Cloud Run, Azure Container Instances
- Frontend: Vercel, Netlify, CloudFront
- Storage: AWS S3, GCS, Azure Blob Storage
- Database: PostgreSQL for persistent results
from slowapi import Limiter
from slowapi.util import get_remote_address
limiter = Limiter(key_func=get_remote_address)
@app.post("/predict")
@limiter.limit("5/minute")
async def predict(request: Request, file: UploadFile):
# Implementationfrom sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
DATABASE_URL = "postgresql://user:password@localhost/qis"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)
# Store results in database instead of JSONfrom fastapi.security import HTTPBearer
security = HTTPBearer()
@app.post("/predict")
async def predict(credentials: HTTPAuthorizationCredentials = Depends(security)):
# Verify token# Install pytest
pip install pytest pytest-asyncio
# Run tests
pytest backend/tests/# Install testing libraries
npm install --save-dev @testing-library/react @testing-library/jest-dom vitest
# Run tests
npm run testContributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request
MIT License - See LICENSE file for details
For issues and questions:
- Create an issue on GitHub
- Contact: support@qualityinspection.local
- Initial release
- Core detection and classification
- Frontend dashboard
- Visualization suite
- Real YOLOv8 model integration
- Database persistence layer
- User authentication & authorization
- Multi-language support
- Mobile app (React Native)
- IoT device integration
- Advanced reporting & analytics
- Model training UI
- Batch processing API
- Real-time video processing
- OpenCV for computer vision utilities
- FastAPI for web framework
- React for UI framework
- YOLO for object detection inspiration
Quality Inspection System - Automated Defect Detection with Computer Vision & Deep Learning