Industrial Anomaly Detection
Overall block diagram
User browser
↓ HTTP :5000
┌────────────────────┐
│ Flask Web │
│ app.py │
└────────┬───────────┘
↓
┌────────────────────┐
│ PatchCore engine │
│ patchcore_train.py │ ← training (CPU builds the memory bank)
│ patchcore_test.py │ ← inference
└────────┬───────────┘
↓
┌────────────────────┐
│ RKNN3 Lite │ ← rknn3-toolkit-lite 1.0.0 installed
│ DINOv3-S backbone │
└────────────────────┘The board currently has Flask 3.1.3 (Debian 12 apt's
python3-flask); PatchCore's ML dependencies (numpy / scipy / torch, etc.) are not installed, sorun.sh startreportsModuleNotFoundError.
1. What Industrial Problems Can the Anomaly Detection Application Solve?
The RK182X anomaly detection application is based on the PatchCore algorithm + DINOv3-Small backbone, designed for industrial surface defect detection.
Actual path:
/userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/
Typical scenarios:
- Bottles: bubbles, scratches, dents
- Cables: insulation damage, deformed connectors
- Fabrics: holes, stains, weaving defects
- Metal surfaces: scratches, oxidation, corrosion
Application workflow: first train with good samples (building a feature memory bank), then run anomaly detection on test samples and output a heatmap.
2. Actual Deployment Status
Application directory:
ls /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/Output:
app.py
install.sh
model
patchcore_test.py
patchcore_train.py
pictures
README_CN.md
README_EN.md
run.sh
static
system
templates
THIRD_PARTY_LICENSES.txtTotal size:
du -sh /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/Output:
45M /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/Flask installation status:
python3 -c "import flask; print('Flask version:', flask.__version__)"Output:
Flask version: 3.1.3Flask 3.1.3 comes from Debian 12 apt's
python3-flask.
Basic functionality:
python3 -c "import flask; from flask import Flask, render_template; print('Flask基础功能可用')"Output:
Flask基础功能可用Model files:
ls -lh /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/model/Output:
总计 43M
-rw-r--r-- 1 linaro linaro 613K 2026年 1月28日 dinov3s.rknn
-rw-r--r-- 1 linaro linaro 42M 2026年 1月28日 dinov3s.weightPython scripts:
ls -la /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/*.pyOutput:
-rw-r--r-- 1 linaro linaro 26823 2026年 1月28日 app.py
-rw-r--r-- 1 linaro linaro 5726 2026年 1月28日 patchcore_test.py
-rw-r--r-- 1 linaro linaro 12168 2026年 1月28日 patchcore_train.pyPatchcore dependencies:
cd /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/system/install && unzip -p patchcore-0.1.0-py3-none-any.whl patchcore-0.1.0.dist-info/METADATA | grep -A 10 "Requires-Dist"Output:
Requires-Dist: click (>=8.0.3)
Requires-Dist: faiss-cpu
Requires-Dist: matplotlib (>=3.5.0)
Requires-Dist: pillow (>=8.4.0)
Requires-Dist: pretrainedmodels (>=0.7.4)
Requires-Dist: scikit-image (>=0.18.3)
Requires-Dist: scikit-learn (>=1.0.1)
Requires-Dist: scipy (>=1.7.1)
Requires-Dist: timm
Requires-Dist: torch (>=1.10.0)
Requires-Dist: torchvision (>=0.11.1)
Requires-Dist: tqdm (>=4.62.3)Installed related packages:
python3 -m pip list | grep -E "(click|scipy|tqdm)"Output:
click 8.4.2
tqdm 4.70.0
scipyis not installed —install.shonly installspython3-flask+patchcore-0.1.0-py3-none-any.whl; it does not install ML libraries such as scipy / numpy / torch / timm.
3. How to Deploy?
3.1 Push Files to the Board
# On the PC
adb push examples/anomaly-detection /userdata/3.2 Prepare the Dataset
Download the MVTec AD dataset:
https://www.mvtec.com/company/research/datasets/mvtec-adDirectory structure:
/userdata/datasets/
├── bottle/
│ ├── test/
│ │ ├── good/ ← good test samples
│ │ └── defect/ ← defective test samples (one subdirectory per defect type)
│ ├── train/good/ ← training uses only good samples
│ └── ground_truth/ ← defect annotations
├── cable/
│ ├── test/
│ └── train/
└── ...3.3 Install Dependencies
Startup script:
cat /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/run.sh | head -15Output:
#!/bin/bash -i
start() {
start-stop-daemon --start \
--pidfile /run/anomaly-detection.pid --make-pidfile \
--chdir /userdata/anomaly-detection \
--exec /usr/bin/python3 -- /userdata/anomaly-detection/app.py --webbrowser
}The script points to
/userdata/anomaly-detection, but the actual path is/userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection.
Installation:
cd /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection
./install.shinstall.sh installs:
python3-pippython3-flaskpatchcore-0.1.0-py3-none-any.whl
Important limitation: full operation requires many machine-learning dependency libraries (torch, torchvision, faiss-cpu, timm, pretrainedmodels, scikit-image, scikit-learn, numpy, etc.); these are hard to install on edge devices and consume substantial resources.
3.4 Start the Application
# Method 1: start from a terminal
/userdata/anomaly-detection/run.sh start
# Method 2: desktop icon (created by install.sh)
# Click "工业检测" (Industrial Inspection) on the desktop
# LAN access
http://<board IP>:50004. Workflow
4.1 Labeling (Training)
- Select a dataset in the web interface
- Click the "标注" (Label) button; the system extracts good-sample features with the DINOv3 backbone
- After labeling completes, the anomaly threshold is computed
4.2 Testing
- Select a labeled dataset on the "数据集测试" (Dataset Test) page
- Click "开始测试" (Start Test)
- Results are shown as heatmaps, green = normal, red = anomalous
Test page buttons:
| Button | Function |
|---|---|
| Dropdown | Select the dataset |
| 自动运行 (Auto run) | Whether to run continuously (pauses on anomaly by default) |
| 运行线程数 (Threads) | Set the number of parallel threads |
| 开始测试 (Start Test) | Start the test |
| 停止运行 (Stop) | Stop the current test |
| 连续运行 (Run all) | Detect every image without pausing |
| 运行至异常 (Run to anomaly) | Continue after an anomaly pause |
| 下一张 (Next) | Advance one image per thread |
| 热力图 (Heatmap) | Click the eye icon at the top-right of an image |
5. Actual Directory Structure
examples/anomaly-detection/
├── app.py # Flask web app (26KB)
├── patchcore_train.py # PatchCore training (12KB)
├── patchcore_test.py # PatchCore testing (5.7KB)
├── install.sh # Installation script
├── run.sh # Start/stop script
├── model/
│ ├── dinov3s.rknn # 613KB (DINOv3-S structure)
│ └── dinov3s.weight # 42MB (DINOv3-S weights)
├── README_CN.md / README_EN.md
├── pictures/ # Operation screenshots
│ ├── step1_cn.jpg # Chinese step screenshots
│ ├── step1_en.jpg # English step screenshots
│ └── ...
├── static/ # Flask static assets
│ ├── css/
│ ├── js/
│ ├── fontawesome-free-7.0.0-web/
│ ├── logo.png
│ └── datasets -> ../../datasets # symlink to the datasets
├── system/
│ ├── install/
│ │ ├── patchcore-0.1.0-py3-none-any.whl
│ │ └── pip.conf
│ ├── desktop/ # Desktop icon entry
│ └── lib/
└── templates/ # Flask HTML templates
└── index.html # Main page (6.9KB)Static assets:
ls /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/static/Output:
css
datasets
fontawesome-free-7.0.0-web
js
logo.pngTemplates:
ls -la /userdata/RK1820_RK1828_AI_SDK/examples/anomaly-detection/templates/Output:
-rw-r--r-- 1 linaro linaro 6983 6月 8日 14:28 index.html6. Prerequisites
The RK182X module is connected (PCIe):
systemctl is-active rknn3.serviceOutput:
activels -l /dev/pcie-rkep-*Output:
crw------- 1 root root 10, 124 8月20日 16:56 /dev/pcie-rkep-0004:41:00.0The MVTec AD dataset must be downloaded to /userdata/datasets/ (after actual deployment this directory does not exist yet; download and extract MVTec manually):
ls /userdata/datasets/Important limitation: full operation requires many machine-learning libraries (torch, torchvision, faiss-cpu, numpy, scipy, etc.); these are hard to install on edge devices and consume substantial resources (verified: none are installed).
7. FAQ
| Symptom | Cause | Fix |
|---|---|---|
run.sh start reports ModuleNotFoundError: No module named 'numpy' | scipy/numpy/torch not installed | install.sh does not install ML libs; pip install manually |
Flask fails with Address already in use | Port 5000 occupied | Find the occupier with ss -tlnp | grep 5000 |
dinov3s.rknn fails to load | rknn3-toolkit-lite not installed | apt install rknn3-toolkit-lite |
run.sh path not found | Points to /userdata/anomaly-detection, actually in the SDK path | Fix the path or create a symlink |
datasets symlink broken | /userdata/datasets not created | Create it manually + download MVTec AD |
| MVTec dataset missing some of the 15 classes | Incomplete download | Re-download the full tarball |
8. Next Steps
- ClawChips Architecture and Principles — ModelHub scheduling
- SKILL User Manual — the
rk-inspectinspection Skill - AI Agent Applications — OpenClaw integration
