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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// 智能应用视觉(v39.0.0)
// 自动驾驶、机器人视觉、工业检测、安防监控、农业视觉、天文图像处理
///|
/// 自动驾驶任务类型
pub(all) enum AutonomousDrivingTask {
LaneDetection
TrafficSignRecognition
PedestrianDetection
VehicleDetection
ObstacleDetection
RoadSegmentation
DepthEstimation
TrafficLightDetection
Unknown
} derive(Eq)
///|
/// 机器人视觉任务类型
pub(all) enum RobotVisionTask {
ObjectRecognition
GraspDetection
Navigation
SLAM
HumanFollowing
GestureRecognition
FaceRecognition
SceneUnderstanding
Unknown
} derive(Eq)
///|
/// 工业检测任务类型
pub(all) enum IndustrialInspectionTask {
DefectDetection
SurfaceInspection
DimensionMeasurement
AssemblyVerification
QualityControl
WeldInspection
PCBInspection
TextileInspection
Unknown
} derive(Eq)
///|
/// 安防监控任务类型
pub(all) enum SecurityMonitoringTask {
IntrusionDetection
FaceRecognition
BehaviorAnalysis
CrowdCounting
LicensePlateRecognition
AbandonedObjectDetection
FireSmokeDetection
FallDetection
Unknown
} derive(Eq)
///|
/// 农业视觉任务类型
pub(all) enum AgriculturalVisionTask {
CropDetection
WeedDetection
DiseaseDetection
PestDetection
YieldEstimation
SoilAnalysis
IrrigationMonitoring
HarvestReadiness
Unknown
} derive(Eq)
///|
/// 天文图像处理任务类型
pub(all) enum AstronomicalImageTask {
StarDetection
GalaxyClassification
PlanetDetection
NebulaAnalysis
CometTracking
SupernovaDetection
CosmicRayRemoval
ImageStacking
Unknown
} derive(Eq)
///|
/// 检测结果
pub(all) struct DetectionResultIV {
class_name : String
confidence : Double
bounding_box : Array[Double]
class_id : Int
num_detections : Int
}
///|
/// 车道线检测结果
pub(all) struct LaneDetectionResult {
lanes : Array[Array[Array[Double]]]
num_lanes : Int
lane_type : String
confidence : Double
curvature : Double
}
///|
/// 交通标志识别结果
pub(all) struct TrafficSignResult {
sign_type : String
sign_value : String
confidence : Double
bounding_box : Array[Double]
num_signs : Int
}
///|
/// 缺陷检测结果
pub(all) struct DefectDetectionResult {
defect_type : String
severity : String
confidence : Double
bounding_box : Array[Double]
defect_area : Double
num_defects : Int
}
///|
/// 入侵检测结果
pub(all) struct IntrusionDetectionResult {
is_intrusion : Bool
confidence : Double
intrusion_region : Array[Double]
object_type : String
timestamp : Double
num_intrusions : Int
}
///|
/// 作物检测结果
pub(all) struct CropDetectionResult {
crop_type : String
health_status : String
confidence : Double
bounding_box : Array[Double]
growth_stage : String
num_crops : Int
}
///|
/// 星体检测结果
pub(all) struct StarDetectionResult {
stars : Array[Array[Double]]
num_stars : Int
magnitudes : Array[Double]
brightest_star : Array[Double]
sky_background : Double
}
///|
/// 智能视觉配置
pub(all) struct IntelligentVisionConfig {
confidence_threshold : Double
nms_threshold : Double
max_detections : Int
input_size : Int
use_gpu : Bool
model_type : String
}
///|
/// 创建默认智能视觉配置
pub fn create_default_intelligent_vision_config() -> IntelligentVisionConfig {
IntelligentVisionConfig::{
confidence_threshold: 0.5,
nms_threshold: 0.45,
max_detections: 100,
input_size: 640,
use_gpu: false,
model_type: "yolov8",
}
}
///|
/// 通用目标检测(简化版)
pub fn detect_objects_iv(
image : @types.Image,
config : IntelligentVisionConfig,
) -> Array[DetectionResultIV] {
let w = image.width
let h = image.height
let results : Array[DetectionResultIV] = []
// 简化的检测:基于高对比度区域
let grid_size = 64
let mut detection_count = 0
for gy in 0..<(h / grid_size) {
for gx in 0..<(w / grid_size) {
if detection_count >= config.max_detections {
break
}
// 计算区域对比度
let mut sum = 0.0
let mut sum_sq = 0.0
let count = grid_size * grid_size
for y in 0..<grid_size {
for x in 0..<grid_size {
let idx = (gy * grid_size + y) * w + (gx * grid_size + x)
let v = image.data[idx].to_double()
sum += v
sum_sq += v * v
}
}
let mean = sum / count.to_double()
let variance = sum_sq / count.to_double() - mean * mean
let std_dev = if variance > 0.0 { variance.sqrt() } else { 0.0 }
if std_dev > 30.0 {
let confidence = if std_dev > 50.0 { 0.8 } else { 0.6 }
if confidence >= config.confidence_threshold {
let bx = gx.to_double() * grid_size.to_double()
let by = gy.to_double() * grid_size.to_double()
let bw = grid_size.to_double()
let bh = grid_size.to_double()
let bbox : Array[Double] = [bx, by, bw, bh]
results.push(DetectionResultIV::{
class_name: "object",
confidence,
bounding_box: bbox,
class_id: 0,
num_detections: 1,
})
detection_count += 1
}
}
}
}
results
}
///|
/// 车道线检测(简化版)
pub fn detect_lanes(
image : @types.Image,
config : IntelligentVisionConfig,
) -> LaneDetectionResult {
ignore(config)
let w = image.width
let h = image.height
let lanes : Array[Array[Array[Double]]] = []
// 简化的车道线检测:基于底部区域的边缘检测
let roi_height = h / 3
let roi_y = h - roi_height
// 左车道线
let left_lane : Array[Array[Double]] = []
for i in 0..<10 {
let y = roi_y + i * (roi_height / 10)
let x = w / 4 + i * 5
left_lane.push([x.to_double(), y.to_double()])
}
lanes.push(left_lane)
// 右车道线
let right_lane : Array[Array[Double]] = []
for i in 0..<10 {
let y = roi_y + i * (roi_height / 10)
let x = 3 * w / 4 - i * 5
right_lane.push([x.to_double(), y.to_double()])
}
lanes.push(right_lane)
LaneDetectionResult::{
lanes,
num_lanes: 2,
lane_type: "solid",
confidence: 0.7,
curvature: 0.05,
}
}
///|
/// 交通标志识别(简化版)
pub fn recognize_traffic_signs(
image : @types.Image,
config : IntelligentVisionConfig,
) -> Array[TrafficSignResult] {
let results : Array[TrafficSignResult] = []
let detections = detect_objects_iv(image, config)
for det in detections {
let sign_type = if det.confidence > 0.7 { "speed_limit" } else { "warning" }
let sign_value = if sign_type == "speed_limit" { "60" } else { "caution" }
results.push(TrafficSignResult::{
sign_type,
sign_value,
confidence: det.confidence,
bounding_box: det.bounding_box,
num_signs: 1,
})
}
results
}
///|
/// 缺陷检测(简化版)
pub fn detect_defects(
image : @types.Image,
config : IntelligentVisionConfig,
) -> Array[DefectDetectionResult] {
let results : Array[DefectDetectionResult] = []
let detections = detect_objects_iv(image, config)
for det in detections {
let defect_type = if det.confidence > 0.7 { "crack" } else { "scratch" }
let severity = if det.confidence > 0.8 {
"high"
} else if det.confidence > 0.6 {
"medium"
} else {
"low"
}
results.push(DefectDetectionResult::{
defect_type,
severity,
confidence: det.confidence,
bounding_box: det.bounding_box,
defect_area: det.bounding_box[2] * det.bounding_box[3],
num_defects: 1,
})
}
results
}
///|
/// 入侵检测(简化版)
pub fn detect_intrusion(
image : @types.Image,
roi : Array[Double],
config : IntelligentVisionConfig,
) -> IntrusionDetectionResult {
let detections = detect_objects_iv(image, config)
let mut is_intrusion = false
let mut intrusion_region : Array[Double] = []
let mut object_type = "none"
let mut max_confidence = 0.0
for det in detections {
// 检查检测框是否与ROI重叠
let x1 = det.bounding_box[0]
let y1 = det.bounding_box[1]
let x2 = x1 + det.bounding_box[2]
let y2 = y1 + det.bounding_box[3]
let roi_x1 = roi[0]
let roi_y1 = roi[1]
let roi_x2 = roi[0] + roi[2]
let roi_y2 = roi[1] + roi[3]
let overlap_x = if x2 > roi_x1 && x1 < roi_x2 {
let min_x2 = if x2 < roi_x2 { x2 } else { roi_x2 }
let max_x1 = if x1 > roi_x1 { x1 } else { roi_x1 }
min_x2 - max_x1
} else {
0.0
}
let overlap_y = if y2 > roi_y1 && y1 < roi_y2 {
let min_y2 = if y2 < roi_y2 { y2 } else { roi_y2 }
let max_y1 = if y1 > roi_y1 { y1 } else { roi_y1 }
min_y2 - max_y1
} else {
0.0
}
if overlap_x > 0.0 && overlap_y > 0.0 {
is_intrusion = true
intrusion_region = det.bounding_box
object_type = det.class_name
if det.confidence > max_confidence {
max_confidence = det.confidence
}
}
}
IntrusionDetectionResult::{
is_intrusion,
confidence: max_confidence,
intrusion_region,
object_type,
timestamp: 0.0,
num_intrusions: if is_intrusion {
1
} else {
0
},
}
}
///|
/// 作物检测(简化版)
pub fn detect_crops(
image : @types.Image,
config : IntelligentVisionConfig,
) -> Array[CropDetectionResult] {
let results : Array[CropDetectionResult] = []
let detections = detect_objects_iv(image, config)
for det in detections {
let crop_type = if det.confidence > 0.7 { "wheat" } else { "corn" }
let health_status = if det.confidence > 0.8 {
"healthy"
} else if det.confidence > 0.6 {
"moderate"
} else {
"stressed"
}
let growth_stage = if det.confidence > 0.7 { "mature" } else { "growing" }
results.push(CropDetectionResult::{
crop_type,
health_status,
confidence: det.confidence,
bounding_box: det.bounding_box,
growth_stage,
num_crops: 1,
})
}
results
}
///|
/// 星体检测(简化版)
pub fn detect_stars(
image : @types.Image,
config : IntelligentVisionConfig,
) -> StarDetectionResult {
ignore(config)
let w = image.width
let h = image.height
let _ = w * h
let stars : Array[Array[Double]] = []
let magnitudes : Array[Double] = []
// 简化的星体检测:寻找亮像素
let threshold = 200.0
let mut brightest : Array[Double] = [0.0, 0.0]
let mut max_brightness = 0.0
let mut background_sum = 0.0
let mut background_count = 0
for y in 0..<h {
for x in 0..<w {
if y % 2 == 0 && x % 2 == 0 {
let idx = y * w + x
let brightness = image.data[idx].to_double()
if brightness > threshold {
let star_pos : Array[Double] = [x.to_double(), y.to_double()]
stars.push(star_pos)
let magnitude = 25.0 - brightness / 10.0
magnitudes.push(magnitude)
if brightness > max_brightness {
max_brightness = brightness
brightest = [x.to_double(), y.to_double()]
}
} else {
background_sum += brightness
background_count += 1
}
}
}
}
let sky_background = if background_count > 0 {
background_sum / background_count.to_double()
} else {
0.0
}
StarDetectionResult::{
stars,
num_stars: stars.length(),
magnitudes,
brightest_star: brightest,
sky_background,
}
}
///|
/// 生成智能视觉报告
pub fn intelligent_vision_report_to_string(
task_name : String,
num_results : Int,
avg_confidence : Double,
config : IntelligentVisionConfig,
) -> String {
let mut report = "## 智能视觉报告\n\n"
report = report + "### 任务信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report + "| 任务类型 | " + task_name + " |\n"
report = report + "| 检测数量 | " + num_results.to_string() + " |\n"
report = report +
"| 平均置信度 | " +
(avg_confidence * 100.0).to_string() +
"% |\n"
report = report +
"| 置信度阈值 | " +
(config.confidence_threshold * 100.0).to_string() +
"% |\n"
report = report + "| 模型类型 | " + config.model_type + " |\n"
report = report + "| 输入尺寸 | " + config.input_size.to_string() + " |\n"
report
}