1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// 工业检测高级(v57.0.0)
// 缺陷检测、尺寸测量、表面质量检测、产品分类、OCR工业、异常检测
///|
/// 工业检测任务类型
pub(all) enum IndustrialInspectionTaskType {
DefectDetection
DimensionalMeasurement
SurfaceQualityInspection
ProductClassification
IndustrialOCR
AnomalyDetection
AssemblyVerification
Unknown
} derive(Eq)
///|
/// 缺陷类型
pub(all) enum DefectType {
Scratch
Dent
Crack
Stain
Rust
Bubble
Wrinkle
Discoloration
MissingPart
Misalignment
Unknown
} derive(Eq)
///|
/// 表面质量等级
pub(all) enum SurfaceQualityGrade {
Excellent
Good
Acceptable
Poor
Reject
Unknown
} derive(Eq)
///|
/// 检测到的缺陷
pub(all) struct DetectedDefect {
defect_id : Int
defect_type : DefectType
bounding_box : Array[Int]
segmentation_mask : Array[Array[Bool]]
confidence : Double
severity : Double
area : Double
perimeter : Double
position : Array[Double]
is_critical : Bool
}
///|
/// 尺寸测量结果
pub(all) struct DimensionalMeasurementResult {
measurement_name : String
measured_value : Double
nominal_value : Double
tolerance_upper : Double
tolerance_lower : Double
deviation : Double
is_within_tolerance : Bool
unit : String
measurement_method : String
uncertainty : Double
}
///|
/// 表面质量检测结果
pub(all) struct SurfaceQualityResult {
overall_grade : SurfaceQualityGrade
overall_score : Double
roughness : Double
waviness : Double
flatness : Double
glossiness : Double
color_uniformity : Double
defect_density : Double
num_defects : Int
critical_defects : Int
}
///|
/// 产品分类结果
pub(all) struct ProductClassificationResult {
product_id : String
predicted_class : String
confidence : Double
all_classes : Array[String]
all_scores : Array[Double]
is_accepted : Bool
rejection_reason : String
inspection_time_ms : Double
}
///|
/// 异常检测结果
pub(all) struct AnomalyDetectionResult {
is_anomaly : Bool
anomaly_score : Double
threshold : Double
anomaly_map : Array[Array[Double]]
anomaly_regions : Array[Array[Int]]
num_anomaly_regions : Int
confidence : Double
reconstruction_error : Double
}
///|
/// 工业检测配置
pub(all) struct IndustrialInspectionConfig {
task_type : IndustrialInspectionTaskType
defect_types : Array[DefectType]
detection_threshold : Double
segmentation_threshold : Double
severity_threshold : Double
measurement_unit : String
tolerance_default : Double
surface_quality_threshold : Double
anomaly_threshold : Double
use_ai_detection : Bool
use_traditional_vision : Bool
use_hybrid : Bool
camera_resolution : Array[Int]
lighting_type : String
inspection_speed : Double
reject_on_critical : Bool
auto_calibration : Bool
}
///|
/// 创建默认工业检测配置
pub fn create_default_industrial_config() -> IndustrialInspectionConfig {
IndustrialInspectionConfig::{
task_type: IndustrialInspectionTaskType::DefectDetection,
defect_types: [
DefectType::Scratch,
DefectType::Dent,
DefectType::Crack,
DefectType::Stain,
],
detection_threshold: 0.5,
segmentation_threshold: 0.5,
severity_threshold: 0.7,
measurement_unit: "mm",
tolerance_default: 0.1,
surface_quality_threshold: 0.8,
anomaly_threshold: 0.5,
use_ai_detection: true,
use_traditional_vision: false,
use_hybrid: true,
camera_resolution: [2048, 2048],
lighting_type: "ring_light",
inspection_speed: 100.0,
reject_on_critical: true,
auto_calibration: true,
}
}
///|
/// 检测缺陷(简化版)
pub fn detect_defects(
image : Array[Double],
width : Int,
height : Int,
config : IndustrialInspectionConfig,
) -> Array[DetectedDefect] {
ignore(image)
let defects : Array[DetectedDefect] = []
// 简化的缺陷检测:基于图像强度变化
let mut state = 42
let num_defects = 5
for i in 0..<num_defects {
state = (state * 11035 + 12345) % 1000000
let x = state % (width - 100)
state = (state * 11035 + 12345) % 1000000
let y = state % (height - 100)
state = (state * 11035 + 12345) % 1000000
let w = state % 50 + 20
state = (state * 11035 + 12345) % 1000000
let h = state % 50 + 20
state = (state * 11035 + 12345) % 1000000
let confidence = (state % 1000).to_double() / 1000.0 * 0.5 + 0.5
state = (state * 11035 + 12345) % 1000000
let severity = (state % 1000).to_double() / 1000.0
let defect_types = [
DefectType::Scratch,
DefectType::Dent,
DefectType::Crack,
DefectType::Stain,
DefectType::Rust,
]
let defect_type = defect_types[i % defect_types.length()]
let bbox : Array[Int] = []
bbox.push(x)
bbox.push(y)
bbox.push(x + w)
bbox.push(y + h)
let area = w.to_double() * h.to_double()
let perimeter = 2.0 * (w.to_double() + h.to_double())
let defect = DetectedDefect::{
defect_id: i,
defect_type,
bounding_box: bbox,
segmentation_mask: [],
confidence,
severity,
area,
perimeter,
position: [
x.to_double() + w.to_double() / 2.0,
y.to_double() + h.to_double() / 2.0,
],
is_critical: severity > config.severity_threshold,
}
defects.push(defect)
}
defects
}
///|
/// 测量尺寸(简化版)
pub fn measure_dimensions(
image : Array[Double],
width : Int,
height : Int,
calibration_factor : Double,
config : IndustrialInspectionConfig,
) -> Array[DimensionalMeasurementResult] {
ignore(calibration_factor)
ignore(height)
ignore(image)
ignore(width)
let measurements : Array[DimensionalMeasurementResult] = []
// 简化的尺寸测量
let measurement_names = [
"width", "height", "diameter", "thickness", "hole_diameter",
]
let nominal_values = [100.0, 50.0, 25.0, 5.0, 10.0]
let mut state = 42
for i in 0..<measurement_names.length() {
state = (state * 11035 + 12345) % 1000000
let deviation = ((state % 1000).to_double() / 1000.0 - 0.5) *
config.tolerance_default *
2.0
let measured = nominal_values[i] + deviation
let upper = nominal_values[i] + config.tolerance_default
let lower = nominal_values[i] - config.tolerance_default
let result = DimensionalMeasurementResult::{
measurement_name: measurement_names[i],
measured_value: measured,
nominal_value: nominal_values[i],
tolerance_upper: upper,
tolerance_lower: lower,
deviation,
is_within_tolerance: measured >= lower && measured <= upper,
unit: config.measurement_unit,
measurement_method: "vision",
uncertainty: 0.01,
}
measurements.push(result)
}
measurements
}
///|
/// 评估表面质量(简化版)
pub fn evaluate_surface_quality(
image : Array[Double],
width : Int,
height : Int,
defects : Array[DetectedDefect],
config : IndustrialInspectionConfig,
) -> SurfaceQualityResult {
ignore(config)
ignore(image)
// 简化的表面质量评估
let defect_area_ratio = if defects.length() > 0 {
let mut total_area = 0.0
for d in defects {
total_area += d.area
}
total_area / (width.to_double() * height.to_double())
} else {
0.0
}
let overall_score = 1.0 - defect_area_ratio * 10.0
let grade = if overall_score > 0.9 {
SurfaceQualityGrade::Excellent
} else if overall_score > 0.8 {
SurfaceQualityGrade::Good
} else if overall_score > 0.7 {
SurfaceQualityGrade::Acceptable
} else if overall_score > 0.6 {
SurfaceQualityGrade::Poor
} else {
SurfaceQualityGrade::Reject
}
let mut critical_count = 0
for d in defects {
if d.is_critical {
critical_count += 1
}
}
SurfaceQualityResult::{
overall_grade: grade,
overall_score,
roughness: 0.5,
waviness: 0.3,
flatness: 0.8,
glossiness: 0.7,
color_uniformity: 0.9,
defect_density: defect_area_ratio,
num_defects: defects.length(),
critical_defects: critical_count,
}
}
///|
/// 异常检测(简化版)
pub fn detect_anomalies(
image : Array[Double],
reference : Array[Double],
width : Int,
height : Int,
config : IndustrialInspectionConfig,
) -> AnomalyDetectionResult {
ignore(height)
ignore(width)
// 简化的异常检测:计算与参考图像的差异
let mut total_error = 0.0
let anomaly_map : Array[Array[Double]] = []
let anomaly_regions : Array[Array[Int]] = []
let min_len = if image.length() < reference.length() {
image.length()
} else {
reference.length()
}
for i in 0..<min_len {
let diff = (image[i] - reference[i]).abs()
total_error += diff
}
let avg_error = if min_len > 0 {
total_error / min_len.to_double()
} else {
0.0
}
let is_anomaly = avg_error > config.anomaly_threshold
let confidence = if is_anomaly {
(avg_error - config.anomaly_threshold) * 2.0
} else {
1.0 - avg_error
}
AnomalyDetectionResult::{
is_anomaly,
anomaly_score: avg_error,
threshold: config.anomaly_threshold,
anomaly_map,
anomaly_regions,
num_anomaly_regions: 0,
confidence,
reconstruction_error: avg_error,
}
}
///|
/// 生成工业检测报告
pub fn industrial_inspection_report_to_string(
task_type : IndustrialInspectionTaskType,
config : IndustrialInspectionConfig,
result_description : String,
) -> String {
let mut report = "## 工业检测高级报告\n\n"
report = report + "### 任务信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 任务类型 | " +
(match task_type {
IndustrialInspectionTaskType::DefectDetection => "缺陷检测"
IndustrialInspectionTaskType::DimensionalMeasurement => "尺寸测量"
IndustrialInspectionTaskType::SurfaceQualityInspection =>
"表面质量检测"
IndustrialInspectionTaskType::ProductClassification => "产品分类"
IndustrialInspectionTaskType::IndustrialOCR => "工业OCR"
IndustrialInspectionTaskType::AnomalyDetection => "异常检测"
IndustrialInspectionTaskType::AssemblyVerification => "装配验证"
_ => "未知"
}) +
" |\n"
report = report +
"| 检测阈值 | " +
config.detection_threshold.to_string() +
" |\n"
report = report +
"| 严重度阈值 | " +
config.severity_threshold.to_string() +
" |\n"
report = report + "| 测量单位 | " + config.measurement_unit + " |\n"
report = report +
"| 默认公差 | " +
config.tolerance_default.to_string() +
" |\n"
report = report +
"| 表面质量阈值 | " +
config.surface_quality_threshold.to_string() +
" |\n"
report = report +
"| 异常阈值 | " +
config.anomaly_threshold.to_string() +
" |\n"
report = report +
"| AI检测 | " +
(if config.use_ai_detection { "开启" } else { "关闭" }) +
" |\n"
report = report +
"| 传统视觉 | " +
(if config.use_traditional_vision { "开启" } else { "关闭" }) +
" |\n"
report = report +
"| 混合方法 | " +
(if config.use_hybrid { "开启" } else { "关闭" }) +
" |\n"
report = report +
"| 相机分辨率 | " +
config.camera_resolution[0].to_string() +
"x" +
config.camera_resolution[1].to_string() +
" |\n"
report = report + "| 照明类型 | " + config.lighting_type + " |\n"
report = report +
"| 检测速度 | " +
config.inspection_speed.to_string() +
" 件/分钟 |\n"
report = report + "\n### 结果\n\n"
report = report + result_description + "\n"
report
}