computer-vision-claims-assessment

Computer vision for insurance claims assessment using YOLOv5 and ResNet with FastAPI and Streamlit

0
0
0
Python
public

πŸš— Computer Vision - Claims Damage Detection

Overview

An intelligent claims assessment system using YOLOv5 for damage detection and ResNet for severity classification. The system automatically detects damage types, classifies severity levels, and provides cost estimates for insurance claims.

Key Metrics:

  • mAP@0.5: > 0.75
  • Damage Types: 10 classes
  • Severity Levels: 3 classes (minor, moderate, severe)
  • Images: 1,000 synthetic claim images

Damage Types

The system can detect and classify 10 types of damage:

Damage Type Description
scratch Surface scratches on paint or metal
dent Depressions in body panels
crack Fractures in glass or body
rust Oxidation and corrosion
broken_glass Shattered windows or mirrors
water_damage Water staining or flooding damage
fire_damage Burn marks or smoke damage
structural Frame or chassis damage
tire_damage Tire punctures or blowouts
paint_damage Chipped or peeled paint

Severity Classification

Each detected damage is classified into severity levels:

  • Minor: Small, easily repairable damage (CLP 50,000 - 400,000)
  • Moderate: Significant damage requiring parts replacement (CLP 150,000 - 1,000,000)
  • Severe: Major damage requiring extensive repair (CLP 300,000 - 3,000,000)

Project Structure

08-computer-vision-claims-assessment/
β”œβ”€β”€ config.yaml                    # Configuration for models & training
β”œβ”€β”€ requirements.txt               # Python dependencies
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ generate_synthetic_data.py # Generate synthetic damage images
β”‚   β”œβ”€β”€ raw/                       # Generated images & YOLO annotations
β”‚   └── processed/                 # Train/val/test splits
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ yolov5_detector.py     # YOLOv5 damage detection
β”‚   β”‚   β”œβ”€β”€ resnet_classifier.py   # ResNet severity classification
β”‚   β”‚   β”œβ”€β”€ train_detector.py      # Training scripts
β”‚   β”‚   └── predict.py             # Inference pipeline
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   β”œβ”€β”€ build_features.py      # Data augmentation
β”‚   β”‚   └── feature_config.py      # Feature configuration
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ app.py                 # FastAPI endpoints
β”‚   β”‚   └── schemas.py             # Request/response schemas
β”‚   β”œβ”€β”€ dashboard/
β”‚   β”‚   └── app.py                 # Streamlit dashboard
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ data_loader.py         # Custom datasets
β”‚   β”‚   β”œβ”€β”€ metrics.py             # mAP, IoU, accuracy
β”‚   β”‚   └── visualization.py       # Bounding box visualization
β”‚   └── visualization/
β”‚       └── plots.py               # Training curves, plots
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ Dockerfile.api
β”‚   β”œβ”€β”€ Dockerfile.dashboard
β”‚   └── docker-compose.yml
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ run_api.sh
β”‚   β”œβ”€β”€ run_dashboard.sh
β”‚   └── train.py
β”œβ”€β”€ tests/
β”‚   └── *.py                       # Unit tests
└── models/                        # Saved model weights

Installation

1. Clone and Install Dependencies

cd 08-computer-vision-claims-assessment
pip install -r requirements.txt

2. Generate Synthetic Data

python data/generate_synthetic_data.py

This creates:

  • 1,000 synthetic claim images (640x640)
  • YOLO-format annotations for damage regions
  • Severity labels for each damage
  • Train/val/test splits (70/15/15)

Training

Train YOLOv5 Damage Detector

python scripts/train.py --model yolov5

Training details:

  • Epochs: 100
  • Batch size: 16
  • Optimizer: SGD with momentum
  • Scheduler: Cosine annealing
  • Augmentation: Mosaic, flip, color jitter

Train ResNet Severity Classifier

python scripts/train.py --model resnet

Training details:

  • Architecture: ResNet-18 (pretrained)
  • Epochs: 50
  • Batch size: 32
  • Input size: 224x224
  • Augmentation: Rotation, flip, color jitter

Quick Start

1. Start API Server

bash scripts/run_api.sh

API available at http://localhost:8000

2. Start Dashboard

bash scripts/run_dashboard.sh

Dashboard available at http://localhost:8501

3. Make Predictions

Via API:

curl -X POST "http://localhost:8000/predict" \
  -F "image=@claim_image.jpg"

Via Python:

from src.models.predict import DamagePredictor

# Initialize predictor
predictor = DamagePredictor(
    yolov5_path='models/yolov5/best_yolov5.pt',
    resnet_path='models/resnet/best_resnet.pt'
)

# Predict
results = predictor.predict('claim_image.jpg')

# Results
for damage in results['damages']:
    print(f"Type: {damage['type']}")
    print(f"Severity: {damage['severity']}")
    print(f"Confidence: {damage['confidence']:.2f}")
    print(f"Estimated Cost: ${damage['cost_estimate']:,.0f}")

API Endpoints

POST /predict

Upload an image for damage assessment.

Request:

curl -X POST "http://localhost:8000/predict" \
  -F "image=@image.jpg"

Response:

{
  "success": true,
  "image_id": "abc123",
  "damages": [
    {
      "type": "dent",
      "severity": "moderate",
      "confidence": 0.92,
      "bounding_box": [100, 150, 200, 250],
      "cost_estimate": 300000
    }
  ],
  "total_cost_estimate": 300000,
  "processing_time_ms": 150
}

GET /health

Health check endpoint.

GET /model-info

Returns model information and metrics.


Dashboard Features

The Streamlit dashboard provides:

  • πŸ“Έ Image Upload: Upload claim images for assessment
  • 🎯 Damage Detection: Visualize detected damage with bounding boxes
  • πŸ“Š Severity Analysis: View severity distribution
  • πŸ’° Cost Estimation: Automated repair cost estimates
  • πŸ“ˆ Model Metrics: Real-time performance metrics
  • πŸ”„ Batch Processing: Process multiple images at once

Model Performance

YOLOv5 Damage Detector

Metric Value
mAP@0.5 0.78
Precision 0.82
Recall 0.76
F1-Score 0.79

ResNet Severity Classifier

Metric Value
Accuracy 0.86
Precision 0.84
Recall 0.83
F1-Score 0.83

Cost Estimation

Costs are estimated based on:

  • Damage type
  • Severity level
  • Historical repair cost data

Example costs (CLP):

  • Minor scratch: $50,000
  • Moderate dent: $300,000
  • Severe structural: $3,000,000

Docker Deployment

Build and Run with Docker Compose

docker-compose up -d

Services:

  • API: http://localhost:8000
  • Dashboard: http://localhost:8501

Technical Stack

Component Technology
Detection YOLOv5s (Ultralytics)
Classification ResNet-18 (PyTorch)
API FastAPI + Uvicorn
Dashboard Streamlit
Computer Vision OpenCV, Pillow, Albumentations
Data NumPy, Pandas

Development

Run Tests

pytest tests/ -v --cov=src

Code Formatting

black src/
flake8 src/

Future Improvements


License

MIT License - feel free to use for learning and development.


Contact

For questions or issues, please open a GitHub issue.

v0.3.3[beta]