# Synthetic Defect Factory: Automated Optical Inspection (AOI) Digital Twin [![Python 3.11](https://img.shields.io/badge/Python-3.11-3776AB?logo=python&logoColor=white)](https://www.python.org/) [![OpenUSD](https://img.shields.io/badge/Pixar-OpenUSD-00599C?logo=nvidia&logoColor=white)](https://openusd.org/) [![Blender 5.2](https://img.shields.io/badge/Blender-5.2%20LTS-E87D0D?logo=blender&logoColor=white)](https://www.blender.org/) [![YOLOv8](https://img.shields.io/badge/YOLOv8-Ultralytics-00FFFF?logo=yolo&logoColor=black)](https://github.com/ultralytics/ultralytics) [![PyTorch](https://img.shields.io/badge/PyTorch-2.11%2BCUDA-EE4C2C?logo=pytorch&logoColor=white)](https://pytorch.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE) An end-to-end industrial digital twin and computer vision pipeline for automated optical inspection (AOI) in sheet-metal manufacturing. Synthesizes millimeter-accurate physical defect variations on CAD brackets in **Pixar OpenUSD**, renders photorealistic scenes with **Blender OptiX raytracing**, projects 100% mathematical **2D YOLO bounding boxes**, trains deep neural networks (**YOLOv8**), and runs real-time edge inference with an automated conveyor rejection HUD. --- ## 🏭 Overview & Engineering Motivation Modern industrial stamping and machining lines operate at defect rates below **0.1%** (>99.9% yield). While optimal for plant throughput, this creates an **AI Training Deadlock**: gathering 1,000 real-world defect images requires inspecting over 1,000,000 physical parts, taking months of line time and thousands of dollars in manual labeling. **Synthetic Defect Factory** solves this bottleneck through a **physics-calibrated 3D digital twin** and **procedural defect engine**: * **100% Automated Ground-Truth**: 2D YOLO bounding boxes are derived analytically from 3D vertex deformation math (**$0 labeling cost, 0 human error**). * **Pixar OpenUSD Compliance**: 118 station CAD primitives validated with **0 errors and 0 warnings** under Pixar's `UsdValidation` suite. * **Photorealistic Optics**: OptiX hardware-accelerated raytracing, physical LED ring illuminator, 50mm C-mount lens simulation, and domain randomization (yaw jitter $\pm 3^\circ$, lighting intensity $\pm 10\%$). * **Closed-Loop Physical Simulation**: Conveyor translation, photoelectric sensor triggering, real-time machine vision inspection HUD, and pneumatic cylinder rejection into a scrap chute. --- ## 📸 Inspection Workstation & AI Detection HUD | 3D Inspection Cell Digital Twin | Real-Time AI Defect Detection HUD | | :---: | :---: | | ![Workstation Overview](renders/samples/station_overview.png) | ![Inferred Detection HUD](renders/samples/inferred_detection_sample.png) | | *118 CAD primitives: Conveyor, ring light, Basler camera, and pneumatic reject diverter.* | *Real-time YOLOv8n inference (~12ms/frame) with automated REJECT signal.* | --- ## 🔬 Procedural Defect Classes The engine injects four physical defect categories directly into CAD bracket geometry alongside nominal parts: | Class | Type | Defect Mechanism | Preview | | :---: | :---: | :--- | :---: | | **0** | **Scratch** | Abrasive micro-ribbon with randomized directional yaw and specular roughness attenuation. | ![Scratch](renders/samples/preview_scratch.png) | | **1** | **Dent** | Radial $C^1$ Hermite/Gaussian crater deformation depressing surface vertices. | ![Dent](renders/samples/preview_dent.png) | | **2** | **Missing Part** | Fastener deactivation exposing internal tapped bore-hole geometry. | ![Missing Part](renders/samples/preview_missing_part.png) | | **3** | **Misplaced Part**| Cross-threaded screw simulation with multi-axis rotational tilt and vertical standoff. | ![Misplaced Part](renders/samples/preview_misplaced_part.png) | | **—** | **Good (Pristine)**| Nominal bracket with calibrated bevels and flush fasteners (outputs 0-byte label). | ![Good](renders/samples/preview_good.png) | --- ## 📊 Quantitative Benchmarks & Model Performance The model was trained on 1,000 synthetic inspection scenes (800 train / 200 validation) across balanced defect distributions: | Metric | Overall Performance | | :--- | :---: | | **mAP@50 (Overall)** | **85.43%** | | **mAP@50-95 (Overall)** | **84.00%** | | **Overall Precision** | **83.88%** | | **Overall Recall** | **93.75%** | | **Inference Latency** | **~12.8 ms / frame (~78 FPS)** | ### Per-Class Validation Breakdown * **Dent (Class 1)**: `99.50%` mAP@50 * **Missing Part (Class 2)**: `99.50%` mAP@50 * **Scratch (Class 0)**: `96.14%` mAP@50 * **Misplaced Part (Class 3)**: `40.87%` mAP@50 *(subtle 24° tilt detection)* *Detailed metrics exported to [`reports/model_metrics.json`](reports/model_metrics.json).* --- ## 🏗️ System Architecture ```mermaid graph TD subgraph S1["1. OpenUSD Stage Authoring"] A["make_base_usd.py"] --> B["station.usda (118 Prims)"] C["defects.py"] --> D["Procedural USD Overrides"] end subgraph S2["2. Headless Simulation & Rendering"] B & D --> E["Blender 5.2 (Cycles / EEVEE)"] E --> F["station.blend (Interactive Digital Twin)"] E --> G["render_dataset_batch.py (1,000 Scenes)"] end subgraph S3["3. Mathematical Ground Truth"] D --> H["Analytical 3D-to-2D Projection"] H --> I["YOLO Labels (800 Train / 200 Val)"] end subgraph S4["4. Deep Learning & Edge Vision"] G & I --> J["YOLOv8 Nano Training (train.py)"] J --> K["best.pt (Trained Weights)"] K --> L["infer_video.py (Real-Time In-Line Inspection HUD)"] end ``` --- ## 📁 Repository Structure ``` ├── blender/ │ ├── create_station_blend.py # Imports USD & builds Blender digital twin │ ├── render_dataset_batch.py # Headless batch renderer for synthetic scenes │ ├── render_animation_video.py # Compiles conveyor simulation into broadcast MP4 │ └── test_render_single.py # Single-frame render verification ├── config/ │ └── project_config.yaml # Global seeds, class IDs, optical specs, paths ├── dataset/ │ ├── data.yaml # YOLOv8 dataset configuration │ └── labels/ # 1,000 mathematically projected 2D YOLO annotations ├── renders/ │ ├── animation/ # 24 FPS broadcast inspection videos │ ├── final/ # Real-time AI-inferred HUD video │ └── samples/ # High-resolution benchmark renders & keyframes ├── reports/ │ ├── base_usd.md # USD 118-primitive engineering specification │ ├── defects_engine.md # Procedural deformation mathematical documentation │ └── model_metrics.json # Quantitative validation benchmarks ├── src/ │ ├── make_base_usd.py # Procedural OpenUSD workstation CAD generator │ ├── defects.py # Mathematical defect injection & 2D projection │ ├── generate_scenes.py # Dataset manifests & YOLO label batch creator │ ├── train.py # YOLOv8n GPU training pipeline │ ├── infer_video.py # Edge video inference & HUD overlay engine │ └── verify_labels.py # Ground-truth geometry & distribution validator ├── station.blend # Master 3D digital twin workstation scene (~293 KB) ├── usd/ │ ├── base/station.usda # Validated base OpenUSD inspection station │ └── manifests/ # 1,000 JSON scene metadata records ├── .gitignore ├── requirements.txt └── README.md ``` --- ## 🚀 Quickstart & Reproduction Guide ### 1. Environment Setup ```bash # Clone the repository git clone https://github.com/omkarande14/synthetic-defect-factory.git cd synthetic-defect-factory # Create and activate virtual environment python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies pip install -r requirements.txt ``` ### 2. Generate Base USD Station & Defect Manifests ```bash # Generate the 118-primitive OpenUSD workstation python src/make_base_usd.py # Generate 1,000 balanced scene manifests and exact 2D YOLO labels python src/generate_scenes.py # Verify label bounding box integrity python src/verify_labels.py ``` ### 3. Render Synthetic Imagery (Headless Blender) ```bash # Batch render 1,000 synthetic inspection scenes (800 train, 200 val) blender station.blend --background --python blender/render_dataset_batch.py ``` ### 4. Train YOLOv8 Model ```bash # Train YOLOv8 Nano on dedicated GPU python src/train.py ``` ### 5. Run In-Line Video AI Inference ```bash # Process conveyor inspection stream with machine vision HUD python src/infer_video.py ``` --- ## 📜 License Distributed under the MIT License. See `LICENSE` for more information.