image.vision\intelligent_vision\intelligent_vision.mbt
94.4% · 101 / 107 points · 641 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 智能视觉 intelligent_vision/intelligent_vision - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 智能应用视觉(v39.0.0) - 7
// 自动驾驶、机器人视觉、工业检测、安防监控、农业视觉、天文图像处理 - 8
- 9
///| - 10
/// 自动驾驶任务类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum AutonomousDrivingTask { - 15
LaneDetection - 16
TrafficSignRecognition - 17
PedestrianDetection - 18
VehicleDetection - 19
ObstacleDetection - 20
RoadSegmentation - 21
DepthEstimation - 22
TrafficLightDetection - 23
Unknown - 24
} - 25
- 26
///| - 27
pub extend AutonomousDrivingTask with @debug.Debug::{to_repr} - 28
- 29
///| - 30
pub extend AutonomousDrivingTask with Eq::{not_equal, equal} - 31
- 32
///| - 33
/// 机器人视觉任务类型 - 34
/// `#non_exhaustive` 表示未来可能添加新构造器 - 35
#derive(Eq, @debug.Debug) - 36
#non_exhaustive - 37
pub(all) enum RobotVisionTask { - 38
ObjectRecognition - 39
GraspDetection - 40
Navigation - 41
SLAM - 42
HumanFollowing - 43
GestureRecognition - 44
FaceRecognition - 45
SceneUnderstanding - 46
Unknown - 47
} - 48
- 49
///| - 50
pub extend RobotVisionTask with @debug.Debug::{to_repr} - 51
- 52
///| - 53
pub extend RobotVisionTask with Eq::{not_equal, equal} - 54
- 55
///| - 56
/// 工业检测任务类型 - 57
/// `#non_exhaustive` 表示未来可能添加新构造器 - 58
#derive(Eq, @debug.Debug) - 59
#non_exhaustive - 60
pub(all) enum IndustrialInspectionTask { - 61
DefectDetection - 62
SurfaceInspection - 63
DimensionMeasurement - 64
AssemblyVerification - 65
QualityControl - 66
WeldInspection - 67
PCBInspection - 68
TextileInspection - 69
Unknown - 70
} - 71
- 72
///| - 73
pub extend IndustrialInspectionTask with @debug.Debug::{to_repr} - 74
- 75
///| - 76
pub extend IndustrialInspectionTask with Eq::{not_equal, equal} - 77
- 78
///| - 79
/// 安防监控任务类型 - 80
/// `#non_exhaustive` 表示未来可能添加新构造器 - 81
#derive(Eq, @debug.Debug) - 82
#non_exhaustive - 83
pub(all) enum SecurityMonitoringTask { - 84
IntrusionDetection - 85
FaceRecognition - 86
BehaviorAnalysis - 87
CrowdCounting - 88
LicensePlateRecognition - 89
AbandonedObjectDetection - 90
FireSmokeDetection - 91
FallDetection - 92
Unknown - 93
} - 94
- 95
///| - 96
pub extend SecurityMonitoringTask with @debug.Debug::{to_repr} - 97
- 98
///| - 99
pub extend SecurityMonitoringTask with Eq::{not_equal, equal} - 100
- 101
///| - 102
/// 农业视觉任务类型 - 103
/// `#non_exhaustive` 表示未来可能添加新构造器 - 104
#derive(Eq, @debug.Debug) - 105
#non_exhaustive - 106
pub(all) enum AgriculturalVisionTask { - 107
CropDetection - 108
WeedDetection - 109
DiseaseDetection - 110
PestDetection - 111
YieldEstimation - 112
SoilAnalysis - 113
IrrigationMonitoring - 114
HarvestReadiness - 115
Unknown - 116
} - 117
- 118
///| - 119
pub extend AgriculturalVisionTask with @debug.Debug::{to_repr} - 120
- 121
///| - 122
pub extend AgriculturalVisionTask with Eq::{not_equal, equal} - 123
- 124
///| - 125
/// 天文图像处理任务类型 - 126
/// `#non_exhaustive` 表示未来可能添加新构造器 - 127
#derive(Eq, @debug.Debug) - 128
#non_exhaustive - 129
pub(all) enum AstronomicalImageTask { - 130
StarDetection - 131
GalaxyClassification - 132
PlanetDetection - 133
NebulaAnalysis - 134
CometTracking - 135
SupernovaDetection - 136
CosmicRayRemoval - 137
ImageStacking - 138
Unknown - 139
} - 140
- 141
///| - 142
pub extend AstronomicalImageTask with @debug.Debug::{to_repr} - 143
- 144
///| - 145
pub extend AstronomicalImageTask with Eq::{not_equal, equal} - 146
- 147
///| - 148
/// 检测结果 - 149
#derive(Eq, @debug.Debug) - 150
pub(all) struct DetectionResultIV { - 151
class_name : String - 152
confidence : Double - 153
bounding_box : Array[Double] - 154
class_id : Int32 - 155
num_detections : Int32 - 156
} - 157
- 158
///| - 159
pub extend DetectionResultIV with Eq::{not_equal, equal} - 160
- 161
///| - 162
pub extend DetectionResultIV with @debug.Debug::{to_repr} - 163
- 164
///| - 165
/// 车道线检测结果 - 166
#derive(Eq, @debug.Debug) - 167
pub(all) struct LaneDetectionResult { - 168
lanes : Array[Array[Array[Double]]] - 169
num_lanes : Int32 - 170
lane_type : String - 171
confidence : Double - 172
curvature : Double - 173
} - 174
- 175
///| - 176
pub extend LaneDetectionResult with Eq::{not_equal, equal} - 177
- 178
///| - 179
pub extend LaneDetectionResult with @debug.Debug::{to_repr} - 180
- 181
///| - 182
/// 交通标志识别结果 - 183
#derive(Eq, @debug.Debug) - 184
pub(all) struct TrafficSignResult { - 185
sign_type : String - 186
sign_value : String - 187
confidence : Double - 188
bounding_box : Array[Double] - 189
num_signs : Int32 - 190
} - 191
- 192
///| - 193
pub extend TrafficSignResult with Eq::{not_equal, equal} - 194
- 195
///| - 196
pub extend TrafficSignResult with @debug.Debug::{to_repr} - 197
- 198
///| - 199
/// 缺陷检测结果 - 200
#derive(Eq, @debug.Debug) - 201
pub(all) struct DefectDetectionResult { - 202
defect_type : String - 203
severity : String - 204
confidence : Double - 205
bounding_box : Array[Double] - 206
defect_area : Double - 207
num_defects : Int32 - 208
} - 209
- 210
///| - 211
pub extend DefectDetectionResult with Eq::{not_equal, equal} - 212
- 213
///| - 214
pub extend DefectDetectionResult with @debug.Debug::{to_repr} - 215
- 216
///| - 217
/// 入侵检测结果 - 218
#derive(Eq, @debug.Debug) - 219
pub(all) struct IntrusionDetectionResult { - 220
is_intrusion : Bool - 221
confidence : Double - 222
intrusion_region : Array[Double] - 223
object_type : String - 224
timestamp : Double - 225
num_intrusions : Int32 - 226
} - 227
- 228
///| - 229
pub extend IntrusionDetectionResult with Eq::{not_equal, equal} - 230
- 231
///| - 232
pub extend IntrusionDetectionResult with @debug.Debug::{to_repr} - 233
- 234
///| - 235
/// 作物检测结果 - 236
#derive(Eq, @debug.Debug) - 237
pub(all) struct CropDetectionResult { - 238
crop_type : String - 239
health_status : String - 240
confidence : Double - 241
bounding_box : Array[Double] - 242
growth_stage : String - 243
num_crops : Int32 - 244
} - 245
- 246
///| - 247
pub extend CropDetectionResult with Eq::{not_equal, equal} - 248
- 249
///| - 250
pub extend CropDetectionResult with @debug.Debug::{to_repr} - 251
- 252
///| - 253
/// 星体检测结果 - 254
#derive(Eq, @debug.Debug) - 255
pub(all) struct StarDetectionResult { - 256
stars : Array[Array[Double]] - 257
num_stars : Int32 - 258
magnitudes : Array[Double] - 259
brightest_star : Array[Double] - 260
sky_background : Double - 261
} - 262
- 263
///| - 264
pub extend StarDetectionResult with Eq::{not_equal, equal} - 265
- 266
///| - 267
pub extend StarDetectionResult with @debug.Debug::{to_repr} - 268
- 269
///| - 270
/// 智能视觉配置 - 271
#derive(Eq, @debug.Debug) - 272
pub(all) struct IntelligentVisionConfig { - 273
confidence_threshold : Double - 274
nms_threshold : Double - 275
max_detections : Int32 - 276
input_size : Int32 - 277
use_gpu : Bool - 278
model_type : String - 279
} - 280
- 281
///| - 282
pub extend IntelligentVisionConfig with Eq::{not_equal, equal} - 283
- 284
///| - 285
pub extend IntelligentVisionConfig with @debug.Debug::{to_repr} - 286
- 287
///| - 288
/// 创建默认智能视觉配置 - 289
pub fn create_default_intelligent_vision_config() -> IntelligentVisionConfig { - 290●
IntelligentVisionConfig::{ - 291
confidence_threshold: 0.5, - 292
nms_threshold: 0.45, - 293
max_detections: 100, - 294
input_size: 640, - 295
use_gpu: false, - 296
model_type: "yolov8", - 297
} - 298
} - 299
- 300
///| - 301
/// 通用目标检测(简化版) - 302
pub fn detect_objects_iv( - 303
image : @types.Image, - 304
config : IntelligentVisionConfig, - 305
) -> Array[DetectionResultIV] { - 306●
let w = image.width - 307
let h = image.height - 308
let results : Array[DetectionResultIV] = [] - 309
- 310
// 简化的检测:基于高对比度区域 - 311
let grid_size = 64 - 312
let mut detection_count = 0 - 313
- 314
for gy in 0..<(h / grid_size) { - 315●
for gx in 0..<(w / grid_size) { - 316●
if detection_count >= config.max_detections { - 317●
break - 318
} - 319
- 320
// 计算区域对比度 - 321
let mut sum = 0.0 - 322
let mut sum_sq = 0.0 - 323
let count = grid_size * grid_size - 324
for y in 0..<grid_size { - 325●
for x in 0..<grid_size { - 326●
let idx = (gy * grid_size + y) * w + (gx * grid_size + x) - 327●
let v = image.data[idx].to_double() - 328
sum += v - 329
sum_sq += v * v - 330
} - 331
} - 332●
let mean = sum / count.to_double() - 333●
let variance = sum_sq / count.to_double() - mean * mean - 334●
let std_dev = if variance > 0.0 { variance.sqrt() } else { 0.0 } - 335
- 336
if std_dev > 30.0 { - 337●
let confidence = if std_dev > 50.0 { 0.8 } else { 0.6 } - 338
if confidence >= config.confidence_threshold { - 339●
let bx = gx.to_double() * grid_size.to_double() - 340●
let by = gy.to_double() * grid_size.to_double() - 341●
let bw = grid_size.to_double() - 342●
let bh = grid_size.to_double() - 343
let bbox : Array[Double] = [bx, by, bw, bh] - 344●
results.push(DetectionResultIV::{ - 345
class_name: "object", - 346
confidence, - 347
bounding_box: bbox, - 348
class_id: 0, - 349
num_detections: 1, - 350
}) - 351
detection_count += 1 - 352
} - 353
} - 354
} - 355
} - 356
- 357
results - 358
} - 359
- 360
///| - 361
/// 车道线检测(简化版) - 362
pub fn detect_lanes( - 363
image : @types.Image, - 364
config : IntelligentVisionConfig, - 365
) -> LaneDetectionResult { - 366●
ignore(config) - 367
let w = image.width - 368
let h = image.height - 369
let lanes : Array[Array[Array[Double]]] = [] - 370
- 371
// 简化的车道线检测:基于底部区域的边缘检测 - 372
let roi_height = h / 3 - 373
let roi_y = h - roi_height - 374
- 375
// 左车道线 - 376
let left_lane : Array[Array[Double]] = [] - 377
for i in 0..<10 { - 378●
let y = roi_y + i * (roi_height / 10) - 379
let x = w / 4 + i * 5 - 380●
left_lane.push([x.to_double(), y.to_double()]) - 381
} - 382●
lanes.push(left_lane) - 383
- 384
// 右车道线 - 385
let right_lane : Array[Array[Double]] = [] - 386
for i in 0..<10 { - 387●
let y = roi_y + i * (roi_height / 10) - 388
let x = 3 * w / 4 - i * 5 - 389●
right_lane.push([x.to_double(), y.to_double()]) - 390
} - 391●
lanes.push(right_lane) - 392
- 393
LaneDetectionResult::{ - 394
lanes, - 395
num_lanes: 2, - 396
lane_type: "solid", - 397
confidence: 0.7, - 398
curvature: 0.05, - 399
} - 400
} - 401
- 402
///| - 403
/// 交通标志识别(简化版) - 404
pub fn recognize_traffic_signs( - 405
image : @types.Image, - 406
config : IntelligentVisionConfig, - 407
) -> Array[TrafficSignResult] { - 408●
let results : Array[TrafficSignResult] = [] - 409●
let detections = detect_objects_iv(image, config) - 410
- 411
for det in detections { - 412●
let sign_type = if det.confidence > 0.7 { "speed_limit" } else { "warning" } - 413●
let sign_value = if sign_type == "speed_limit" { "60" } else { "caution" } - 414
- 415●
results.push(TrafficSignResult::{ - 416
sign_type, - 417
sign_value, - 418
confidence: det.confidence, - 419
bounding_box: det.bounding_box, - 420
num_signs: 1, - 421
}) - 422
} - 423
- 424
results - 425
} - 426
- 427
///| - 428
/// 缺陷检测(简化版) - 429
pub fn detect_defects( - 430
image : @types.Image, - 431
config : IntelligentVisionConfig, - 432
) -> Array[DefectDetectionResult] { - 433●
let results : Array[DefectDetectionResult] = [] - 434●
let detections = detect_objects_iv(image, config) - 435
- 436
for det in detections { - 437●
let defect_type = if det.confidence > 0.7 { "crack" } else { "scratch" } - 438
let severity = if det.confidence > 0.8 { - 439○
"high" - 440●
} else if det.confidence > 0.6 { - 441●
"medium" - 442
} else { - 443●
"low" - 444
} - 445
- 446●
results.push(DefectDetectionResult::{ - 447
defect_type, - 448
severity, - 449
confidence: det.confidence, - 450
bounding_box: det.bounding_box, - 451
defect_area: det.bounding_box[2] * det.bounding_box[3], - 452
num_defects: 1, - 453
}) - 454
} - 455
- 456
results - 457
} - 458
- 459
///| - 460
/// 入侵检测(简化版) - 461
pub fn detect_intrusion( - 462
image : @types.Image, - 463
roi : Array[Double], - 464
config : IntelligentVisionConfig, - 465
) -> IntrusionDetectionResult { - 466●
let detections = detect_objects_iv(image, config) - 467
let mut is_intrusion = false - 468
let mut intrusion_region : Array[Double] = [] - 469
let mut object_type = "none" - 470
let mut max_confidence = 0.0 - 471
- 472
for det in detections { - 473
// 检查检测框是否与ROI重叠 - 474●
let x1 = det.bounding_box[0] - 475
let y1 = det.bounding_box[1] - 476
let x2 = x1 + det.bounding_box[2] - 477
let y2 = y1 + det.bounding_box[3] - 478
- 479
let roi_x1 = roi[0] - 480
let roi_y1 = roi[1] - 481
let roi_x2 = roi[0] + roi[2] - 482
let roi_y2 = roi[1] + roi[3] - 483
- 484
let overlap_x = if x2 > roi_x1 && x1 < roi_x2 { - 485◐
let min_x2 = if x2 < roi_x2 { x2 } else { roi_x2 } - 486◐
let max_x1 = if x1 > roi_x1 { x1 } else { roi_x1 } - 487
min_x2 - max_x1 - 488
} else { - 489●
0.0 - 490
} - 491
- 492
let overlap_y = if y2 > roi_y1 && y1 < roi_y2 { - 493◐
let min_y2 = if y2 < roi_y2 { y2 } else { roi_y2 } - 494◐
let max_y1 = if y1 > roi_y1 { y1 } else { roi_y1 } - 495
min_y2 - max_y1 - 496
} else { - 497●
0.0 - 498
} - 499
- 500
if overlap_x > 0.0 && overlap_y > 0.0 { - 501●
is_intrusion = true - 502
intrusion_region = det.bounding_box - 503
object_type = det.class_name - 504
if det.confidence > max_confidence { - 505●
max_confidence = det.confidence - 506
} - 507
} - 508
} - 509
- 510
IntrusionDetectionResult::{ - 511
is_intrusion, - 512
confidence: max_confidence, - 513
intrusion_region, - 514
object_type, - 515
timestamp: 0.0, - 516
num_intrusions: if is_intrusion { - 517●
1 - 518
} else { - 519●
0 - 520
}, - 521
} - 522
} - 523
- 524
///| - 525
/// 作物检测(简化版) - 526
pub fn detect_crops( - 527
image : @types.Image, - 528
config : IntelligentVisionConfig, - 529
) -> Array[CropDetectionResult] { - 530●
let results : Array[CropDetectionResult] = [] - 531●
let detections = detect_objects_iv(image, config) - 532
- 533
for det in detections { - 534●
let crop_type = if det.confidence > 0.7 { "wheat" } else { "corn" } - 535
let health_status = if det.confidence > 0.8 { - 536○
"healthy" - 537●
} else if det.confidence > 0.6 { - 538●
"moderate" - 539
} else { - 540●
"stressed" - 541
} - 542●
let growth_stage = if det.confidence > 0.7 { "mature" } else { "growing" } - 543
- 544●
results.push(CropDetectionResult::{ - 545
crop_type, - 546
health_status, - 547
confidence: det.confidence, - 548
bounding_box: det.bounding_box, - 549
growth_stage, - 550
num_crops: 1, - 551
}) - 552
} - 553
- 554
results - 555
} - 556
- 557
///| - 558
/// 星体检测(简化版) - 559
pub fn detect_stars( - 560
image : @types.Image, - 561
config : IntelligentVisionConfig, - 562
) -> StarDetectionResult { - 563●
ignore(config) - 564
let w = image.width - 565
let h = image.height - 566
let _ = w * h - 567
let stars : Array[Array[Double]] = [] - 568
let magnitudes : Array[Double] = [] - 569
- 570
// 简化的星体检测:寻找亮像素 - 571
let threshold = 200.0 - 572
let mut brightest : Array[Double] = [0.0, 0.0] - 573
let mut max_brightness = 0.0 - 574
let mut background_sum = 0.0 - 575
let mut background_count = 0 - 576
- 577
for y in 0..<h { - 578●
for x in 0..<w { - 579●
if y % 2 == 0 && x % 2 == 0 { - 580●
let idx = y * w + x - 581●
let brightness = image.data[idx].to_double() - 582
- 583
if brightness > threshold { - 584●
let star_pos : Array[Double] = [x.to_double(), y.to_double()] - 585●
stars.push(star_pos) - 586
let magnitude = 25.0 - brightness / 10.0 - 587●
magnitudes.push(magnitude) - 588
- 589
if brightness > max_brightness { - 590●
max_brightness = brightness - 591●
brightest = [x.to_double(), y.to_double()] - 592
} - 593
} else { - 594●
background_sum += brightness - 595
background_count += 1 - 596
} - 597
} - 598
} - 599
} - 600
- 601
let sky_background = if background_count > 0 { - 602●
background_sum / background_count.to_double() - 603
} else { - 604●
0.0 - 605
} - 606
- 607
StarDetectionResult::{ - 608
stars, - 609●
num_stars: stars.length(), - 610
magnitudes, - 611
brightest_star: brightest, - 612
sky_background, - 613
} - 614
} - 615
- 616
///| - 617
/// 生成智能视觉报告 - 618
pub fn intelligent_vision_report_to_string( - 619
task_name : String, - 620
num_results : Int32, - 621
avg_confidence : Double, - 622
config : IntelligentVisionConfig, - 623
) -> String { - 624●
let mut report = "## 智能视觉报告\n\n" - 625
report = report + "### 任务信息\n\n" - 626
report = report + "| 指标 | 值 |\n|------|------|\n" - 627
report = report + "| 任务类型 | " + task_name + " |\n" - 628●
report = report + "| 检测数量 | " + num_results.to_string() + " |\n" - 629
report = report + - 630
"| 平均置信度 | " + - 631●
(avg_confidence * 100.0).to_string() + - 632
"% |\n" - 633
report = report + - 634
"| 置信度阈值 | " + - 635●
(config.confidence_threshold * 100.0).to_string() + - 636
"% |\n" - 637
report = report + "| 模型类型 | " + config.model_type + " |\n" - 638●
report = report + "| 输入尺寸 | " + config.input_size.to_string() + " |\n" - 639
- 640
report - 641
}