Create set_images.py
Browse files- set_images.py +42 -0
set_images.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from datasets import Dataset, Features, Image, Value
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class MyDataset(datasets.GeneratorBasedBuilder):
|
| 7 |
+
BUILDER_CONFIGS = [{"name": "default", "version": datasets.Version("0.1.0")}]
|
| 8 |
+
|
| 9 |
+
def _info(self):
|
| 10 |
+
return datasets.DatasetInfo(
|
| 11 |
+
features=Features({
|
| 12 |
+
"source_language": Value("string"),
|
| 13 |
+
"target_language": Value("string"),
|
| 14 |
+
"source_image_file_name": Image(),
|
| 15 |
+
"target_image_file_name": Image(),
|
| 16 |
+
"texts": Value("string")
|
| 17 |
+
})
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
def _split_generators(self, dl_manager):
|
| 21 |
+
return [
|
| 22 |
+
datasets.SplitGenerator(
|
| 23 |
+
name=datasets.Split.DEV,
|
| 24 |
+
gen_kwargs={"filepath": "data/dev/metadata.jsonl", "img_dir": "data/dev"},
|
| 25 |
+
),
|
| 26 |
+
datasets.SplitGenerator(
|
| 27 |
+
name=datasets.Split.TEST,
|
| 28 |
+
gen_kwargs={"filepath": "data/test/metadata.jsonl", "img_dir": "data/test"},
|
| 29 |
+
),
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
def _generate_examples(self, filepath, img_dir):
|
| 33 |
+
img_dir = Path(img_dir)
|
| 34 |
+
with open(filepath, encoding="utf-8") as f:
|
| 35 |
+
for line_num, line in enumerate(f):
|
| 36 |
+
row = json.loads(line)
|
| 37 |
+
img_path = img_dir / row["source_image_file_name"] # Assumes "image": "relative/path.jpg" in JSONL; use full relative if needed (e.g., "images/file.jpg")
|
| 38 |
+
img_path_target = img_dir / row["target_image_file_name"]
|
| 39 |
+
if img_path.exists():
|
| 40 |
+
yield line_num, {"source_image_file_name": str(img_path), **row} # Path as str for Image() decoding
|
| 41 |
+
if img_path_target.exists():
|
| 42 |
+
yield line_num, {"target_image_file_name": str(img_path), **row} # Path as str for Image() decoding
|