File size: 54,332 Bytes
56dae00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe7ed23
56dae00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe7ed23
 
 
 
 
 
 
 
 
 
 
 
56dae00
 
 
 
 
 
 
 
fe7ed23
 
 
 
 
 
 
 
 
 
 
 
56dae00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
#!/usr/bin/env python3
"""
数据集预处理统一入口

用法:
  python preprocess.py extract              # 解析 H5 文件
  python preprocess.py extract --check      # 仅检查 H5 结构
  python preprocess.py extract --update     # 更新 metadata(添加热力图/视频路径)
  python preprocess.py heatmap              # 生成热力图
  python preprocess.py heatmap --test       # 测试热力图生成
  python preprocess.py marker_flow          # 生成 xela marker flow 可视化
  python preprocess.py marker_flow --test   # 测试 marker flow 生成
  python preprocess.py video                # 生成视频
  python preprocess.py video --test         # 测试视频生成
  python preprocess.py pack                 # 打包图像为 tar 文件
  python preprocess.py pack --delete        # 打包后删除原始图像
  python preprocess.py unpack               # 解压 tar 文件
  python preprocess.py unpack --delete      # 解压后删除 tar 文件
  python preprocess.py clean                # 删除所有 PNG,只保留视频
  python preprocess.py upload               # 上传到 Hugging Face
  python preprocess.py upload --sync        # 同步上传(删除远端多余文件)
  python preprocess.py all                  # 完整流程(extract -> heatmap -> video -> update)
"""

import argparse
import json
import subprocess
import tempfile
import inspect
from pathlib import Path
from collections import defaultdict

import h5py
import numpy as np
from PIL import Image
from tqdm import tqdm
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt


# ============================================================
# 配置
# ============================================================

BASE_DIR = Path(__file__).parent

# 热力图配置
TACTILE_VMIN = 15
TACTILE_VMAX = 750
TACTILE_CMAP = 'plasma'
XELA_VMIN = -5
XELA_VMAX = 5
XELA_CMAP = 'RdBu_r'


# ============================================================
# 热力图生成函数
# ============================================================

def save_tactile_heatmap(data, output_path, rows=11, cols=6):
    """保存 tactile 热力图"""
    data = np.array(data)
    if len(data.shape) == 1:
        if len(data) == rows * cols:
            data = data.reshape(rows, cols)
        else:
            data = data.reshape(1, -1)
    
    fig, ax = plt.subplots(figsize=(cols * 0.5, rows * 0.5))
    ax.imshow(data, cmap=TACTILE_CMAP, aspect='equal', interpolation='nearest',
              vmin=TACTILE_VMIN, vmax=TACTILE_VMAX)
    ax.axis('off')
    plt.savefig(output_path, dpi=80, bbox_inches='tight', pad_inches=0)
    plt.close(fig)


def save_xela_heatmap(data, output_path):
    """保存 xela 热力图(Z轴热力图 + XY箭头)"""
    data = np.array(data)
    
    if len(data) == 72:
        data = data.reshape(4, 6, 3)
        fx, fy, fz = data[:, :, 0], data[:, :, 1], data[:, :, 2]
        
        fig, ax = plt.subplots(figsize=(4, 3))
        ax.imshow(fz, cmap=XELA_CMAP, aspect='equal', interpolation='nearest',
                  vmin=XELA_VMIN, vmax=XELA_VMAX)
        
        rows, cols = 4, 6
        y_grid, x_grid = np.mgrid[0:rows, 0:cols]
        magnitude = np.sqrt(fx**2 + fy**2)
        max_mag = magnitude.max() if magnitude.max() > 0 else 1
        scale = 0.4 / max_mag
        
        ax.quiver(x_grid, y_grid, fx * scale, -fy * scale,
                  color='black', scale=1, scale_units='xy',
                  width=0.02, headwidth=3, headlength=2)
        ax.axis('off')
        plt.savefig(output_path, dpi=100, bbox_inches='tight', pad_inches=0)
        plt.close(fig)
    else:
        fig, ax = plt.subplots(figsize=(6, 1))
        ax.imshow(data.reshape(1, -1), cmap=XELA_CMAP, aspect='auto',
                  vmin=XELA_VMIN, vmax=XELA_VMAX)
        ax.axis('off')
        plt.savefig(output_path, dpi=80, bbox_inches='tight', pad_inches=0)
        plt.close(fig)


def save_xela_marker_flow(data, output_path):
    """
    保存 xela marker flow 可视化
    - 网格上的圆点根据 XY 力偏移(与箭头方向一致)
    - Z 轴力用圆点大小和颜色表示
    """
    data = np.array(data)
    
    if len(data) != 72:
        return
    
    data = data.reshape(4, 6, 3)
    fx, fy, fz = data[:, :, 0], data[:, :, 1], data[:, :, 2]
    
    # 使用与箭头相同的 scale 计算
    magnitude = np.sqrt(fx**2 + fy**2)
    max_mag = magnitude.max() if magnitude.max() > 0 else 1
    scale = 0.4 / max_mag  # 最大偏移 0.4 格
    
    rows, cols = 4, 6
    fig, ax = plt.subplots(figsize=(6, 4))
    
    # 使用 imshow 建立与 heatmap 完全相同的坐标系
    bg = np.ones((rows, cols)) * 0.95  # 浅灰背景
    ax.imshow(bg, cmap='gray', vmin=0, vmax=1, aspect='equal')
    
    # 绘制原始网格位置(浅灰色小点)
    for i in range(rows):
        for j in range(cols):
            ax.plot(j, i, 'o', color='#cccccc', markersize=8)
    
    # 绘制偏移后的 marker(与 quiver 完全相同的方向处理)
    for i in range(rows):
        for j in range(cols):
            # 偏移量与 quiver 箭头方向完全一致
            dx = fx[i, j] * scale
            dy = -fy[i, j] * scale  # 与 quiver 中的 -fy 一致
            
            # 新位置
            new_x = j + dx
            new_y = i + dy
            
            # 连线(从原点到新位置)
            ax.plot([j, new_x], [i, new_y], '-', color='#888888', linewidth=1, alpha=0.5)
            
            # 圆点大小根据 Z 轴力(法向力),使用固定范围
            z_normalized = abs(fz[i, j]) / XELA_VMAX  # 归一化到 [0, 1]
            size = 8 + z_normalized * 15  # 基础大小 8,最大 23
            size = min(max(size, 6), 25)  # 限制范围
            
            # 颜色根据 Z 轴力(正负)
            if fz[i, j] > 0:
                color = '#e74c3c'  # 红色(正向力/压力)
            else:
                color = '#3498db'  # 蓝色(负向力/拉力)
            
            ax.plot(new_x, new_y, 'o', color=color, markersize=size, 
                    markeredgecolor='white', markeredgewidth=0.5)
    
    ax.axis('off')
    plt.savefig(output_path, dpi=100, bbox_inches='tight', pad_inches=0.1)
    plt.close(fig)


# ============================================================
# H5 解析函数
# ============================================================

def check_h5_structure():
    """检查 H5 文件结构"""
    folder_keys = defaultdict(lambda: defaultdict(set))
    h5_folders = [d for d in BASE_DIR.iterdir() if d.is_dir() and d.name.endswith('_h5')]
    
    for h5_folder in sorted(h5_folders):
        h5_files = list(h5_folder.rglob('*.h5'))
        print(f"\n{'='*60}\n文件夹: {h5_folder.name} ({len(h5_files)} 个文件)\n{'='*60}")
        
        for h5_path in h5_files[:3]:
            print(f"\n  {h5_path.name}:")
            with h5py.File(h5_path, 'r') as f:
                for key in sorted(f.keys()):
                    arr = f[key]
                    print(f"    - {key}: shape={arr.shape}, dtype={arr.dtype}")
                    folder_keys[h5_folder.name][key].add(str(arr.shape))
        
        for h5_path in h5_files:
            with h5py.File(h5_path, 'r') as f:
                for key in f.keys():
                    folder_keys[h5_folder.name][key].add(str(f[key].shape))
        
        print(f"\n  汇总:")
        for key, shapes in sorted(folder_keys[h5_folder.name].items()):
            print(f"    - {key}: {list(shapes)}")


def extract_pose_data(h5_path, output_dir, episode_id, subset_path=""):
    """解析 pose_data H5 文件"""
    episode_dir = output_dir / episode_id
    episode_dir.mkdir(parents=True, exist_ok=True)
    rel_prefix = f"{subset_path}/{episode_id}" if subset_path else episode_id
    records = []
    
    with h5py.File(h5_path, 'r') as f:
        keys = list(f.keys())
        num_frames = len(f['timestamps'][:]) if 'timestamps' in keys else 0
        data_cache = {}
        image_paths = {}
        
        for key in keys:
            arr = f[key][:]
            if arr.dtype == np.uint8:
                if len(arr.shape) == 3:
                    filename = "bg.png"
                    Image.fromarray(arr).save(episode_dir / filename)
                    data_cache[f"{key}_image"] = f"{rel_prefix}/{filename}"
                elif len(arr.shape) == 4:
                    paths = []
                    for i, img in enumerate(arr):
                        filename = f"{key}_{i:04d}.png"
                        Image.fromarray(img).save(episode_dir / filename)
                        paths.append(f"{rel_prefix}/{filename}")
                    image_paths[key] = paths
                elif len(arr.shape) == 5:
                    num_samples = arr.shape[1]
                    paths = []
                    for frame_idx in range(arr.shape[0]):
                        frame_paths = []
                        for sample_idx in range(num_samples):
                            filename = f"{key}_f{frame_idx:04d}_s{sample_idx}.png"
                            Image.fromarray(arr[frame_idx, sample_idx]).save(episode_dir / filename)
                            frame_paths.append(f"{rel_prefix}/{filename}")
                        paths.append(frame_paths)
                    image_paths[key] = paths
                    data_cache[f"{key}_num_samples"] = num_samples
            else:
                data_cache[key] = arr.tolist()
        
        for frame_idx in range(num_frames):
            record = {"episode_id": episode_id, "frame_idx": frame_idx}
            if subset_path:
                record["subset"] = subset_path
            
            for key, paths in image_paths.items():
                if isinstance(paths[0], list):
                    for s_idx, p in enumerate(paths[frame_idx]):
                        if s_idx == 0:
                            record["file_name"] = p
                        record[f"image_s{s_idx}"] = p
                else:
                    record["file_name"] = paths[frame_idx]
            
            for key, val in data_cache.items():
                if key.endswith("_image") or key.endswith("_num_samples"):
                    record[key] = val
            
            if 'timestamps' in data_cache:
                record["timestamp"] = data_cache['timestamps'][frame_idx]
            if 'rotations' in data_cache:
                record["rotation"] = data_cache['rotations'][frame_idx]
            if 'translations' in data_cache:
                record["translation"] = data_cache['translations'][frame_idx]
            if 'tactile' in data_cache:
                record["tactile"] = data_cache['tactile'][frame_idx]
            if 'xela' in data_cache:
                record["xela"] = data_cache['xela'][frame_idx]
            
            record["num_frames"] = num_frames
            records.append(record)
    
    return records


def extract_force_data(h5_path, output_dir, episode_id, subset_path=""):
    """解析 force_data H5 文件"""
    episode_dir = output_dir / episode_id
    episode_dir.mkdir(parents=True, exist_ok=True)
    rel_prefix = f"{subset_path}/{episode_id}" if subset_path else episode_id
    records = []
    
    with h5py.File(h5_path, 'r') as f:
        keys = list(f.keys())
        num_frames = 0
        data_cache = {}
        image_paths = {}
        
        for key in keys:
            arr = f[key][:]
            if arr.dtype == np.uint8:
                if len(arr.shape) == 3:
                    filename = f"{key}.png"
                    Image.fromarray(arr).save(episode_dir / filename)
                    data_cache[f"{key}_image"] = f"{rel_prefix}/{filename}"
                elif len(arr.shape) == 4:
                    num_frames = max(num_frames, len(arr))
                    paths = []
                    for i, img in enumerate(arr):
                        filename = f"{key}_{i:04d}.png"
                        Image.fromarray(img).save(episode_dir / filename)
                        paths.append(f"{rel_prefix}/{filename}")
                    image_paths[key] = paths
            else:
                data_cache[key] = arr.tolist()
                if len(arr.shape) >= 1:
                    num_frames = max(num_frames, len(arr))
        
        for frame_idx in range(num_frames):
            record = {"episode_id": episode_id, "frame_idx": frame_idx, "num_frames": num_frames}
            if subset_path:
                record["subset"] = subset_path
            
            for key, paths in image_paths.items():
                if frame_idx < len(paths):
                    record["file_name"] = paths[frame_idx]
            
            for key, val in data_cache.items():
                if key.endswith("_image"):
                    record[key] = val
                elif isinstance(val, list) and frame_idx < len(val):
                    record[key] = val[frame_idx]
            
            records.append(record)
    
    return records


def extract_tacniq_gsmini(h5_path, output_dir, episode_id, subset_path=""):
    """解析 tacniq_gsmini H5 文件"""
    episode_dir = output_dir / episode_id
    episode_dir.mkdir(parents=True, exist_ok=True)
    gsmini_dir = episode_dir / "gsmini"
    gsmini_dir.mkdir(parents=True, exist_ok=True)
    rel_prefix = f"{subset_path}/{episode_id}" if subset_path else episode_id
    records = []
    
    with h5py.File(h5_path, 'r') as f:
        bg = f['bg'][:]
        gsmini = f['gsmini'][:]
        tacniq = f['tacniq'][:].tolist()
    
    Image.fromarray(bg).save(episode_dir / "bg.png")
    num_frames = len(gsmini)
    
    for frame_idx in range(num_frames):
        gsmini_filename = f"frame_{frame_idx:04d}.png"
        Image.fromarray(gsmini[frame_idx]).save(gsmini_dir / gsmini_filename)
        
        records.append({
            "episode_id": episode_id,
            "frame_idx": frame_idx,
            "file_name": f"{rel_prefix}/gsmini/{gsmini_filename}",
            "gsmini_image": f"{rel_prefix}/gsmini/{gsmini_filename}",
            "bg_image": f"{rel_prefix}/bg.png",
            "tacniq": tacniq[frame_idx] if frame_idx < len(tacniq) else None,
            "num_frames": num_frames,
            "subset": subset_path if subset_path else None,
        })
    
    return records


def extract_xela_9dtact(h5_path, output_dir, episode_id, subset_path=""):
    """解析 xela_9dtact H5 文件"""
    episode_dir = output_dir / episode_id
    episode_dir.mkdir(parents=True, exist_ok=True)
    dtact_dir = episode_dir / "9dtact"
    dtact_dir.mkdir(parents=True, exist_ok=True)
    rel_prefix = f"{subset_path}/{episode_id}" if subset_path else episode_id
    records = []
    
    with h5py.File(h5_path, 'r') as f:
        bg = f['bg'][:]
        dtact = f['9dtact'][:]
        xela = f['xela'][:].tolist()
    
    Image.fromarray(bg).save(episode_dir / "bg.png")
    num_frames = len(dtact)
    
    for frame_idx in range(num_frames):
        dtact_filename = f"frame_{frame_idx:04d}.png"
        Image.fromarray(dtact[frame_idx]).save(dtact_dir / dtact_filename)
        
        records.append({
            "episode_id": episode_id,
            "frame_idx": frame_idx,
            "file_name": f"{rel_prefix}/9dtact/{dtact_filename}",
            "dtact_image": f"{rel_prefix}/9dtact/{dtact_filename}",
            "bg_image": f"{rel_prefix}/bg.png",
            "xela": xela[frame_idx] if frame_idx < len(xela) else None,
            "num_frames": num_frames,
            "subset": subset_path if subset_path else None,
        })
    
    return records


def extract_all():
    """解析所有 H5 文件"""
    h5_folders = [d for d in BASE_DIR.iterdir() if d.is_dir() and d.name.endswith('_h5')]
    
    for h5_folder in h5_folders:
        output_folder = BASE_DIR / h5_folder.name.replace('_h5', '')
        output_folder.mkdir(exist_ok=True)
        
        h5_files = list(h5_folder.rglob('*.h5'))
        print(f"\n解析 {h5_folder.name}: {len(h5_files)} 个文件")
        
        all_records = []
        
        for h5_path in tqdm(h5_files, desc=h5_folder.name):
            relative = h5_path.relative_to(h5_folder)
            sub_output_dir = output_folder / relative.parent
            sub_output_dir.mkdir(parents=True, exist_ok=True)
            
            episode_id = h5_path.stem
            subset_path = str(relative.parent) if relative.parent != Path('.') else ""
            
            try:
                if 'pose_data' in h5_folder.name:
                    records = extract_pose_data(h5_path, sub_output_dir, episode_id, subset_path)
                elif 'tacniq_gsmini' in h5_folder.name:
                    records = extract_tacniq_gsmini(h5_path, sub_output_dir, episode_id, subset_path)
                elif 'xela_9dtact' in h5_folder.name:
                    records = extract_xela_9dtact(h5_path, sub_output_dir, episode_id, subset_path)
                elif 'force_data' in h5_folder.name:
                    records = extract_force_data(h5_path, sub_output_dir, episode_id, subset_path)
                else:
                    continue
                
                all_records.extend(records)
                
                episode_dir = sub_output_dir / episode_id
                with open(episode_dir / "metadata.json", 'w') as f:
                    json.dump(records, f, indent=2, ensure_ascii=False)
                    
            except Exception as e:
                print(f"\nError: {h5_path}: {e}")
        
        with open(output_folder / "metadata.jsonl", 'w') as f:
            for record in all_records:
                f.write(json.dumps(record, ensure_ascii=False) + '\n')
        
        print(f"  生成 {len(all_records)} 条记录")


def update_metadata():
    """更新 metadata,添加热力图和视频路径"""
    data_folders = ['pose_data', 'force_data', 'tacniq_gsmini', 'xela_9dtact']
    updated_count = 0
    
    for folder_name in data_folders:
        folder = BASE_DIR / folder_name
        if not folder.exists():
            continue
        
        json_files = list(folder.rglob('metadata.json'))
        print(f"\n更新 {folder_name}: {len(json_files)} 个文件")
        
        for json_path in tqdm(json_files, desc=folder_name):
            episode_dir = json_path.parent
            rel_prefix = str(episode_dir.relative_to(BASE_DIR))
            
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            modified = False
            
            for record in records:
                frame_idx = record.get('frame_idx', 0)
                
                # 删除重复的 image 字段
                if 'image' in record and 'file_name' in record:
                    if record['image'] == record['file_name']:
                        del record['image']
                        modified = True
                
                # 添加热力图路径
                for s_idx in range(100):
                    for prefix, key_prefix in [('tactile', 'tactile_heatmap'), ('xela', 'xela_heatmap')]:
                        heatmap_file = episode_dir / f"{prefix}_f{frame_idx:04d}_s{s_idx:02d}.png"
                        if heatmap_file.exists():
                            key = f"{key_prefix}_s{s_idx:02d}"
                            new_path = f"{rel_prefix}/{prefix}_f{frame_idx:04d}_s{s_idx:02d}.png"
                            if record.get(key) != new_path:
                                record[key] = new_path
                                modified = True
                        else:
                            break
                
                for prefix in ['tac02', 'xela']:
                    heatmap_file = episode_dir / f"{prefix}_{frame_idx:04d}.png"
                    if heatmap_file.exists():
                        key = f"{prefix}_heatmap"
                        new_path = f"{rel_prefix}/{prefix}_{frame_idx:04d}.png"
                        if record.get(key) != new_path:
                            record[key] = new_path
                            modified = True
                
                for subdir, key in [('tacniq', 'tacniq_heatmap'), ('xela', 'xela_heatmap')]:
                    heatmap_file = episode_dir / subdir / f"heatmap_{frame_idx:04d}.png"
                    if heatmap_file.exists():
                        new_path = f"{rel_prefix}/{subdir}/heatmap_{frame_idx:04d}.png"
                        if record.get(key) != new_path:
                            record[key] = new_path
                            modified = True
            
            # 添加视频路径
            for video_file in episode_dir.glob('video*.mp4'):
                video_key = video_file.stem
                video_path = f"{rel_prefix}/{video_file.name}"
                for record in records:
                    if record.get(video_key) != video_path:
                        record[video_key] = video_path
                        modified = True
            
            if modified:
                with open(json_path, 'w') as f:
                    json.dump(records, f, indent=2, ensure_ascii=False)
                updated_count += 1
    
    print(f"\n更新 {updated_count} 个文件")
    
    # 重新生成 JSONL
    print("\n重新生成 JSONL...")
    for folder_name in data_folders:
        folder = BASE_DIR / folder_name
        if not folder.exists():
            continue
        
        all_records = []
        for json_path in folder.rglob('metadata.json'):
            with open(json_path, 'r') as f:
                all_records.extend(json.load(f))
        
        if all_records:
            with open(folder / "metadata.jsonl", 'w') as f:
                for record in all_records:
                    f.write(json.dumps(record, ensure_ascii=False) + '\n')
            print(f"  {folder_name}: {len(all_records)} 条记录")


# ============================================================
# 热力图生成
# ============================================================

def generate_heatmaps(data_type='all', test_only=False):
    """生成热力图"""
    
    def process_tac02_pose():
        data_dir = BASE_DIR / 'pose_data' / 'tac02_pose_h5'
        if not data_dir.exists():
            return
        print(f"\n处理 tac02_pose_h5...")
        episode_dirs = list(data_dir.iterdir())
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc="tac02_pose"):
            json_path = episode_dir / 'metadata.json'
            if not json_path.exists():
                continue
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            for record in (records[:1] if test_only else records):
                if 'tactile' not in record or record['tactile'] is None:
                    continue
                frame_idx = record['frame_idx']
                tactile = record['tactile']
                
                if isinstance(tactile[0], list):
                    for s_idx, sample in enumerate(tactile):
                        output_path = episode_dir / f"tactile_f{frame_idx:04d}_s{s_idx:02d}.png"
                        save_tactile_heatmap(sample, output_path)
                    if test_only:
                        print(f"  生成 {len(tactile)} 个热力图")
                        return
    
    def process_xela_pose():
        data_dir = BASE_DIR / 'pose_data' / 'xela_pose_h5'
        if not data_dir.exists():
            return
        print(f"\n处理 xela_pose_h5...")
        episode_dirs = list(data_dir.iterdir())
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc="xela_pose"):
            json_path = episode_dir / 'metadata.json'
            if not json_path.exists():
                continue
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            for record in (records[:1] if test_only else records):
                if 'xela' not in record or record['xela'] is None:
                    continue
                frame_idx = record['frame_idx']
                xela = record['xela']
                
                if isinstance(xela[0], list):
                    for s_idx, sample in enumerate(xela):
                        output_path = episode_dir / f"xela_f{frame_idx:04d}_s{s_idx:02d}.png"
                        save_xela_heatmap(sample, output_path)
                    if test_only:
                        print(f"  生成 {len(xela)} 个热力图")
                        return
    
    def process_force_data(sensor_type=None):
        force_dir = BASE_DIR / 'force_data'
        if not force_dir.exists():
            return
        
        for subset_dir in force_dir.iterdir():
            if not subset_dir.is_dir():
                continue
            
            if 'tac02' in subset_dir.name:
                if sensor_type and sensor_type != 'tac02':
                    continue
                data_key, prefix = 'tac02', 'tac02'
            elif 'xela' in subset_dir.name:
                if sensor_type and sensor_type != 'xela':
                    continue
                data_key, prefix = 'xela', 'xela'
            else:
                continue
            
            print(f"\n处理 {subset_dir.name}...")
            episode_dirs = list(subset_dir.iterdir())
            if test_only:
                episode_dirs = episode_dirs[:1]
            
            for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc=subset_dir.name):
                json_path = episode_dir / 'metadata.json'
                if not json_path.exists():
                    continue
                with open(json_path, 'r') as f:
                    records = json.load(f)
                
                for record in (records[:1] if test_only else records):
                    if data_key not in record or record[data_key] is None:
                        continue
                    frame_idx = record['frame_idx']
                    heatmap_path = episode_dir / f"{prefix}_{frame_idx:04d}.png"
                    if prefix == 'tac02':
                        save_tactile_heatmap(record[data_key], heatmap_path)
                    else:
                        save_xela_heatmap(record[data_key], heatmap_path)
                    if test_only:
                        print(f"  生成: {heatmap_path}")
                        return
    
    def process_tacniq_gsmini():
        data_dir = BASE_DIR / 'tacniq_gsmini'
        if not data_dir.exists():
            return
        print(f"\n处理 tacniq_gsmini...")
        episode_dirs = list(data_dir.iterdir())
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc="tacniq_gsmini"):
            json_path = episode_dir / 'metadata.json'
            if not json_path.exists():
                continue
            
            tacniq_dir = episode_dir / 'tacniq'
            tacniq_dir.mkdir(parents=True, exist_ok=True)
            
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            for record in (records[:1] if test_only else records):
                if 'tacniq' not in record or record['tacniq'] is None:
                    continue
                frame_idx = record['frame_idx']
                heatmap_path = tacniq_dir / f"heatmap_{frame_idx:04d}.png"
                save_tactile_heatmap(record['tacniq'], heatmap_path)
                if test_only:
                    print(f"  生成: {heatmap_path}")
                    return
    
    def process_xela_9dtact():
        data_dir = BASE_DIR / 'xela_9dtact'
        if not data_dir.exists():
            return
        print(f"\n处理 xela_9dtact...")
        episode_dirs = list(data_dir.iterdir())
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc="xela_9dtact"):
            json_path = episode_dir / 'metadata.json'
            if not json_path.exists():
                continue
            
            xela_dir = episode_dir / 'xela'
            xela_dir.mkdir(parents=True, exist_ok=True)
            
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            for record in (records[:1] if test_only else records):
                if 'xela' not in record or record['xela'] is None:
                    continue
                frame_idx = record['frame_idx']
                heatmap_path = xela_dir / f"heatmap_{frame_idx:04d}.png"
                save_xela_heatmap(record['xela'], heatmap_path)
                if test_only:
                    print(f"  生成: {heatmap_path}")
                    return
    
    t = data_type
    if t in ['tac02_pose', 'pose', 'all']:
        process_tac02_pose()
    if t in ['xela_pose', 'pose', 'all']:
        process_xela_pose()
    if t in ['tac02_force', 'force', 'all']:
        process_force_data('tac02')
    if t in ['xela_force', 'force', 'all']:
        process_force_data('xela')
    if t in ['tacniq_gsmini', 'all']:
        process_tacniq_gsmini()
    if t in ['xela_9dtact', 'all']:
        process_xela_9dtact()


def generate_marker_flow(data_type='all', test_only=False):
    """生成 xela marker flow 可视化"""
    
    def process_xela_pose():
        data_dir = BASE_DIR / 'pose_data' / 'xela_pose_h5'
        if not data_dir.exists():
            return
        print(f"\n生成 xela_pose marker flow...")
        episode_dirs = list(data_dir.iterdir())
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc="xela_pose"):
            json_path = episode_dir / 'metadata.json'
            if not json_path.exists():
                continue
            
            # 创建 marker_flow 子文件夹
            flow_dir = episode_dir / 'marker_flow'
            flow_dir.mkdir(parents=True, exist_ok=True)
            
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            for record in (records[:1] if test_only else records):
                if 'xela' not in record or record['xela'] is None:
                    continue
                frame_idx = record['frame_idx']
                xela = record['xela']
                
                if isinstance(xela[0], list):
                    for s_idx, sample in enumerate(xela):
                        output_path = flow_dir / f"flow_f{frame_idx:04d}_s{s_idx:02d}.png"
                        save_xela_marker_flow(sample, output_path)
                    if test_only:
                        print(f"  生成 {len(xela)} 个 marker flow")
                        return
                else:
                    output_path = flow_dir / f"flow_{frame_idx:04d}.png"
                    save_xela_marker_flow(xela, output_path)
                    if test_only:
                        print(f"  生成: {output_path}")
                        return
    
    def process_xela_force():
        force_dir = BASE_DIR / 'force_data'
        if not force_dir.exists():
            return
        
        for subset_dir in force_dir.iterdir():
            if not subset_dir.is_dir() or 'xela' not in subset_dir.name:
                continue
            
            print(f"\n生成 {subset_dir.name} marker flow...")
            episode_dirs = list(subset_dir.iterdir())
            if test_only:
                episode_dirs = episode_dirs[:1]
            
            for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc=subset_dir.name):
                json_path = episode_dir / 'metadata.json'
                if not json_path.exists():
                    continue
                
                flow_dir = episode_dir / 'marker_flow'
                flow_dir.mkdir(parents=True, exist_ok=True)
                
                with open(json_path, 'r') as f:
                    records = json.load(f)
                
                for record in (records[:1] if test_only else records):
                    if 'xela' not in record or record['xela'] is None:
                        continue
                    frame_idx = record['frame_idx']
                    output_path = flow_dir / f"flow_{frame_idx:04d}.png"
                    save_xela_marker_flow(record['xela'], output_path)
                    if test_only:
                        print(f"  生成: {output_path}")
                        return
    
    def process_xela_9dtact():
        data_dir = BASE_DIR / 'xela_9dtact'
        if not data_dir.exists():
            return
        print(f"\n生成 xela_9dtact marker flow...")
        episode_dirs = list(data_dir.iterdir())
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        for episode_dir in tqdm([d for d in episode_dirs if d.is_dir()], desc="xela_9dtact"):
            json_path = episode_dir / 'metadata.json'
            if not json_path.exists():
                continue
            
            # marker_flow 放在 xela 子文件夹内
            flow_dir = episode_dir / 'xela' / 'marker_flow'
            flow_dir.mkdir(parents=True, exist_ok=True)
            
            with open(json_path, 'r') as f:
                records = json.load(f)
            
            for record in (records[:1] if test_only else records):
                if 'xela' not in record or record['xela'] is None:
                    continue
                frame_idx = record['frame_idx']
                output_path = flow_dir / f"flow_{frame_idx:04d}.png"
                save_xela_marker_flow(record['xela'], output_path)
                if test_only:
                    print(f"  生成: {output_path}")
                    return
    
    t = data_type
    if t in ['xela_pose', 'pose', 'all']:
        process_xela_pose()
    if t in ['xela_force', 'force', 'all']:
        process_xela_force()
    if t in ['xela_9dtact', 'all']:
        process_xela_9dtact()


# ============================================================
# 视频生成
# ============================================================

def create_video_from_images(episode_dir, output_path, image_patterns=None,
                             subdir=None, fps_fallback=10, multi_sample=False,
                             sample_pattern=None):
    """从图像序列创建视频"""
    json_path = episode_dir / 'metadata.json'
    if not json_path.exists():
        return False
    
    with open(json_path, 'r') as f:
        records = json.load(f)
    
    if not records:
        return False
    
    img_dir = episode_dir / subdir if subdir else episode_dir
    
    if multi_sample and sample_pattern:
        all_frames = []
        timestamps = []
        for record in records:
            frame_idx = record.get('frame_idx', len(timestamps))
            timestamp = (record.get('sensor_timestamps') or
                        record.get('force_timestamps') or
                        record.get('timestamp'))
            timestamps.append({'frame_idx': frame_idx, 'timestamp': timestamp})
        
        timestamps.sort(key=lambda x: x['frame_idx'])
        
        for i, ts_info in enumerate(timestamps):
            frame_idx = ts_info['frame_idx']
            sample_files = []
            for sample_idx in range(100):
                try:
                    filename = sample_pattern.format(idx=frame_idx, sample=sample_idx)
                    candidate = img_dir / filename
                    if candidate.exists():
                        sample_files.append(candidate)
                    else:
                        break
                except (KeyError, ValueError):
                    break
            
            if not sample_files:
                continue
            
            if i < len(timestamps) - 1 and ts_info['timestamp'] and timestamps[i+1]['timestamp']:
                frame_duration = max(0.01, min(2.0, timestamps[i+1]['timestamp'] - ts_info['timestamp']))
            else:
                frame_duration = 1.0 / fps_fallback
            
            sample_duration = frame_duration / len(sample_files)
            for sample_file in sample_files:
                all_frames.append({'path': sample_file, 'duration': sample_duration})
        
        if len(all_frames) < 2:
            return False
        
        # 把 concat 文件放在 episode 目录,使用相对路径
        concat_file = str(episode_dir / '_concat.txt')
        with open(concat_file, 'w') as f:
            for frame in all_frames:
                # 使用相对于 episode_dir 的路径
                rel_path = frame['path'].relative_to(episode_dir)
                f.write(f"file '{rel_path}'\nduration {frame['duration']:.6f}\n")
            rel_path = all_frames[-1]['path'].relative_to(episode_dir)
            f.write(f"file '{rel_path}'\n")
    else:
        if image_patterns is None:
            image_patterns = ["gelsight_{idx:04d}.png", "xela_{idx:04d}.png", "tac02_{idx:04d}.png"]
        
        frames = []
        for record in records:
            frame_idx = record.get('frame_idx', len(frames))
            image_file = None
            
            for field in ['file_name', 'gsmini_image', 'dtact_image']:
                if field in record and record[field]:
                    img_path = record[field].split('/')[-1]
                    candidate = img_dir / img_path
                    if candidate.exists():
                        image_file = candidate
                        break
            
            if not image_file:
                for pattern in image_patterns:
                    try:
                        candidate = img_dir / pattern.format(idx=frame_idx)
                        if candidate.exists():
                            image_file = candidate
                            break
                    except:
                        continue
            
            if not image_file and subdir:
                for pattern in [f"frame_{frame_idx:04d}.png", f"heatmap_{frame_idx:04d}.png"]:
                    candidate = img_dir / pattern
                    if candidate.exists():
                        image_file = candidate
                        break
            
            if image_file:
                timestamp = (record.get('sensor_timestamps') or
                            record.get('force_timestamps') or
                            record.get('timestamp'))
                frames.append({'path': image_file, 'timestamp': timestamp, 'frame_idx': frame_idx})
        
        if len(frames) < 2:
            return False
        
        frames.sort(key=lambda x: x['frame_idx'])
        
        # 把 concat 文件放在 episode 目录,使用相对路径
        concat_file = str(episode_dir / '_concat.txt')
        with open(concat_file, 'w') as f:
            for i, frame in enumerate(frames):
                if i < len(frames) - 1 and frame['timestamp'] and frames[i+1]['timestamp']:
                    duration = max(0.01, min(1.0, frames[i+1]['timestamp'] - frame['timestamp']))
                else:
                    duration = 1.0 / fps_fallback
                # 使用相对于 episode_dir 的路径
                rel_path = frame['path'].relative_to(episode_dir)
                f.write(f"file '{rel_path}'\nduration {duration:.6f}\n")
            rel_path = frames[-1]['path'].relative_to(episode_dir)
            f.write(f"file '{rel_path}'\n")
    
    # scale 确保宽高是 2 的倍数(libx264 要求)
    cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', concat_file,
           '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
           '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-crf', '23', output_path]
    
    try:
        result = subprocess.run(cmd, capture_output=True, text=True)
        return result.returncode == 0
    except FileNotFoundError:
        print("  错误: ffmpeg 未安装")
        return False
    finally:
        Path(concat_file).unlink(missing_ok=True)


def generate_videos(data_type='all', test_only=False):
    """生成视频"""
    
    def process(data_path, name, **kwargs):
        data_dir = BASE_DIR / data_path
        if not data_dir.exists():
            print(f"{data_path} 不存在")
            return
        
        print(f"\n处理 {name}...")
        episode_dirs = sorted([d for d in data_dir.iterdir() if d.is_dir()],
                             key=lambda x: int(x.name.split('_')[-1]))
        if test_only:
            episode_dirs = episode_dirs[:1]
        
        video_name = kwargs.pop('video_name', 'video.mp4')
        success = 0
        for episode_dir in tqdm(episode_dirs, desc=name):
            if create_video_from_images(episode_dir, str(episode_dir / video_name), **kwargs):
                success += 1
                if test_only:
                    print(f"  生成: {episode_dir / video_name}")
        print(f"  成功: {success}/{len(episode_dirs)}")
    
    t = data_type
    
    # force_data
    if t in ['9dtact_force', 'all']:
        process('force_data/9dtact_force_h5', '9dtact_force', image_patterns=["gelsight_{idx:04d}.png"])
    if t in ['xela_force', 'all']:
        process('force_data/xela_force_h5', 'xela_force', image_patterns=["xela_{idx:04d}.png"])
    if t in ['gelsight_force', 'all']:
        process('force_data/gelsight_force_h5', 'gelsight_force', image_patterns=["gelsight_{idx:04d}.png"])
    if t in ['tac02_force', 'all']:
        process('force_data/tac02_force_h5', 'tac02_force', image_patterns=["tac02_{idx:04d}.png"])
    
    # pose_data
    if t in ['gelsight_pose', 'all']:
        process('pose_data/gelsight_pose_h5', 'gelsight_pose', multi_sample=True, sample_pattern="images_f{idx:04d}_s{sample}.png")
    if t in ['9dtact_pose', 'all']:
        process('pose_data/9dtact_pose_h5', '9dtact_pose', multi_sample=True, sample_pattern="images_f{idx:04d}_s{sample}.png")
    if t in ['tac02_pose', 'all']:
        process('pose_data/tac02_pose_h5', 'tac02_pose', multi_sample=True, sample_pattern="tactile_f{idx:04d}_s{sample:02d}.png")
    if t in ['xela_pose', 'all']:
        process('pose_data/xela_pose_h5', 'xela_pose', multi_sample=True, sample_pattern="xela_f{idx:04d}_s{sample:02d}.png")
    
    # marker_flow 视频
    if t in ['xela_pose_flow', 'all']:
        process('pose_data/xela_pose_h5', 'xela_pose (marker_flow)', subdir='marker_flow', 
                multi_sample=True, sample_pattern="flow_f{idx:04d}_s{sample:02d}.png", video_name="video_flow.mp4")
    if t in ['xela_force_flow', 'all']:
        process('force_data/xela_force_h5', 'xela_force (marker_flow)', subdir='marker_flow',
                image_patterns=["flow_{idx:04d}.png"], video_name="video_flow.mp4")
    if t in ['xela_9dtact_flow', 'all']:
        process('xela_9dtact', 'xela_9dtact (marker_flow)', subdir='xela/marker_flow',
                image_patterns=["flow_{idx:04d}.png"], video_name="video_flow.mp4")
    
    # 双传感器
    if t in ['tacniq_gsmini', 'all']:
        process('tacniq_gsmini', 'tacniq (gsmini)', subdir='gsmini', image_patterns=["frame_{idx:04d}.png"], video_name="video_gsmini.mp4")
        process('tacniq_gsmini', 'tacniq (tacniq)', subdir='tacniq', image_patterns=["heatmap_{idx:04d}.png"], video_name="video_tacniq.mp4")
    if t in ['xela_9dtact', 'all']:
        process('xela_9dtact', 'xela_9dtact (9dtact)', subdir='9dtact', image_patterns=["frame_{idx:04d}.png"], video_name="video_9dtact.mp4")
        process('xela_9dtact', 'xela_9dtact (xela)', subdir='xela', image_patterns=["heatmap_{idx:04d}.png"], video_name="video_xela.mp4")


# ============================================================
# 打包图像序列
# ============================================================

def pack_images(delete_originals=False):
    """
    把每个 episode 的图像序列打包成 tar 文件(WebDataset 格式)
    减少文件数量,便于上传 Hugging Face
    """
    import tarfile
    
    data_folders = ['pose_data', 'force_data', 'tacniq_gsmini', 'xela_9dtact']
    
    for folder_name in data_folders:
        folder = BASE_DIR / folder_name
        if not folder.exists():
            continue
        
        # 找到所有 episode 目录
        episode_dirs = []
        for p in folder.rglob('metadata.json'):
            episode_dirs.append(p.parent)
        
        print(f"\n打包 {folder_name}: {len(episode_dirs)} 个 episode")
        
        for episode_dir in tqdm(episode_dirs, desc=folder_name):
            # 收集所有图像文件
            image_files = list(episode_dir.glob('*.png'))
            
            # 检查子文件夹中的图像
            for subdir in ['gsmini', '9dtact', 'tacniq', 'xela', 'marker_flow']:
                subpath = episode_dir / subdir
                if subpath.exists():
                    image_files.extend(subpath.glob('*.png'))
                    # 嵌套子文件夹
                    for nested in subpath.iterdir():
                        if nested.is_dir():
                            image_files.extend(nested.glob('*.png'))
            
            if not image_files:
                continue
            
            # 创建 tar 文件
            tar_path = episode_dir / 'images.tar'
            with tarfile.open(tar_path, 'w') as tar:
                for img_path in image_files:
                    # 使用相对路径作为 tar 内的文件名
                    arcname = str(img_path.relative_to(episode_dir))
                    tar.add(img_path, arcname=arcname)
            
            # 删除原始图像文件
            if delete_originals:
                for img_path in image_files:
                    img_path.unlink()
                # 删除空的子文件夹
                for subdir in ['gsmini', '9dtact', 'tacniq', 'xela', 'marker_flow']:
                    subpath = episode_dir / subdir
                    if subpath.exists():
                        for nested in subpath.iterdir():
                            if nested.is_dir() and not any(nested.iterdir()):
                                nested.rmdir()
                        if not any(subpath.iterdir()):
                            subpath.rmdir()
    
    print("\n打包完成!")
    if delete_originals:
        print("原始图像文件已删除")


def unpack_images(delete_tar=False):
    """
    解压 tar 文件中的图像
    """
    import tarfile
    
    data_folders = ['pose_data', 'force_data', 'tacniq_gsmini', 'xela_9dtact']
    
    for folder_name in data_folders:
        folder = BASE_DIR / folder_name
        if not folder.exists():
            continue
        
        # 找到所有 tar 文件
        tar_files = list(folder.rglob('images.tar'))
        if not tar_files:
            continue
        
        print(f"\n解压 {folder_name}: {len(tar_files)} 个 tar 文件")
        
        for tar_path in tqdm(tar_files, desc=folder_name):
            episode_dir = tar_path.parent
            
            try:
                with tarfile.open(tar_path, 'r') as tar:
                    tar.extractall(path=episode_dir)
                
                if delete_tar:
                    tar_path.unlink()
            except Exception as e:
                print(f"\n  解压失败 {tar_path}: {e}")
    
    print("\n解压完成!")
    if delete_tar:
        print("tar 文件已删除")


def clean_images():
    """删除所有 PNG 图像,只保留视频和 metadata"""
    data_folders = ['pose_data', 'force_data', 'tacniq_gsmini', 'xela_9dtact']
    
    total_deleted = 0
    for folder_name in data_folders:
        folder = BASE_DIR / folder_name
        if not folder.exists():
            continue
        
        png_files = list(folder.rglob('*.png'))
        print(f"{folder_name}: {len(png_files)} 个 PNG 文件")
        
        for png_path in tqdm(png_files, desc=f"删除 {folder_name}"):
            png_path.unlink()
            total_deleted += 1
    
    # 删除空文件夹
    for folder_name in data_folders:
        folder = BASE_DIR / folder_name
        if not folder.exists():
            continue
        for subdir in folder.rglob('*'):
            if subdir.is_dir() and not any(subdir.iterdir()):
                subdir.rmdir()
    
    print(f"\n删除完成!共删除 {total_deleted} 个文件")


# ============================================================
# 上传
# ============================================================

def upload_to_hf(sync=False):
    """上传到 Hugging Face
    
    Args:
        sync: 如果为 True,删除远端存在但本地不存在的文件
    """
    from huggingface_hub import HfApi
    
    api = HfApi()

    has_large_upload = hasattr(api, "upload_large_folder")
    if has_large_upload:
        large_params = set(inspect.signature(api.upload_large_folder).parameters)
    else:
        large_params = set()

    supports_delete = "delete_patterns" in large_params

    if sync and not supports_delete:
        # 旧版 huggingface_hub 不支持 upload_large_folder 的 delete_patterns
        api.upload_folder(
            repo_id="BorisGuo/pair_touch_13m",
            repo_type="dataset",
            folder_path=str(BASE_DIR),
            ignore_patterns=["__pycache__/**", "*.h5"],
            delete_patterns=["*"],  # 删除远端存在但本地不存在的文件
        )
    else:
        # 普通模式:只上传/更新,不删除
        upload_kwargs = {
            "repo_id": "BorisGuo/pair_touch_13m",
            "repo_type": "dataset",
            "folder_path": str(BASE_DIR),
            "ignore_patterns": ["__pycache__/**", "*.h5"],
        }
        if sync and supports_delete:
            upload_kwargs["delete_patterns"] = ["*"]
        if has_large_upload:
            api.upload_large_folder(**upload_kwargs)
        else:
            api.upload_folder(**upload_kwargs)
    print("上传完成!")


# ============================================================
# 主函数
# ============================================================

def main():
    parser = argparse.ArgumentParser(description="数据集预处理")
    subparsers = parser.add_subparsers(dest='command', help='命令')
    
    # extract
    extract_parser = subparsers.add_parser('extract', help='解析 H5 文件')
    extract_parser.add_argument('--check', action='store_true', help='仅检查结构')
    extract_parser.add_argument('--update', action='store_true', help='仅更新 metadata')
    
    # heatmap
    heatmap_parser = subparsers.add_parser('heatmap', help='生成热力图')
    heatmap_parser.add_argument('--test', action='store_true', help='测试模式')
    heatmap_parser.add_argument('--type', default='all', help='数据类型')
    
    # marker_flow
    flow_parser = subparsers.add_parser('marker_flow', help='生成 xela marker flow 可视化')
    flow_parser.add_argument('--test', action='store_true', help='测试模式')
    flow_parser.add_argument('--type', default='all', 
                             choices=['xela_pose', 'xela_force', 'xela_9dtact', 'pose', 'force', 'all'],
                             help='数据类型')
    
    # video
    video_parser = subparsers.add_parser('video', help='生成视频')
    video_parser.add_argument('--test', action='store_true', help='测试模式')
    video_parser.add_argument('--type', default='all', help='数据类型')
    
    # pack
    pack_parser = subparsers.add_parser('pack', help='打包图像序列为 tar 文件')
    pack_parser.add_argument('--delete', action='store_true', help='打包后删除原始图像')
    
    # unpack
    unpack_parser = subparsers.add_parser('unpack', help='解压 tar 文件中的图像')
    unpack_parser.add_argument('--delete', action='store_true', help='解压后删除 tar 文件')
    
    # clean
    subparsers.add_parser('clean', help='删除所有 PNG 图像,只保留视频')
    
    # upload
    upload_parser = subparsers.add_parser('upload', help='上传到 Hugging Face')
    upload_parser.add_argument('--sync', action='store_true', 
                               help='同步模式:删除远端存在但本地不存在的文件')
    
    # all
    subparsers.add_parser('all', help='完整流程')
    
    args = parser.parse_args()
    
    if args.command == 'extract':
        if args.check:
            check_h5_structure()
        elif args.update:
            update_metadata()
        else:
            extract_all()
    elif args.command == 'heatmap':
        print("生成热力图...")
        generate_heatmaps(args.type, args.test)
        print("\n完成!")
    elif args.command == 'marker_flow':
        print("生成 marker flow...")
        generate_marker_flow(args.type, args.test)
        print("\n完成!")
    elif args.command == 'video':
        print("生成视频...")
        generate_videos(args.type, args.test)
        print("\n完成!")
    elif args.command == 'pack':
        print("打包图像序列...")
        pack_images(delete_originals=args.delete)
    elif args.command == 'unpack':
        print("解压图像...")
        unpack_images(delete_tar=args.delete)
    elif args.command == 'clean':
        print("清理图像文件...")
        clean_images()
    elif args.command == 'upload':
        upload_to_hf(sync=args.sync)
    elif args.command == 'all':
        print("="*60 + "\n完整流程\n" + "="*60)
        print("\n[1/4] 解析 H5 文件...")
        extract_all()
        print("\n[2/4] 生成热力图...")
        generate_heatmaps('all', False)
        print("\n[3/4] 生成视频...")
        generate_videos('all', False)
        print("\n[4/4] 更新 metadata...")
        update_metadata()
        print("\n" + "="*60 + "\n完成!\n" + "="*60)
    else:
        parser.print_help()


if __name__ == "__main__":
    main()