CoolFace
Apppublic

ahmedg12104/Stegno-image-analysis

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
App README

⦿ STEGANOSCAN — Neural Forensics Terminal v4.2

A Flask-based neural forensics web application for image steganalysis, tamper detection, object detection/tracking, and AI-generated image identification. Trained on 16,505 images and validated on 4,127 images with a 256-dimensional feature vector extracted via SRM (Spatial Rich Model) filters and related steganalysis techniques.

Live Demo: HuggingFace Space

Table of Contents

  1. 1.Features
  2. 2.Architecture Overview
  3. 3.Modules
  4. 4.Config (`modules/config.py`)
  5. 5.YOLO Detector (`modules/yolo_detector.py`)
  6. 6.Stego Analyzer (`modules/stego_analyzer.py`)
  7. 7.Residual Heatmap (`modules/residual_heatmap.py`)
  8. 8.ELA Forensics (`modules/ela_forensics.py`)
  9. 9.Text OCR (`modules/text_ocr.py`)
  10. 10.GAN Detector (`modules/gan_detector.py`)
  11. 11.Utils (`modules/utils.py`)
  12. 12.Trackers (`modules/trackers/`)
  13. 13.Feature Extraction Pipeline (256-dim)
  14. 14.ML Models
  15. 15.Steganalysis Models
  16. 16.YOLO Models
  17. 17.GAN Detection Model
  18. 18.API Endpoints
  19. 19.UI / Frontend
  20. 20.Installation & Setup
  21. 21.Docker & HuggingFace Spaces
  22. 22.Project Structure
  23. 23.Dependencies

Features

  • Steganalysis Detection — SVM + URD ensemble models detect hidden data in images using 18 feature families (256-dim)
  • YOLO Object Detection — YOLOv8x/YOLOv8s/YOLO11x with SRM residual heatmap overlay
  • Multi-Object Tracking — 5 trackers: ByteTrack, Bot-SORT, Deep SORT, Strong SORT, OC-SORT
  • Error Level Analysis (ELA) — JPEG tamper detection without ML models
  • OCR Text Detection — EasyOCR-based text region extraction
  • GAN/AI Image Detection — Vision Transformer (ViT) + frequency heuristic hybrid
  • All-in-One Forensics Pipeline — Run all 5 analyses in a single request
  • Cyberpunk Terminal UI — Matrix rain, CRT scanlines, glitch animations, glass-morphism panels
  • Interactive Charts — Radar chart, per-dataset bar chart, animated GIF metrics, probability gauge
  • Drag-and-Drop — Image upload via click or drag-and-drop on all analysis tabs

Architecture Overview

User Browser (HTML/JS/CSS)
        │
        ▼ HTTP
  Flask Web Server (app.py)
        │
        ├── /api/predict  ──► Feature Extraction (256-dim) ──► SVM / URD
        ├── /api/yolo/*   ──► YOLODetector + ResidualHeatmap
        ├── /api/forensics/*
        │   ├── ela       ──► ELAAnalyzer
        │   ├── ocr       ──► TextDetector (EasyOCR)
        │   ├── gan       ──► GANDetector (ViT + FFT heuristics)
        │   └── full      ──► All of the above + steganalysis + YOLO
        ├── /api/tracker/* ──► Tracker Factory (5 trackers)
        ├── /api/metrics   ──► Validation metrics + charts
        └── /              ──► index.html (SPA frontend)

Modules

Config (modules/config.py)

Central configuration constants for all modules:

SettingValueDescription
YOLO_CONF_THRESHOLD0.25Detection confidence threshold
YOLO_IOU_THRESHOLD0.45NMS IoU threshold
YOLO_DEVICEcpuInference device
YOLO_DEFAULTyolov8xDefault YOLO model
YOLO_MODEL_REGISTRYyolov8x, yolov8s, yolo11xAvailable models
HEATMAP_WINDOW32SRM sliding window size
HEATMAP_STRIDE16SRM sliding window stride
HEATMAP_SENSITIVITY1.5Heatmap sensitivity multiplier
ELA_QUALITY85JPEG re-compression quality
ELA_SCALE15ELA error amplification scale
OCR_LANGUAGES("en",)EasyOCR language list
OCR_GPUFalseUse GPU for OCR
GAN_MODEL_REPOdima806/deepfake_vs_real_image_detectionHF model for GAN detection
MAX_IMAGE_SIZE1024Max dimension for analysis
PREDICT_MAX_SIZE512Max dimension for steganalysis
ALLOWED_EXTENSIONS.png,.jpg,.jpeg,.tiff,.webp,.bmpSupported image formats
DEFAULT_TRACKERbytetrackDefault tracking algorithm

YOLO Detector (modules/yolo_detector.py)

YOLODetector

Wraps Ultralytics YOLO for object detection.

MethodDescription
detect(image)Run inference → list of detections with box, confidence, class_id, label
detect_stego_regions(image, srm_energy_map)Detect + enrich each detection with mean SRM energy
export_onnx()Export model to ONNX format
get_model_info()Model metadata: task, classes, param count
YOLOPool

Multi-model manager with lazy loading.

MethodDescription
get_model(name)Get or create a detector instance
switch(name)Swap active model
list_models()Return registry descriptions

Models:

NameParamsSizeDescription
yolov8x68M130MBHighest accuracy (default)
yolov8s11M22MBFastest inference
yolo11x~70M130MBLatest architecture

Stego Analyzer (modules/stego_analyzer.py)

StegoAnalyzer

Orchestrates combined steganalysis analysis:

  1. 1.Runs YOLO detection on the image
  2. 2.Computes SRM residual heatmap
  3. 3.For each detected region, extracts 256-dim features and runs SVM + URD prediction
  4. 4.Returns per-detection probabilities, hotspot contours, and aggregate scores

Residual Heatmap (modules/residual_heatmap.py)

ResidualHeatmap

Computes SRM (Spatial Rich Model) energy maps:

  • Applies 3 SRM filters: f1h (horizontal), f1v (vertical), sq3 (square 3×3)
  • Sliding-window aggregation (32×32 window, 16 stride)
  • Generates COLORMAP_JET overlay
  • Extracts stego hotspot contours (regions with energy > mean + sensitivity × std)
  • Returns heatmap image (base64) and hotspot polygons

ELA Forensics (modules/ela_forensics.py)

ELAAnalyzer

JPEG tamper detection without ML models:

  1. 1.Re-saves image at JPEG quality ELA_QUALITY (default 85)
  2. 2.Computes pixel-wise absolute difference between original and re-encoded
  3. 3.Scales error by ELA_SCALE (default 15×)
  4. 4.Thresholds to find suspicious regions (error > 95th percentile)
  5. 5.Returns:
  6. 6.ela_score — mean error
  7. 7.mean_error, max_error, p95_error
  8. 8.suspicious_ratio — fraction of pixels above threshold
  9. 9.suspicious_regions — bounding contours > 50 px²
  10. 10.Overlay heatmap (COLORMAP_HOT)
  11. 11.Verdict: "MANIPULATED" (score > 0.3) or "ORIGINAL"

Text OCR (modules/text_ocr.py)

TextDetector

Wraps EasyOCR (default: English, CPU).

MethodDescription
analyze(image)Detect text → returns num_text_regions, total_characters, avg_confidence, avg_region_area, per-region details (box, text, confidence, length)
draw_text_regions(image)Annotate image with bounding boxes and recognized text

GAN Detector (modules/gan_detector.py)

GANDetector

Two-pronged AI-generated image detection:

1. ViT Model (HuggingFace)

  • Model: dima806/deepfake_vs_real_image_detection
  • Vision Transformer fine-tuned for real vs. fake classification
  • Lazy-loaded on first use
  • Gracefully falls back to heuristics if model unavailable

2. Heuristic Analysis

  • FFT Frequency Features: radial power spectrum, high/mid/low frequency ratios, spectral entropy
  • Noise Correlation: local noise std, spatial noise correlation

Combined Score: 0.7 × model_probability + 0.3 × heuristic_probability

Verdict: "AI_GENERATED" (> 0.5) or "NATURAL"


Utils (modules/utils.py)

FunctionDescription
load_image(path, max_size)Read and resize image
image_to_base64(img) / base64_to_image(b64)Base64 encode/decode
rgb_to_gray(img)Color conversion
xyxy_to_xywh() / xywh_to_xyxy() / clip_box()Bounding box operations
iou(box_a, box_b)Intersection-over-Union
draw_detections(img, detections)Annotate image with boxes (green < 0.5 stego prob, red ≥ 0.5)
serialize_result(obj)Recursive numpy‑to‑native Python conversion for JSON

Trackers (modules/trackers/)

FileClassBackendDescription
base_tracker.pyBaseTracker (ABC)Abstract interface: track(), get_tracked_objects(), reset()
byte_tracker.pyByteTrackerUltralytics bytetrack.yamlLightweight, fast
bot_sort_tracker.pyBotSortTrackerUltralytics botsort.yamlrobust re-identification
deep_sort_tracker.pyDeepSortTrackerboxmot.DeepSortAppearance-based
strong_sort_tracker.pyStrongSortTrackerboxmot.StrongSortRobust with reID
oc_sort_tracker.pyOCSortTrackerboxmot.OCSortObservation-centric
tracker_factory.pyFactoryget_tracker(name, model, config), list_trackers()

All trackers accept a YOLO model and return tracked objects with track_id, box, confidence, class_id, label.


Feature Extraction Pipeline (256-dim)

The 256-dimensional feature vector is assembled from 18 feature families:

#FamilyDimsDescription
1SRM gray5010 SRM filters × 5 histogram bins (gray channel)
2SRM RGB183 channels × 3 filters × 2 stats (mean, std)
3LSB entropy123 channels × 4 bit-planes
4Chi-square3Per-channel chi-square statistic
5Statistical moments123 channels × 4 moments (mean, var, skew, kurt)
6Gradient features2Gradient entropy + Laplacian variance
7FFT frequency2Beta slope + high-frequency ratio
8Run-length encoding1LSB run-length uniformity
9Color correlation3RGB channel correlation
10Wavelet features3LH, HL, HH subband std (Haar DWT)
11Markov transition4LSB 2×2 transition probabilities
12Benford's law deviation2Pixel + first-difference Benford fit
13PVD flatness6Pixel-value differencing over 6 ranges
14GLCM features8Contrast, energy, homogeneity, correlation (4 angles × 2 stats)
15Weighted stego (WS)4WS statistics per channel
16RS analysis6Regular/singular group statistics
17JPEG calibration3JPEG compression fingerprint
18LSB 4-gram164-bit LSB pattern frequencies

ML Models

Steganalysis Models

ModelFileTypeAUCAccuracyDescription
Robust SVMrobust_svm.pklSVM + PCA + StandardScaler0.860577.20%Primary steganalysis model
URDurd.pklStacking ensemble (SVM + tree) + meta-model0.873877.44%Unified Robust Detector

Both models auto-download from GitHub Releases if missing:

https://github.com/ahmedA-gif/cv-project-stegno-analysis/releases/download/v1.0.0/robust_svm.pkl
https://github.com/ahmedA-gif/cv-project-stegno-analysis/releases/download/v1.0.0/urd.pkl

Per-Dataset AUC (from validation):

DatasetSVM AUCURD AUCSVM AccURD Acc
ALASKA20.93050.9387
BOSSBASE0.65650.6572
IPHONE0.86410.9174
STEGANAYIS0.76100.7780
STEGO-PVD0.85300.8479
STEGOIMAGES0.98560.9863
UCID0.95490.9614

YOLO Models

NameParamsSizeTask
yolov8x68.3M130MBObject detection (default)
yolov8s11.2M22MBObject detection
yolo11x~70M130MBObject detection

GAN Detection Model

ModelSourceTypeInput
dima806/deepfake_vs_real_image_detectionHuggingFace HubViT (Vision Transformer)224×224 RGB

API Endpoints

System & Status

MethodEndpointDescription
GET/Serve the main HTML UI
GET/api/statusServer status, model load state, version (4.2.0), feature dim
GET/api/metricsValidation metrics for SVM + URD (AUC, accuracy, per-dataset)
GET/api/metrics/animatedAnimated GIF — per-dataset bar chart (30 frames)
GET/api/metrics/histogramStatic PNG — per-dataset AUC + Accuracy histogram

Steganalysis Prediction

MethodEndpointDescription
POST/api/predictUpload image → extract 256-dim features → SVM + URD → probability + verdict

Request: multipart/form-data with image field. Response:

json
{
  "success": true,
  "inference_time": 1.234,
  "results": {
    "urd": {
      "prediction": "STEGO",
      "probability": 0.8912,
      "threshold": 0.570,
      "confidence": 89.12
    },
    "robust_svm": {
      "prediction": "STEGO",
      "probability": 0.7234,
      "threshold": 0.490,
      "confidence": 72.34
    }
  }
}

YOLO Detection

MethodEndpointDescription
GET/api/yolo/statusYOLO model load status and metadata
GET/api/yolo/modelsList available models and active one
POST/api/yolo/switchSwitch active model ({"model": "yolov8s"})
POST/api/yolo/detectUpload image → YOLO detection + SRM heatmap → annotated + heatmap images
POST/api/yolo/analyzeFull analysis: YOLO + per-region stego probabilities + hotspot contours

Tracker

MethodEndpointDescription
GET/api/tracker/listList available tracker algorithms and active one
POST/api/tracker/selectSelect active tracker ({"tracker": "deepsort"})
POST/api/tracker/trackUpload image → YOLO + tracker → annotated image with track IDs

Forensics

MethodEndpointDescription
POST/api/forensics/elaError Level Analysis → tamper score, suspicious regions, ELA heatmap
POST/api/forensics/ocrOCR text detection → text regions, character count, annotated image
POST/api/forensics/ganGAN/AI detection → ViT + heuristic probability, frequency/noise analysis
POST/api/forensics/fullAll-in-one: steganalysis + YOLO + ELA + OCR + GAN

UI / Frontend

Single-page application served from templates/index.html with a cyberpunk/hacker-terminal theme.

Tabs

TabFeatures
Scan_TargetImage upload → steganalysis gauge + verdict + stats (chi-square, RS, entropy, LSB) + spectral variance bars + terminal log
YOLO_DetectModel selector + image upload → annotated image + SRM heatmap + detections table
ForensicsSub-tabs: [ELA], [OCR], [GAN], [ALL-IN-1] → color-coded results with typing animation, raw JSON expand
TrackerAlgorithm selector + image upload → tracked objects with persistent IDs
MetricsModel performance cards + radar chart + per-dataset bar chart + animated GIF + score list

Visual Theme

  • Colors: Neon green (#00ff41), cyan (#00eefc), amber (#ffb000), dark background (#050505)
  • Effects: CRT scanlines overlay, Matrix rain (katakana + ASCII), glass-morphism panels, glitch/flicker animations, animated border pulses
  • Typography: JetBrains Mono (monospace)
  • Custom scrollbar styled in neon green

Installation & Setup

Prerequisites

  • Python 3.12+
  • pip

Local Setup

bash
# 1. Clone the repository
git clone https://github.com/ahmedA-gif/cv-project-stegno-analysis.git
cd cv-project-stegno-analysis

# 2. Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate   # Linux/Mac
# venv\Scripts\activate    # Windows

# 3. Install dependencies
pip install -r requirements.txt

# 4. Run the app
python app.py

The app starts at http://localhost:5050.

Models (robust_svm.pkl, urd.pkl) auto-download from GitHub Releases on first startup. YOLO weights download from Ultralytics on first use.

Configuration

Edit modules/config.py to adjust:

  • YOLO confidence/IoU thresholds
  • Heatmap sensitivity
  • ELA quality/scale
  • OCR languages
  • Max image dimensions

Docker & HuggingFace Spaces

Docker Build

bash
docker build -t steganoscan .
docker run -p 7860:7860 steganoscan

HuggingFace Spaces

The project is configured for Docker-based HuggingFace Spaces:

  1. 1.Fork/push the repo to a HF Space
  2. 2.Space SDK: Docker
  3. 3.Port: 7860
  4. 4.The Space auto-builds on push

Environment variables (optional):

  • PORT — server port (default: 5050 dev, 7860 Docker)

Project Structure

cv-project/
├── app.py                      # Flask application (1007 lines)
├── Dockerfile                  # Docker image for HF Spaces
├── requirements.txt            # Python dependencies
├── README.md                   # This file
├── manifest.json               # Training metadata
├── robust_svm.pkl              # SVM model (auto-downloaded)
├── urd.pkl                     # URD ensemble model (auto-downloaded)
├── yolov8x.pt                  # YOLOv8x weights
├── yolov8s.pt                  # YOLOv8s weights
├── yolo11x.pt                  # YOLO11x weights
├── validation_metrics_animated.gif
│
├── templates/
│   └── index.html              # Single-page UI (718 lines)
│
├── static/                     # Static assets
│
├── uploads/                    # Temporary uploads (auto-cleaned)
│
├── modules/
│   ├── __init__.py
│   ├── config.py               # Global configuration
│   ├── yolo_detector.py        # YOLO detection + model pool
│   ├── stego_analyzer.py       # Combined stego region analyzer
│   ├── residual_heatmap.py     # SRM heatmap generation
│   ├── ela_forensics.py        # Error Level Analysis
│   ├── text_ocr.py             # EasyOCR text detection
│   ├── gan_detector.py         # GAN/AI image detection
│   ├── utils.py                # Helper functions
│   └── trackers/
│       ├── __init__.py
│       ├── base_tracker.py     # Abstract interface
│       ├── byte_tracker.py     # ByteTrack
│       ├── bot_sort_tracker.py # Bot-SORT
│       ├── deep_sort_tracker.py# Deep SORT
│       ├── strong_sort_tracker.py # Strong SORT
│       ├── oc_sort_tracker.py  # OC-SORT
│       └── tracker_factory.py  # Factory + registry
│
└── Phase 2/                    # Earlier version (identical structure)

Dependencies

PackageVersionPurpose
flask≥3.0Web framework
gunicorn≥22.0Production WSGI server
numpy≥1.24, <2.0Numerical computation
scipy≥1.10Signal processing (convolve2d, gaussian_filter)
scikit-learn≥1.3ML models (SVM, PCA, scalers, ensemble)
opencv-python-headless≥4.8Computer vision
Pillow≥10.0Image I/O
joblib≥1.3Model serialization
matplotlib≥3.7Charts and GIF generation
xgboost≥2.0URD base model
lightgbm≥4.0URD base model
ultralytics≥8.2YOLO models
boxmot≥10.0Multi-object trackers
easyocr≥1.7OCR text detection
transformers≥4.36, <5.0.0HuggingFace ViT for GAN detection
huggingface-hub≥0.20Model download from HF Hub

License

MIT