-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_visualize_vqa_data.py
More file actions
1009 lines (872 loc) · 30.5 KB
/
quick_visualize_vqa_data.py
File metadata and controls
1009 lines (872 loc) · 30.5 KB
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Author: WANG Maonan
Date: 2026-01-04 15:30:12
LastEditors: WANG Maonan
Description: VQA数据可视化工具 - 将JSONL格式转换为美观的HTML页面
LastEditTime: 2026-01-04 15:30:14
'''
import json
import argparse
import shutil
from pathlib import Path
from typing import List, Dict, Any, Set
def load_jsonl(file_path: str) -> List[Dict[str, Any]]:
"""加载 JSONL 文件"""
data = []
with open(file_path, 'r', encoding='utf-8') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
if not line:
continue
try:
data.append(json.loads(line))
except json.JSONDecodeError as e:
print(f" ⚠️ 警告: 第 {line_num} 行解析失败: {e}")
continue
return data
def get_task_name(filename: str) -> str:
"""从文件名提取任务名称"""
return filename.replace('.jsonl', '').replace('_', ' ').title()
def generate_html_header() -> str:
"""生成HTML头部"""
return """<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Traffic VQA Benchmark - 数据可视化</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 20px;
line-height: 1.6;
color: #333;
}
.container {
max-width: 1400px;
margin: 0 auto;
}
.header {
background: white;
padding: 30px;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
margin-bottom: 30px;
text-align: center;
}
.header h1 {
color: #667eea;
font-size: 2.5em;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
.header .subtitle {
color: #666;
font-size: 1.1em;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-top: 20px;
}
.stat-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
}
.stat-card .number {
font-size: 2em;
font-weight: bold;
margin-bottom: 5px;
}
.stat-card .label {
font-size: 0.9em;
opacity: 0.9;
}
.task-section {
background: white;
border-radius: 15px;
padding: 25px;
margin-bottom: 25px;
box-shadow: 0 5px 20px rgba(0,0,0,0.15);
transition: transform 0.3s ease;
}
.task-section:hover {
transform: translateY(-5px);
box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}
.task-header {
border-bottom: 3px solid #667eea;
padding-bottom: 15px;
margin-bottom: 20px;
}
.task-title {
font-size: 1.8em;
color: #667eea;
margin-bottom: 10px;
display: flex;
align-items: center;
gap: 10px;
}
.task-meta {
display: flex;
gap: 20px;
flex-wrap: wrap;
color: #666;
font-size: 0.95em;
}
.task-meta span {
display: flex;
align-items: center;
gap: 5px;
}
.badge {
background: #667eea;
color: white;
padding: 3px 10px;
border-radius: 15px;
font-size: 0.85em;
font-weight: 500;
}
.qa-item {
background: #f8f9fa;
border-left: 4px solid #667eea;
padding: 20px;
margin-bottom: 20px;
border-radius: 8px;
transition: all 0.3s ease;
}
.qa-item:hover {
background: #e9ecef;
border-left-width: 6px;
}
.qa-number {
background: #667eea;
color: white;
width: 35px;
height: 35px;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-weight: bold;
margin-right: 10px;
}
.question {
font-size: 1.15em;
font-weight: 600;
color: #2c3e50;
margin-bottom: 15px;
display: flex;
align-items: flex-start;
}
.question-text {
flex: 1;
line-height: 1.5;
}
.answer {
background: white;
padding: 15px;
border-radius: 8px;
margin: 15px 0;
border: 2px solid #e0e0e0;
}
.answer-label {
font-weight: 600;
color: #28a745;
margin-bottom: 8px;
display: flex;
align-items: center;
gap: 5px;
}
.answer-label::before {
content: "✓";
background: #28a745;
color: white;
border-radius: 50%;
width: 20px;
height: 20px;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 0.8em;
}
.answer-text {
color: #495057;
line-height: 1.6;
}
.options {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 12px;
margin: 15px 0;
}
.option {
background: white;
padding: 12px 15px;
border-radius: 8px;
border: 2px solid #e0e0e0;
transition: all 0.3s ease;
cursor: pointer;
}
.option:hover {
border-color: #667eea;
transform: translateX(5px);
}
.option.correct {
border-color: #28a745;
background: #d4edda;
}
.option-key {
display: inline-block;
width: 28px;
height: 28px;
background: #667eea;
color: white;
border-radius: 50%;
text-align: center;
line-height: 28px;
font-weight: bold;
margin-right: 10px;
}
.option.correct .option-key {
background: #28a745;
}
.metadata {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid #dee2e6;
}
.metadata-item {
font-size: 0.9em;
color: #6c757d;
}
.metadata-item strong {
color: #495057;
display: block;
margin-bottom: 3px;
}
.capabilities {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 10px;
}
.capability-tag {
background: #e7f1ff;
color: #004085;
padding: 5px 12px;
border-radius: 15px;
font-size: 0.85em;
border: 1px solid #b8daff;
}
.images-container {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin: 15px 0;
padding: 15px;
background: white;
border-radius: 8px;
}
.image-item {
flex: 1;
min-width: 250px;
max-width: 500px;
display: flex;
flex-direction: column;
gap: 10px;
}
.image-display {
width: 100%;
border-radius: 8px;
border: 2px solid #e0e0e0;
transition: all 0.3s ease;
cursor: pointer;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.image-display:hover {
border-color: #667eea;
box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
transform: scale(1.02);
}
.image-path {
font-family: 'Courier New', monospace;
background: #f8f9fa;
padding: 8px 12px;
border-radius: 5px;
color: #495057;
font-size: 0.85em;
word-break: break-all;
border-left: 3px solid #667eea;
}
.image-error {
background: #fff3cd;
border: 2px dashed #ffc107;
padding: 30px;
text-align: center;
border-radius: 8px;
color: #856404;
}
.toc {
background: white;
border-radius: 15px;
padding: 25px;
margin-bottom: 25px;
box-shadow: 0 5px 20px rgba(0,0,0,0.15);
}
.toc h2 {
color: #667eea;
margin-bottom: 15px;
font-size: 1.5em;
}
.toc ul {
list-style: none;
}
.toc li {
padding: 10px;
border-bottom: 1px solid #e0e0e0;
transition: all 0.3s ease;
}
.toc li:last-child {
border-bottom: none;
}
.toc li:hover {
background: #f8f9fa;
padding-left: 20px;
}
.toc a {
color: #495057;
text-decoration: none;
display: flex;
justify-content: space-between;
align-items: center;
}
.toc a:hover {
color: #667eea;
}
.item-count {
background: #e7f1ff;
color: #004085;
padding: 3px 10px;
border-radius: 15px;
font-size: 0.85em;
}
@media (max-width: 768px) {
.header h1 {
font-size: 1.8em;
}
.options {
grid-template-columns: 1fr;
}
.stats {
grid-template-columns: 1fr;
}
}
.back-to-top {
position: fixed;
bottom: 30px;
right: 30px;
background: #667eea;
color: white;
width: 50px;
height: 50px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
transition: all 0.3s ease;
text-decoration: none;
font-size: 1.5em;
}
.back-to-top:hover {
background: #764ba2;
transform: translateY(-5px);
}
</style>
</head>
<body>
<div class="container">
"""
def generate_html_footer() -> str:
"""生成HTML尾部"""
return """
</div>
<a href="#top" class="back-to-top">↑</a>
<script>
// 点击选项高亮显示
document.querySelectorAll('.option').forEach(option => {
option.addEventListener('click', function() {
this.style.transform = 'scale(1.05)';
setTimeout(() => {
this.style.transform = '';
}, 200);
});
});
// 返回顶部按钮
window.addEventListener('scroll', function() {
const backToTop = document.querySelector('.back-to-top');
if (window.pageYOffset > 300) {
backToTop.style.display = 'flex';
} else {
backToTop.style.display = 'none';
}
});
</script>
</body>
</html>
"""
def generate_task_html(filename: str, data: List[Dict[str, Any]], task_id: int) -> str:
"""生成单个任务的HTML"""
if not data:
return ""
task_name = get_task_name(filename)
first_item = data[0]
html = f"""
<div class="task-section" id="task-{task_id}">
<div class="task-header">
<div class="task-title">
<span>📋</span>
<span>{task_name}</span>
<span class="badge">{len(data)} 条数据</span>
</div>
<div class="task-meta">
<span>📁 <strong>文件:</strong> {filename}</span>
<span>🏷️ <strong>类别:</strong> {first_item.get('category', 'N/A')}</span>
<span>📊 <strong>任务:</strong> {first_item.get('task', 'N/A')}</span>
<span>🎯 <strong>子任务:</strong> {first_item.get('subtask', 'N/A')}</span>
</div>
</div>
"""
for idx, item in enumerate(data, 1):
# 问题
question = item.get('question', '')
answer = item.get('answer', '')
options = item.get('options', {})
correct_answer = item.get('correct_answer', '')
capabilities = item.get('capabilities', [])
# 图片路径
image_path = item.get('image_path', '')
images = item.get('images', [])
bev_image = item.get('bev_image', '')
view_image = item.get('view_image', '')
reference_images = item.get('reference_images', [])
option_images = item.get('option_images', [])
html += f"""
<div class="qa-item">
<div class="question">
<span class="qa-number">{idx}</span>
<span class="question-text">{question}</span>
</div>
"""
# 根据不同类型显示问题相关图片
# 1. BEV to View: 只显示BEV图
if bev_image:
html += """
<div class="images-container">
"""
html += f"""
<div class="image-item">
<img src="{bev_image}" alt="BEV Image" class="image-display"
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
loading="lazy">
<div class="image-error" style="display:none;">
⚠️ 图片加载失败<br>
<small>请检查图片路径是否正确</small>
</div>
<div class="image-path">📁 {bev_image}</div>
</div>
"""
html += """
</div>
"""
# 2. View to BEV: 只显示View图
elif view_image:
html += """
<div class="images-container">
"""
html += f"""
<div class="image-item">
<img src="{view_image}" alt="View Image" class="image-display"
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
loading="lazy">
<div class="image-error" style="display:none;">
⚠️ 图片加载失败<br>
<small>请检查图片路径是否正确</small>
</div>
<div class="image-path">📁 {view_image}</div>
</div>
"""
html += """
</div>
"""
# 3. Temporal Between: 只显示参考图片(2张)
elif reference_images:
html += """
<div class="images-container">
"""
for ref_idx, ref_img in enumerate(reference_images, 1):
html += f"""
<div class="image-item">
<img src="{ref_img}" alt="Reference Image {ref_idx}" class="image-display"
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
loading="lazy">
<div class="image-error" style="display:none;">
⚠️ 图片加载失败<br>
<small>请检查图片路径是否正确</small>
</div>
<div class="image-path">📁 {ref_img}</div>
</div>
"""
html += """
</div>
"""
# 4. 普通单图或多图
elif image_path or (images and not option_images):
html += """
<div class="images-container">
"""
if image_path:
html += f"""
<div class="image-item">
<img src="{image_path}" alt="VQA Image" class="image-display"
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
loading="lazy">
<div class="image-error" style="display:none;">
⚠️ 图片加载失败<br>
<small>请检查图片路径是否正确</small>
</div>
<div class="image-path">📁 {image_path}</div>
</div>
"""
elif images:
for img_idx, img in enumerate(images, 1):
html += f"""
<div class="image-item">
<img src="{img}" alt="VQA Image {img_idx}" class="image-display"
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
loading="lazy">
<div class="image-error" style="display:none;">
⚠️ 图片加载失败<br>
<small>请检查图片路径是否正确</small>
</div>
<div class="image-path">📁 {img}</div>
</div>
"""
html += """
</div>
"""
# 选项
if options:
html += """
<div class="options">
"""
for key_idx, key in enumerate(sorted(options.keys())):
value = options[key]
is_correct = key == correct_answer
correct_class = ' correct' if is_correct else ''
# 如果有选项图片,只显示图片+字母标签,不显示文字
if option_images and key_idx < len(option_images):
option_img = option_images[key_idx]
html += f"""
<div class="option{correct_class}" style="display: flex; flex-direction: column; align-items: center; gap: 8px;">
<img src="{option_img}" alt="Option {key}" class="image-display"
style="max-width: 200px;"
onerror="this.style.display='none';"
loading="lazy">
<div class="option-key" style="margin: 0;">{key}</div>
</div>
"""
else:
html += f"""
<div class="option{correct_class}">
<span class="option-key">{key}</span>
<span>{value}</span>
</div>
"""
html += """
</div>
"""
# 答案
if answer:
html += f"""
<div class="answer">
<div class="answer-label">正确答案 ({correct_answer})</div>
<div class="answer-text">{answer}</div>
</div>
"""
# 元数据
metadata_items = []
for key, value in item.items():
if key not in ['question', 'answer', 'options', 'correct_answer',
'capabilities', 'image_path', 'images', 'category',
'task', 'subtask']:
metadata_items.append((key, value))
if metadata_items or capabilities:
html += """
<div class="metadata">
"""
for key, value in metadata_items:
html += f"""
<div class="metadata-item">
<strong>{key}:</strong> {value}
</div>
"""
html += """
</div>
"""
if capabilities:
html += """
<div class="capabilities">
<strong style="width: 100%; color: #495057;">所需能力:</strong>
"""
for cap in capabilities:
html += f"""
<span class="capability-tag">💡 {cap}</span>
"""
html += """
</div>
"""
html += """
</div>
"""
html += """
</div>
"""
return html
def generate_statistics(all_data: Dict[str, List[Dict[str, Any]]]) -> Dict[str, Any]:
"""生成统计信息"""
total_items = sum(len(data) for data in all_data.values())
total_files = len(all_data)
categories = set()
tasks = set()
for data in all_data.values():
for item in data:
if 'category' in item:
categories.add(item['category'])
if 'task' in item:
tasks.add(item['task'])
return {
'total_items': total_items,
'total_files': total_files,
'total_categories': len(categories),
'total_tasks': len(tasks)
}
def collect_image_paths(all_data: Dict[str, List[Dict[str, Any]]]) -> Set[str]:
"""从数据中收集所有图片路径"""
image_paths = set()
for data in all_data.values():
for item in data:
# 收集单个图片路径
if 'image_path' in item and item['image_path']:
image_paths.add(item['image_path'])
# 收集图片列表
if 'images' in item and item['images']:
for img in item['images']:
if img:
image_paths.add(img)
# 收集BEV图片
if 'bev_image' in item and item['bev_image']:
image_paths.add(item['bev_image'])
# 收集View图片
if 'view_image' in item and item['view_image']:
image_paths.add(item['view_image'])
# 收集参考图片
if 'reference_images' in item and item['reference_images']:
for img in item['reference_images']:
if img:
image_paths.add(img)
# 收集选项图片
if 'option_images' in item and item['option_images']:
for img in item['option_images']:
if img:
image_paths.add(img)
return image_paths
def copy_images(source_dir: str, target_dir: Path, image_paths: Set[str]):
"""
从源目录复制图片到目标目录
Args:
source_dir: 源图片目录,用于与JSON中的图片路径拼接
target_dir: 目标目录 (dataset_dir/images),会先删除再重新创建
image_paths: JSON中的所有图片路径
"""
source_path = Path(source_dir)
if not source_path.exists():
print(f"⚠️ 警告: 源图片目录不存在: {source_dir}")
return
# 删除旧的 dataset_dir/images 文件夹
if target_dir.exists():
print(f"🗑️ 删除旧的图片文件夹: {target_dir}")
shutil.rmtree(target_dir)
# 创建新的 images 文件夹
target_dir.mkdir(parents=True, exist_ok=True)
print(f"📁 创建图片文件夹: {target_dir}")
# 复制图片
copied_count = 0
failed_count = 0
for img_path in image_paths:
# 将源目录和JSON中的图片路径拼接
image_name = img_path.split('/')[-1]
time_stamp = img_path.split('/')[-2]
source_file = source_path / time_stamp / 'high_quality_rgb/' / image_name
# 目标文件保持相对路径结构
target_file = target_dir / time_stamp / image_name
# 确保目标文件的父目录存在
target_file.parent.mkdir(parents=True, exist_ok=True)
if source_file.exists():
try:
shutil.copy2(source_file, target_file)
copied_count += 1
except Exception as e:
print(f" ⚠️ 复制失败 {img_path}: {e}")
failed_count += 1
else:
print(f" ⚠️ 源文件不存在: {source_file}")
failed_count += 1
print(f"✅ 图片复制完成: 成功 {copied_count} 个, 失败 {failed_count} 个")
def visualize_vqa_dataset(dataset_dir: str, source_image_dir: str = None, output_file: str = None):
"""
可视化VQA数据集
Args:
dataset_dir: JSONL文件所在目录
source_image_dir: 原始图片文件夹路径(可选)
output_file: 输出HTML文件路径,默认为 dataset_dir/vqa_visualization.html
"""
dataset_path = Path(dataset_dir)
if output_file is None:
output_file = dataset_path / 'vqa_visualization.html'
# 查找所有JSONL文件
jsonl_files = sorted(dataset_path.glob('*.jsonl'))
if not jsonl_files:
print(f"❌ 在 {dataset_dir} 目录下没有找到JSONL文件")
return
print(f"📁 找到 {len(jsonl_files)} 个JSONL文件")
# 加载所有数据
all_data = {}
for jsonl_file in jsonl_files:
filename = jsonl_file.name
print(f"📖 加载: {filename}")
data = load_jsonl(jsonl_file)
all_data[filename] = data
print(f" ✓ 加载了 {len(data)} 条数据")
# 如果提供了源图片目录,复制图片
if source_image_dir:
print(f"\n📸 开始处理图片...")
image_paths = collect_image_paths(all_data)
print(f" 找到 {len(image_paths)} 个唯一图片路径")
# 目标图片文件夹为 dataset_dir/images
target_image_dir = dataset_path / 'images'
copy_images(source_image_dir, target_image_dir, image_paths)
# 生成统计信息
stats = generate_statistics(all_data)
# 开始生成HTML
html_content = generate_html_header()
# 添加头部
html_content += f"""
<div class="header" id="top">
<h1>🚦 Traffic VQA Benchmark</h1>
<p class="subtitle">交通视觉问答基准数据集 - 数据可视化</p>
<div class="stats">
<div class="stat-card">
<div class="number">{stats['total_items']}</div>
<div class="label">总问题数</div>
</div>
<div class="stat-card">
<div class="number">{stats['total_files']}</div>
<div class="label">数据文件</div>
</div>
<div class="stat-card">
<div class="number">{stats['total_categories']}</div>
<div class="label">问题类别</div>
</div>
<div class="stat-card">
<div class="number">{stats['total_tasks']}</div>
<div class="label">任务类型</div>
</div>
</div>
</div>
"""
# 添加目录
html_content += """
<div class="toc">
<h2>📚 目录导航</h2>
<ul>
"""
for idx, (filename, data) in enumerate(all_data.items(), 1):
task_name = get_task_name(filename)
html_content += f"""
<li>
<a href="#task-{idx}">
<span>{task_name}</span>
<span class="item-count">{len(data)} 条</span>
</a>
</li>
"""
html_content += """
</ul>
</div>
"""
# 添加每个任务的内容
for idx, (filename, data) in enumerate(all_data.items(), 1):
html_content += generate_task_html(filename, data, idx)
# 添加尾部
html_content += generate_html_footer()
# 写入文件
with open(output_file, 'w', encoding='utf-8') as f:
f.write(html_content)
print(f"\n✅ 可视化文件已生成: {output_file}")
print(f"📊 共生成 {stats['total_items']} 个问答对的可视化")
print(f"💡 在浏览器中打开该文件即可查看")
def main():
parser = argparse.ArgumentParser(
description='将VQA JSONL数据转换为美观的HTML可视化页面',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例用法:
# 可视化benchmark_dataset目录下的所有JSONL文件
python visualize_vqa_data.py benchmark_dataset
# 指定原始图片文件夹并复制图片
python visualize_vqa_data.py benchmark_dataset -i /path/to/original/images
# 指定输出文件
python visualize_vqa_data.py benchmark_dataset -o my_visualization.html
# 完整示例
python visualize_vqa_data.py benchmark_dataset -i original_images -o output.html
"""
)
parser.add_argument(
'dataset_dir',
type=str,
help='包含JSONL文件的数据集目录'
)
parser.add_argument(
'-i', '--images',
type=str,
default=None,
dest='source_image_dir',
help='原始图片文件夹路径(可选)。如果提供,会先删除 dataset_dir/images 文件夹,'
'然后将此路径与JSONL文件中的图片路径拼接,复制图片到 dataset_dir/images 文件夹'
)
parser.add_argument(
'-o', '--output',
type=str,
default=None,
help='输出HTML文件路径(默认: dataset_dir/vqa_visualization.html)'