工业产品质量检测是制造业的关键环节,传统人工质检效率低且容易疲劳。本系统将结合计算机视觉和深度学习技术,开发一个能够自动检测产品缺陷、测量关键尺寸并识别标记的智能质检平台。系统可部署在生产线上实现7×24小时不间断检测,大幅提升质检效率和准确性。
industrial_vision/
│── core/ # 核心模块
│ │── defect_detection.py # 缺陷检测
│ │── dimension_measure.py # 尺寸测量
│ └── mark_recognition.py # 标记识别
│── models/ # 模型定义
│ │── cnn_models.py # CNN模型
│ └── yolo_models.py # YOLO模型
│── utils/ # 工具类
│ │── image_processing.py # 图像处理
│ └── factory_interface.py # 工厂接口
│── configs/ # 配置文件
│ │── camera_config.yaml # 相机配置
│ └── product_configs/ # 产品特定配置
│── main.py # 主程序
└── requirements.txt # 依赖库
import cv2
import numpy as np
from tensorflow.keras.models import load_model
from typing import List, Dict
class DefectDetector:
"""工业产品缺陷检测器"""
def __init__(self, model_path: str, threshold: float = 0.9):
self.model = load_model(model_path)
self.threshold = threshold
self.class_names = ['正常', '划痕', '凹陷', '污渍', '裂纹']
def preprocess_image(self, image: np.ndarray) -> np.ndarray:
"""预处理工业产品图像"""
# 自适应直方图均衡化
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
cl = clahe.apply(l)
limg = cv2.merge((cl,a,b))
enhanced = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
# 标准化
normalized = enhanced / 255.0
return normalized
def detect_defects(self, image: np.ndarray) -> List[Dict]:
"""检测图像中的缺陷"""
# 预处理
processed = self.preprocess_image(image)
input_tensor = np.expand_dims(processed, axis=0)
# 预测
predictions = self.model.predict(input_tensor)[0]
# 解析结果
results = []
for i, prob in enumerate(predictions):
if prob > self.threshold and i != 0: # 忽略"正常"类别
results.append({
'class': self.class_names[i],
'confidence': float(prob),
'severity': self._calculate_severity(prob)
})
return results
def _calculate_severity(self, probability: float) -> str:
"""根据概率计算缺陷严重程度"""
if probability > 0.98:
return 'critical'
elif probability > 0.95:
return 'major'
else:
return 'minor'
def visualize_defects(self, image: np.ndarray, defects: List[Dict]) -> np.ndarray:
"""可视化缺陷检测结果"""
vis_image = image.copy()
for defect in defects:
# 在图像上绘制缺陷信息
label = f"{
defect['class']} ({
defect['severity']})"
cv2.putText(vis_image, label, (20, 40 + 30 * len(results)),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
return vis_image