#实战项目二:工业缺陷检测
📂 所属阶段:第六阶段 — 顶级综合项目实战
🔗 相关章节:实战项目一:智能人脸考勤系统 · 实战项目三:自动驾驶感知
#1. 系统架构
输入:产品图像
↓
预处理(标准化、增强)
↓
特征提取(CNN)
↓
异常检测(Isolation Forest / 深度学习)
↓
输出:合格/不合格#2. 异常检测方法
import torch
import torch.nn as nn
from sklearn.ensemble import IsolationForest
# 方法一:Isolation Forest
class DefectDetector:
def __init__(self):
self.model = IsolationForest(contamination=0.1)
def train(self, normal_images):
# 提取特征
features = self.extract_features(normal_images)
self.model.fit(features)
def detect(self, image):
features = self.extract_features([image])
prediction = self.model.predict(features)
return prediction[0] == -1 # -1 表示异常
def extract_features(self, images):
# 使用预训练 CNN 提取特征
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)
features = []
for img in images:
with torch.no_grad():
feat = model(img.unsqueeze(0))
features.append(feat.numpy())
return np.concatenate(features)#3. 深度学习异常检测
import torch
import torch.nn as nn
class AutoEncoder(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 32, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(64, 32, 4, stride=2, padding=1),
nn.ReLU(),
nn.ConvTranspose2d(32, 3, 4, stride=2, padding=1),
nn.Sigmoid(),
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
# 训练:只用正常产品图像
model = AutoEncoder()
optimizer = torch.optim.Adam(model.parameters())
criterion = nn.MSELoss()
for epoch in range(100):
for normal_image in normal_images:
output = model(normal_image)
loss = criterion(output, normal_image)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 检测:重建误差大 = 异常
def detect_defect(image):
output = model(image)
error = torch.mean((output - image) ** 2)
return error > threshold#4. 完整系统
class IndustrialDefectDetection:
def __init__(self):
self.model = AutoEncoder()
self.threshold = 0.05
def process_image(self, image_path):
# 读取图像
image = cv2.imread(image_path)
image = cv2.resize(image, (224, 224))
image = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0
# 检测
output = self.model(image.unsqueeze(0))
error = torch.mean((output - image.unsqueeze(0)) ** 2)
if error > self.threshold:
return "不合格", error.item()
else:
return "合格", error.item()
def process_video(self, video_path):
cap = cv2.VideoCapture(video_path)
results = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
status, error = self.process_image(frame)
results.append((status, error))
cap.release()
return results#5. 小结
工业缺陷检测关键技术:
1. 异常检测:Isolation Forest 或 AutoEncoder
2. 特征提取:预训练 CNN
3. 阈值设置:根据业务需求调整
4. 实时处理:视频流处理
性能指标:
- 检测速度:30+ FPS
- 准确率:95%+
- 误检率:< 5%
2026 年趋势:
- 多模态检测(RGB + 红外)
- 实时 3D 检测
- 边缘计算部署💡 记住:异常检测是工业 AI 的核心应用。掌握它,你就掌握了工业 4.0 的关键技术。
🔗 扩展阅读

