SynLayers commited on
Commit
83bce40
·
verified ·
1 Parent(s): ef463a8

Upload infer/convert_vlm_jsonl.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. infer/convert_vlm_jsonl.py +115 -0
infer/convert_vlm_jsonl.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import json
3
+ import shutil
4
+ from pathlib import Path
5
+
6
+
7
+ def resolve_image_path(image_value: str, image_dir: str | None) -> Path | None:
8
+ if not image_value:
9
+ return None
10
+
11
+ path = Path(image_value)
12
+ if path.is_absolute():
13
+ return path
14
+
15
+ if image_dir:
16
+ candidate = Path(image_dir) / path
17
+ if candidate.exists():
18
+ return candidate
19
+
20
+ return path
21
+
22
+
23
+ def build_layers(bboxes: list) -> list:
24
+ layers = []
25
+ for i, bbox in enumerate(bboxes):
26
+ if not isinstance(bbox, (list, tuple)) or len(bbox) < 4:
27
+ continue
28
+
29
+ x0, y0, x1, y1 = [int(float(value)) for value in bbox[:4]]
30
+ x0, x1 = min(x0, x1), max(x0, x1)
31
+ y0, y1 = min(y0, y1), max(y0, y1)
32
+ layers.append({
33
+ "layer_idx": i,
34
+ "box": [x0, y0, x1, y1],
35
+ "width_dst": x1 - x0,
36
+ "height_dst": y1 - y0,
37
+ })
38
+ return layers
39
+
40
+
41
+ def convert(
42
+ input_path: str,
43
+ output_path: str,
44
+ canvas_size: int = 1024,
45
+ image_dir: str | None = None,
46
+ materialize_data_dir: str | None = None,
47
+ ):
48
+ converted_count = 0
49
+ materialize_root = Path(materialize_data_dir) if materialize_data_dir else None
50
+
51
+ with open(input_path, "r", encoding="utf-8") as fin, \
52
+ open(output_path, "w", encoding="utf-8") as fout:
53
+ for line in fin:
54
+ line = line.strip()
55
+ if not line:
56
+ continue
57
+ vlm = json.loads(line)
58
+
59
+ sample_name = (
60
+ vlm.get("sample_or_stem")
61
+ or vlm.get("sample_dir")
62
+ or Path(vlm.get("image", f"sample_{converted_count:06d}")).stem
63
+ )
64
+ image_path = resolve_image_path(vlm.get("image", ""), image_dir)
65
+ layers = build_layers(vlm.get("bboxes", []))
66
+
67
+ sample_dir = sample_name
68
+ blend_path = str(image_path) if image_path else ""
69
+
70
+ if materialize_root and image_path and image_path.exists():
71
+ sample_path = materialize_root / sample_name
72
+ sample_path.mkdir(parents=True, exist_ok=True)
73
+ whole_image_path = sample_path / "whole_image.png"
74
+ shutil.copyfile(image_path, whole_image_path)
75
+ sample_dir = sample_name
76
+ blend_path = str(whole_image_path)
77
+
78
+ record = {
79
+ "sample_dir": sample_dir,
80
+ "whole_caption": vlm.get("whole_caption", ""),
81
+ "layer_count": len(layers),
82
+ "width": canvas_size,
83
+ "height": canvas_size,
84
+ "layers": layers,
85
+ }
86
+ if blend_path:
87
+ # prism_infer.py falls back to blend_path when sample_dir/whole_image.png is absent.
88
+ record["blend_path"] = blend_path
89
+
90
+ fout.write(json.dumps(record, ensure_ascii=False) + "\n")
91
+ converted_count += 1
92
+
93
+ print(f"Converted {converted_count} samples: {input_path} -> {output_path}")
94
+
95
+
96
+ if __name__ == "__main__":
97
+ parser = argparse.ArgumentParser(description="Convert VLM JSONL to inference-compatible format")
98
+ parser.add_argument("--input", "-i", type=str, required=True)
99
+ parser.add_argument("--output", "-o", type=str, required=True)
100
+ parser.add_argument("--canvas_size", type=int, default=1024)
101
+ parser.add_argument("--image_dir", type=str, default=None)
102
+ parser.add_argument(
103
+ "--materialize_data_dir",
104
+ type=str,
105
+ default=None,
106
+ help="Optional output data dir. Copies each VLM image to sample_dir/whole_image.png for infer.py.",
107
+ )
108
+ args = parser.parse_args()
109
+ convert(
110
+ args.input,
111
+ args.output,
112
+ args.canvas_size,
113
+ image_dir=args.image_dir,
114
+ materialize_data_dir=args.materialize_data_dir,
115
+ )