image.vision\scene_understanding\scene_understanding.mbt

84.7% · 122 / 144 points · 676 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 场景理解 scene_understanding/scene_understanding
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 场景理解与图像描述(v35.0.0)
  7. 7// 场景分类、图像描述、视觉问答、图像翻译、场景图生成、目标关系推理
  8. 8
  9. 9///|
  10. 10/// 场景分类算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum SceneClassificationAlgorithm {
  15. 15 ResNet
  16. 16 VGG
  17. 17 Inception
  18. 18 EfficientNet
  19. 19 VisionTransformer
  20. 20 SwinTransformer
  21. 21 ConvNeXt
  22. 22 DenseNet
  23. 23 MobileNet
  24. 24 Unknown
  25. 25}
  26. 26
  27. 27///|
  28. 28pub extend SceneClassificationAlgorithm with @debug.Debug::{to_repr}
  29. 29
  30. 30///|
  31. 31pub extend SceneClassificationAlgorithm with Eq::{not_equal, equal}
  32. 32
  33. 33///|
  34. 34/// 图像描述算法类型
  35. 35/// `#non_exhaustive` 表示未来可能添加新构造器
  36. 36#derive(Eq, @debug.Debug)
  37. 37#non_exhaustive
  38. 38pub(all) enum ImageCaptioningAlgorithm {
  39. 39 ShowAndTell
  40. 40 ShowAttendAndTell
  41. 41 UpDown
  42. 42 TransformerCaption
  43. 43 Oscar
  44. 44 VinVL
  45. 45 BLIP
  46. 46 CLIPCaption
  47. 47 Unknown
  48. 48}
  49. 49
  50. 50///|
  51. 51pub extend ImageCaptioningAlgorithm with @debug.Debug::{to_repr}
  52. 52
  53. 53///|
  54. 54pub extend ImageCaptioningAlgorithm with Eq::{not_equal, equal}
  55. 55
  56. 56///|
  57. 57/// 视觉问答算法类型
  58. 58/// `#non_exhaustive` 表示未来可能添加新构造器
  59. 59#derive(Eq, @debug.Debug)
  60. 60#non_exhaustive
  61. 61pub(all) enum VisualQuestionAnsweringAlgorithm {
  62. 62 VQA
  63. 63 VisualBERT
  64. 64 LXMERT
  65. 65 UNITER
  66. 66 ViLBERT
  67. 67 CLIP_VQA
  68. 68 BLIP_VQA
  69. 69 GPT4V
  70. 70 Unknown
  71. 71}
  72. 72
  73. 73///|
  74. 74pub extend VisualQuestionAnsweringAlgorithm with @debug.Debug::{to_repr}
  75. 75
  76. 76///|
  77. 77pub extend VisualQuestionAnsweringAlgorithm with Eq::{not_equal, equal}
  78. 78
  79. 79///|
  80. 80/// 场景分类结果
  81. 81#derive(Eq, @debug.Debug)
  82. 82pub(all) struct SceneClassificationResult {
  83. 83 scene_type : String
  84. 84 confidence : Double
  85. 85 top_predictions : Array[String]
  86. 86 top_confidences : Array[Double]
  87. 87 num_classes : Int32
  88. 88 scene_category : String
  89. 89}
  90. 90
  91. 91///|
  92. 92pub extend SceneClassificationResult with Eq::{not_equal, equal}
  93. 93
  94. 94///|
  95. 95pub extend SceneClassificationResult with @debug.Debug::{to_repr}
  96. 96
  97. 97///|
  98. 98/// 图像描述结果
  99. 99#derive(Eq, @debug.Debug)
  100. 100pub(all) struct ImageCaptioningResult {
  101. 101 caption : String
  102. 102 confidence : Double
  103. 103 alternative_captions : Array[String]
  104. 104 num_captions : Int32
  105. 105 bleu_score : Double
  106. 106 cider_score : Double
  107. 107 generation_time_ms : Double
  108. 108}
  109. 109
  110. 110///|
  111. 111pub extend ImageCaptioningResult with Eq::{not_equal, equal}
  112. 112
  113. 113///|
  114. 114pub extend ImageCaptioningResult with @debug.Debug::{to_repr}
  115. 115
  116. 116///|
  117. 117/// 视觉问答结果
  118. 118#derive(Eq, @debug.Debug)
  119. 119pub(all) struct VisualQuestionAnsweringResult {
  120. 120 answer : String
  121. 121 confidence : Double
  122. 122 alternative_answers : Array[String]
  123. 123 question_type : String
  124. 124 answer_type : String
  125. 125 reasoning_steps : Array[String]
  126. 126}
  127. 127
  128. 128///|
  129. 129pub extend VisualQuestionAnsweringResult with Eq::{not_equal, equal}
  130. 130
  131. 131///|
  132. 132pub extend VisualQuestionAnsweringResult with @debug.Debug::{to_repr}
  133. 133
  134. 134///|
  135. 135/// 场景图节点
  136. 136#derive(Eq, @debug.Debug)
  137. 137pub(all) struct SceneGraphNode {
  138. 138 object_id : Int32
  139. 139 object_name : String
  140. 140 bounding_box : Array[Double]
  141. 141 attributes : Array[String]
  142. 142 confidence : Double
  143. 143}
  144. 144
  145. 145///|
  146. 146pub extend SceneGraphNode with Eq::{not_equal, equal}
  147. 147
  148. 148///|
  149. 149pub extend SceneGraphNode with @debug.Debug::{to_repr}
  150. 150
  151. 151///|
  152. 152/// 场景图关系
  153. 153#derive(Eq, @debug.Debug)
  154. 154pub(all) struct SceneGraphRelation {
  155. 155 subject_id : Int32
  156. 156 object_id : Int32
  157. 157 relation : String
  158. 158 confidence : Double
  159. 159}
  160. 160
  161. 161///|
  162. 162pub extend SceneGraphRelation with Eq::{not_equal, equal}
  163. 163
  164. 164///|
  165. 165pub extend SceneGraphRelation with @debug.Debug::{to_repr}
  166. 166
  167. 167///|
  168. 168/// 场景图
  169. 169#derive(Eq, @debug.Debug)
  170. 170pub(all) struct SceneGraph {
  171. 171 nodes : Array[SceneGraphNode]
  172. 172 relations : Array[SceneGraphRelation]
  173. 173 num_objects : Int32
  174. 174 num_relations : Int32
  175. 175}
  176. 176
  177. 177///|
  178. 178pub extend SceneGraph with Eq::{not_equal, equal}
  179. 179
  180. 180///|
  181. 181pub extend SceneGraph with @debug.Debug::{to_repr}
  182. 182
  183. 183///|
  184. 184/// 场景理解配置
  185. 185#derive(Eq, @debug.Debug)
  186. 186pub(all) struct SceneUnderstandingConfig {
  187. 187 classification_algorithm : SceneClassificationAlgorithm
  188. 188 captioning_algorithm : ImageCaptioningAlgorithm
  189. 189 vqa_algorithm : VisualQuestionAnsweringAlgorithm
  190. 190 classification_threshold : Double
  191. 191 caption_temperature : Double
  192. 192 max_caption_length : Int32
  193. 193 use_attention : Bool
  194. 194 use_scene_graph : Bool
  195. 195}
  196. 196
  197. 197///|
  198. 198pub extend SceneUnderstandingConfig with Eq::{not_equal, equal}
  199. 199
  200. 200///|
  201. 201pub extend SceneUnderstandingConfig with @debug.Debug::{to_repr}
  202. 202
  203. 203///|
  204. 204/// 创建默认场景理解配置
  205. 205pub fn create_default_scene_config() -> SceneUnderstandingConfig {
  206. 206● SceneUnderstandingConfig::{
  207. 207 classification_algorithm: SceneClassificationAlgorithm::VisionTransformer,
  208. 208 captioning_algorithm: ImageCaptioningAlgorithm::BLIP,
  209. 209 vqa_algorithm: VisualQuestionAnsweringAlgorithm::BLIP_VQA,
  210. 210 classification_threshold: 0.5,
  211. 211 caption_temperature: 1.0,
  212. 212 max_caption_length: 50,
  213. 213 use_attention: true,
  214. 214 use_scene_graph: true,
  215. 215 }
  216. 216}
  217. 217
  218. 218///|
  219. 219/// 常见场景类型
  220. 220pub fn get_common_scene_types() -> Array[String] {
  221. 221● [
  222. 222 "indoor", "outdoor", "urban", "rural", "natural", "office", "home", "restaurant",
  223. 223 "store", "street", "park", "forest", "mountain", "beach", "sky", "night", "daytime",
  224. 224 "sunset", "sunrise", "cloudy", "rainy", "snowy", "foggy", "sunny", "windy", "crowded",
  225. 225 "empty", "modern", "traditional", "industrial",
  226. 226 ]
  227. 227}
  228. 228
  229. 229///|
  230. 230/// 场景分类(简化版:基于图像统计特征的启发式分类)
  231. 231pub fn classify_scene(
  232. 232 img : @types.Image,
  233. 233 config : SceneUnderstandingConfig,
  234. 234) -> SceneClassificationResult {
  235. 235● ignore(config)
  236. 236 let w = img.width
  237. 237 let h = img.height
  238. 238 let total = w * h
  239. 239
  240. 240 // 计算图像统计特征
  241. 241 let mut sum = 0.0
  242. 242 let mut sum_sq = 0.0
  243. 243 let mut edge_count = 0
  244. 244 let color_variation = 0.0
  245. 245● ignore(color_variation)
  246. 246
  247. 247 for y in 0..<h {
  248. 248● for x in 0..<w {
  249. 249● let idx = y * w + x
  250. 250● let pixel = img.data[idx].to_double()
  251. 251 sum += pixel
  252. 252 sum_sq += pixel * pixel
  253. 253
  254. 254 if x > 0 && x < w - 1 && y > 0 && y < h - 1 {
  255. 255● let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
  256. 256● let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
  257. 257● if (gx * gx + gy * gy).sqrt() > 20.0 {
  258. 258○ edge_count += 1
  259. 259 }
  260. 260 }
  261. 261 }
  262. 262 }
  263. 263
  264. 264● let mean = sum / total.to_double()
  265. 265● let variance = sum_sq / total.to_double() - mean * mean
  266. 266◐ let std_dev = if variance > 0.0 { variance.sqrt() } else { 0.0 }
  267. 267● let edge_density = edge_count.to_double() / total.to_double()
  268. 268
  269. 269 // 简化的场景分类
  270. 270 let mut scene_type = "indoor"
  271. 271 let mut scene_category = "environment"
  272. 272 let mut confidence = 0.45
  273. 273
  274. 274 if mean < 60.0 {
  275. 275● scene_type = "night"
  276. 276 scene_category = "time_of_day"
  277. 277 confidence = 0.7
  278. 278● } else if mean > 200.0 {
  279. 279● scene_type = "daytime"
  280. 280 scene_category = "time_of_day"
  281. 281 confidence = 0.65
  282. 282● } else if edge_density > 0.3 {
  283. 283○ scene_type = "urban"
  284. 284 scene_category = "environment"
  285. 285 confidence = 0.6
  286. 286● } else if edge_density < 0.1 {
  287. 287● scene_type = "natural"
  288. 288 scene_category = "environment"
  289. 289 confidence = 0.55
  290. 290○ } else if std_dev > 60.0 {
  291. 291○ scene_type = "outdoor"
  292. 292 scene_category = "environment"
  293. 293 confidence = 0.5
  294. 294 } else {
  295. 295○ scene_type = "indoor"
  296. 296 scene_category = "environment"
  297. 297 confidence = 0.45
  298. 298 }
  299. 299
  300. 300 // Top 预测
  301. 301 let top_predictions : Array[String] = []
  302. 302 let top_confidences : Array[Double] = []
  303. 303● top_predictions.push(scene_type)
  304. 304● top_confidences.push(confidence)
  305. 305● top_predictions.push("indoor")
  306. 306● top_confidences.push(0.3)
  307. 307● top_predictions.push("outdoor")
  308. 308● top_confidences.push(0.25)
  309. 309
  310. 310 SceneClassificationResult::{
  311. 311 scene_type,
  312. 312 confidence,
  313. 313 top_predictions,
  314. 314 top_confidences,
  315. 315 num_classes: 30,
  316. 316 scene_category,
  317. 317 }
  318. 318}
  319. 319
  320. 320///|
  321. 321/// 生成图像描述(简化版:基于场景分类和图像特征的模板化描述)
  322. 322pub fn generate_caption(
  323. 323 img : @types.Image,
  324. 324 config : SceneUnderstandingConfig,
  325. 325) -> ImageCaptioningResult {
  326. 326● let classification = classify_scene(img, config)
  327. 327 let w = img.width
  328. 328 let h = img.height
  329. 329 let total = w * h
  330. 330
  331. 331 // 计算更多图像特征
  332. 332 let mut sum = 0.0
  333. 333 for i in 0..<total {
  334. 334● sum += img.data[i].to_double()
  335. 335 }
  336. 336● let mean = sum / total.to_double()
  337. 337
  338. 338 // 简化的描述生成
  339. 339 let mut caption = ""
  340. 340 let brightness_desc = if mean < 60.0 {
  341. 341○ "dark"
  342. 342● } else if mean < 120.0 {
  343. 343○ "dimly lit"
  344. 344● } else if mean < 180.0 {
  345. 345● "well-lit"
  346. 346 } else {
  347. 347○ "bright"
  348. 348 }
  349. 349
  350. 350 caption = "A " + brightness_desc + " " + classification.scene_type + " scene."
  351. 351
  352. 352 let alternative_captions : Array[String] = []
  353. 353● alternative_captions.push(
  354. 354 "This appears to be a " + classification.scene_type + " environment.",
  355. 355 )
  356. 356● alternative_captions.push(
  357. 357 "An image showing a " + classification.scene_type + " setting.",
  358. 358 )
  359. 359● alternative_captions.push(
  360. 360 "A photograph of a " +
  361. 361 brightness_desc +
  362. 362 " " +
  363. 363 classification.scene_type +
  364. 364 ".",
  365. 365 )
  366. 366
  367. 367 ImageCaptioningResult::{
  368. 368 caption,
  369. 369 confidence: classification.confidence * 0.8,
  370. 370 alternative_captions,
  371. 371 num_captions: 4,
  372. 372 bleu_score: 0.3,
  373. 373 cider_score: 0.5,
  374. 374 generation_time_ms: 0.0,
  375. 375 }
  376. 376}
  377. 377
  378. 378///|
  379. 379/// 视觉问答(简化版:基于场景分类的规则化问答)
  380. 380pub fn answer_visual_question(
  381. 381 img : @types.Image,
  382. 382 question : String,
  383. 383 config : SceneUnderstandingConfig,
  384. 384) -> VisualQuestionAnsweringResult {
  385. 385● let classification = classify_scene(img, config)
  386. 386 let w = img.width
  387. 387 let h = img.height
  388. 388 let total = w * h
  389. 389
  390. 390 // 计算亮度
  391. 391 let mut sum = 0.0
  392. 392 for i in 0..<total {
  393. 393● sum += img.data[i].to_double()
  394. 394 }
  395. 395● let mean = sum / total.to_double()
  396. 396
  397. 397 // 简化的问答
  398. 398 let mut answer = ""
  399. 399 let mut question_type = ""
  400. 400 let mut answer_type = ""
  401. 401 let mut confidence = 0.0
  402. 402
  403. 403● if question.contains("scene") ||
  404. 404● question.contains("environment") ||
  405. 405● question.contains("place") {
  406. 406● answer = classification.scene_type
  407. 407 question_type = "scene_recognition"
  408. 408 answer_type = "open"
  409. 409 confidence = classification.confidence
  410. 410● } else if question.contains("bright") ||
  411. 411● question.contains("dark") ||
  412. 412● question.contains("light") {
  413. 413● if mean < 60.0 {
  414. 414● answer = "dark"
  415. 415○ } else if mean < 120.0 {
  416. 416○ answer = "dimly lit"
  417. 417○ } else if mean < 180.0 {
  418. 418○ answer = "well-lit"
  419. 419 } else {
  420. 420○ answer = "bright"
  421. 421 }
  422. 422 question_type = "attribute_recognition"
  423. 423 answer_type = "open"
  424. 424 confidence = 0.7
  425. 425● } else if question.contains("indoor") || question.contains("outdoor") {
  426. 426○ answer = classification.scene_type
  427. 427 question_type = "binary"
  428. 428 answer_type = "yes/no"
  429. 429 confidence = classification.confidence
  430. 430● } else if question.contains("color") || question.contains("colour") {
  431. 431○ if mean < 85.0 {
  432. 432○ answer = "dark colors"
  433. 433○ } else if mean < 170.0 {
  434. 434○ answer = "medium colors"
  435. 435 } else {
  436. 436○ answer = "light colors"
  437. 437 }
  438. 438 question_type = "attribute_recognition"
  439. 439 answer_type = "open"
  440. 440 confidence = 0.6
  441. 441 } else {
  442. 442● answer = "I'm not sure about that."
  443. 443 question_type = "unknown"
  444. 444 answer_type = "open"
  445. 445 confidence = 0.3
  446. 446 }
  447. 447
  448. 448 let alternative_answers : Array[String] = []
  449. 449● alternative_answers.push(classification.scene_type)
  450. 450● alternative_answers.push("indoor")
  451. 451● alternative_answers.push("outdoor")
  452. 452
  453. 453 let reasoning_steps : Array[String] = []
  454. 454● reasoning_steps.push("Analyze image statistics")
  455. 455● reasoning_steps.push("Classify scene type")
  456. 456● reasoning_steps.push("Match question to answer type")
  457. 457● reasoning_steps.push("Generate answer")
  458. 458
  459. 459 VisualQuestionAnsweringResult::{
  460. 460 answer,
  461. 461 confidence,
  462. 462 alternative_answers,
  463. 463 question_type,
  464. 464 answer_type,
  465. 465 reasoning_steps,
  466. 466 }
  467. 467}
  468. 468
  469. 469///|
  470. 470/// 生成场景图(简化版:基于图像区域分割的场景图)
  471. 471pub fn generate_scene_graph(
  472. 472 img : @types.Image,
  473. 473 config : SceneUnderstandingConfig,
  474. 474) -> SceneGraph {
  475. 475● ignore(config)
  476. 476 let w = img.width
  477. 477 let h = img.height
  478. 478
  479. 479 // 简化版:将图像分为 4 个区域,每个区域作为一个对象
  480. 480 let nodes : Array[SceneGraphNode] = []
  481. 481 let relations : Array[SceneGraphRelation] = []
  482. 482
  483. 483 let regions = [
  484. 484● [0.0, 0.0, w.to_double() / 2.0, h.to_double() / 2.0],
  485. 485● [w.to_double() / 2.0, 0.0, w.to_double() / 2.0, h.to_double() / 2.0],
  486. 486● [0.0, h.to_double() / 2.0, w.to_double() / 2.0, h.to_double() / 2.0],
  487. 487 [
  488. 488● w.to_double() / 2.0,
  489. 489● h.to_double() / 2.0,
  490. 490● w.to_double() / 2.0,
  491. 491● h.to_double() / 2.0,
  492. 492 ],
  493. 493 ]
  494. 494
  495. 495 let object_names = [
  496. 496 "top_left_object", "top_right_object", "bottom_left_object", "bottom_right_object",
  497. 497 ]
  498. 498
  499. 499 for i in 0..<4 {
  500. 500● let attributes : Array[String] = []
  501. 501● attributes.push("region_" + i.to_string())
  502. 502
  503. 503● nodes.push(SceneGraphNode::{
  504. 504 object_id: i,
  505. 505 object_name: object_names[i],
  506. 506 bounding_box: regions[i],
  507. 507 attributes,
  508. 508 confidence: 0.5,
  509. 509 })
  510. 510 }
  511. 511
  512. 512 // 添加空间关系
  513. 513● relations.push(SceneGraphRelation::{
  514. 514 subject_id: 0,
  515. 515 object_id: 1,
  516. 516 relation: "next_to",
  517. 517 confidence: 0.8,
  518. 518 })
  519. 519● relations.push(SceneGraphRelation::{
  520. 520 subject_id: 0,
  521. 521 object_id: 2,
  522. 522 relation: "above",
  523. 523 confidence: 0.8,
  524. 524 })
  525. 525● relations.push(SceneGraphRelation::{
  526. 526 subject_id: 1,
  527. 527 object_id: 3,
  528. 528 relation: "above",
  529. 529 confidence: 0.8,
  530. 530 })
  531. 531● relations.push(SceneGraphRelation::{
  532. 532 subject_id: 2,
  533. 533 object_id: 3,
  534. 534 relation: "next_to",
  535. 535 confidence: 0.8,
  536. 536 })
  537. 537
  538. 538 SceneGraph::{ nodes, relations, num_objects: 4, num_relations: 4, }
  539. 539}
  540. 540
  541. 541///|
  542. 542/// 完整的场景理解
  543. 543pub fn understand_scene(
  544. 544 img : @types.Image,
  545. 545 config : SceneUnderstandingConfig,
  546. 546) -> (SceneClassificationResult, ImageCaptioningResult, SceneGraph) {
  547. 547● let classification = classify_scene(img, config)
  548. 548● let caption = generate_caption(img, config)
  549. 549 let scene_graph = if config.use_scene_graph {
  550. 550● generate_scene_graph(img, config)
  551. 551 } else {
  552. 552○ SceneGraph::{ nodes: [], relations: [], num_objects: 0, num_relations: 0, }
  553. 553 }
  554. 554
  555. 555 (classification, caption, scene_graph)
  556. 556}
  557. 557
  558. 558///|
  559. 559/// 生成场景理解报告
  560. 560pub fn scene_understanding_report_to_string(
  561. 561 classification : SceneClassificationResult,
  562. 562 caption : ImageCaptioningResult,
  563. 563 vqa : VisualQuestionAnsweringResult,
  564. 564 scene_graph : SceneGraph,
  565. 565) -> String {
  566. 566● let mut report = "## 场景理解与图像描述报告\n\n"
  567. 567
  568. 568 report = report + "### 场景分类\n\n"
  569. 569 report = report + "| 指标 | 值 |\n|------|------|\n"
  570. 570 report = report + "| 场景类型 | " + classification.scene_type + " |\n"
  571. 571 report = report + "| 场景类别 | " + classification.scene_category + " |\n"
  572. 572 report = report +
  573. 573 "| 置信度 | " +
  574. 574● (classification.confidence * 100.0).to_string() +
  575. 575 "% |\n"
  576. 576 report = report +
  577. 577 "| 类别数 | " +
  578. 578● classification.num_classes.to_string() +
  579. 579 " |\n\n"
  580. 580
  581. 581 report = report + "### Top 预测\n\n"
  582. 582 report = report + "| 排名 | 场景 | 置信度 |\n|------|------|------|\n"
  583. 583● for i in 0..<classification.top_predictions.length() {
  584. 584● report = report +
  585. 585 "| " +
  586. 586● (i + 1).to_string() +
  587. 587 " | " +
  588. 588 classification.top_predictions[i] +
  589. 589 " | " +
  590. 590● (classification.top_confidences[i] * 100.0).to_string() +
  591. 591 "% |\n"
  592. 592 }
  593. 593
  594. 594 report = report + "\n### 图像描述\n\n"
  595. 595 report = report + "| 指标 | 值 |\n|------|------|\n"
  596. 596 report = report + "| 主描述 | " + caption.caption + " |\n"
  597. 597 report = report +
  598. 598 "| 置信度 | " +
  599. 599● (caption.confidence * 100.0).to_string() +
  600. 600 "% |\n"
  601. 601● report = report + "| BLEU 分数 | " + caption.bleu_score.to_string() + " |\n"
  602. 602 report = report +
  603. 603 "| CIDEr 分数 | " +
  604. 604● caption.cider_score.to_string() +
  605. 605 " |\n"
  606. 606 report = report +
  607. 607 "| 备选描述数 | " +
  608. 608● caption.num_captions.to_string() +
  609. 609 " |\n\n"
  610. 610
  611. 611 report = report + "### 备选描述\n\n"
  612. 612● for i in 0..<caption.alternative_captions.length() {
  613. 613● report = report +
  614. 614● (i + 1).to_string() +
  615. 615 ". " +
  616. 616 caption.alternative_captions[i] +
  617. 617 "\n"
  618. 618 }
  619. 619
  620. 620 report = report + "\n### 视觉问答\n\n"
  621. 621 report = report + "| 指标 | 值 |\n|------|------|\n"
  622. 622 report = report + "| 答案 | " + vqa.answer + " |\n"
  623. 623 report = report +
  624. 624 "| 置信度 | " +
  625. 625● (vqa.confidence * 100.0).to_string() +
  626. 626 "% |\n"
  627. 627 report = report + "| 问题类型 | " + vqa.question_type + " |\n"
  628. 628 report = report + "| 答案类型 | " + vqa.answer_type + " |\n\n"
  629. 629
  630. 630 report = report + "### 推理步骤\n\n"
  631. 631● for i in 0..<vqa.reasoning_steps.length() {
  632. 632● report = report + (i + 1).to_string() + ". " + vqa.reasoning_steps[i] + "\n"
  633. 633 }
  634. 634
  635. 635 report = report + "\n### 场景图\n\n"
  636. 636 report = report + "| 指标 | 值 |\n|------|------|\n"
  637. 637 report = report +
  638. 638 "| 对象数 | " +
  639. 639● scene_graph.num_objects.to_string() +
  640. 640 " |\n"
  641. 641 report = report +
  642. 642 "| 关系数 | " +
  643. 643● scene_graph.num_relations.to_string() +
  644. 644 " |\n\n"
  645. 645
  646. 646 report = report + "### 对象列表\n\n"
  647. 647 report = report + "| ID | 名称 | 置信度 |\n|------|------|------|\n"
  648. 648 for node in scene_graph.nodes {
  649. 649● report = report +
  650. 650 "| " +
  651. 651● node.object_id.to_string() +
  652. 652 " | " +
  653. 653 node.object_name +
  654. 654 " | " +
  655. 655● (node.confidence * 100.0).to_string() +
  656. 656 "% |\n"
  657. 657 }
  658. 658
  659. 659 report = report + "\n### 关系列表\n\n"
  660. 660 report = report +
  661. 661 "| 主体 | 关系 | 客体 | 置信度 |\n|------|------|------|------|\n"
  662. 662 for rel in scene_graph.relations {
  663. 663● report = report +
  664. 664 "| " +
  665. 665● rel.subject_id.to_string() +
  666. 666 " | " +
  667. 667 rel.relation +
  668. 668 " | " +
  669. 669● rel.object_id.to_string() +
  670. 670 " | " +
  671. 671● (rel.confidence * 100.0).to_string() +
  672. 672 "% |\n"
  673. 673 }
  674. 674
  675. 675 report
  676. 676}