File size: 3,251 Bytes
2940710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import argparse


def main():
    parser = argparse.ArgumentParser(
        description="Generate metadata.jsonl for DDPD dataset"
    )
    parser.add_argument(
        "--train_dir", default="dd_dp_dataset_png/train_c", help="Train directory of the dataset"
    )
    parser.add_argument(
        "--val_dir", default="dd_dp_dataset_png/val_c", help="Validation directory of the dataset"
    )
    parser.add_argument(
        "--test_dir", default="dd_dp_dataset_png/test_c", help="Test directory of the dataset"
    )
    parser.add_argument(
        "--source_dir_name", default="source", help="Source directory name of the dataset"
    )
    parser.add_argument(
        "--target_dir_name", default="target", help="Target directory name of the dataset"
    )
    parser.add_argument(
        "--output_dir",
        default="config/combined",
        help="Output directory",
    )

    args = parser.parse_args()

    train_dir = args.train_dir
    val_dir = args.val_dir
    test_dir = args.test_dir
    output_dir = args.output_dir

    source_dir_name = args.source_dir_name
    target_dir_name = args.target_dir_name

    # Ensure output directory exists
    os.makedirs(output_dir, exist_ok=True)

    types = ['train', 'val', 'test']
    dirs = [train_dir, val_dir, test_dir]
    for type_, dir_path in zip(types, dirs):
        if os.path.exists(dir_path):
            source_dir = os.path.join(dir_path, source_dir_name)
            target_dir = os.path.join(dir_path, target_dir_name)
            if os.path.exists(source_dir):
                output_file = os.path.join(output_dir, type_, 'metadata.jsonl')
                os.makedirs(os.path.dirname(output_file), exist_ok=True)
                rel_output_dir = os.path.dirname(output_file)
                with open(output_file, "w") as f:
                    source_files = sorted([f for f in os.listdir(source_dir) if f.lower().endswith('.png')])
                    target_files = []
                    if os.path.exists(target_dir):
                        target_files = sorted([f for f in os.listdir(target_dir) if f.lower().endswith('.png')])
                    min_len = min(len(source_files), len(target_files)) if target_files else len(source_files)
                    for i in range(min_len):
                        source_path = os.path.join(source_dir, source_files[i])
                        rel_source = os.path.relpath(source_path, rel_output_dir)
                        entry = {'source_file_name': rel_source}
                        if target_files:
                            target_path = os.path.join(target_dir, target_files[i])
                            rel_target = os.path.relpath(target_path, rel_output_dir)
                            entry['target_file_name'] = rel_target
                        f.write(json.dumps(entry) + '\n')
                    for i in range(min_len, len(source_files)):
                        source_path = os.path.join(source_dir, source_files[i])
                        rel_source = os.path.relpath(source_path, rel_output_dir)
                        entry = {'source_file_name': rel_source}
                        f.write(json.dumps(entry) + '\n')


if __name__ == "__main__":
    main()