Datasets:
File size: 2,431 Bytes
c83afe9 5e1ef3a c83afe9 5e1ef3a c83afe9 5e1ef3a c83afe9 464a7d8 c83afe9 c0aabe6 c83afe9 5e1ef3a c83afe9 5e1ef3a 464a7d8 c0aabe6 c83afe9 c0aabe6 c83afe9 464a7d8 c83afe9 5e1ef3a c83afe9 | 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 | # import os
# import pandas as pd
# from datasets import Dataset, Image, Features, Value
# df = pd.read_json("data/test.jsonl", lines=True)
# def get_full_path(file_name):
# return os.path.join(os.getcwd(), "data", file_name)
# df['image_path'] = df['file_name'].apply(get_full_path)
# # 3. binary
# def __force_embed_image(example):
# with open(example["image_path"], "rb") as f:
# image_bytes = f.read()
# return {"image": {"bytes": image_bytes}}
# ds = Dataset.from_pandas(df)
# ds = ds.map(__force_embed_image, remove_columns=["image_path"], num_proc=1)
# ds = ds.cast_column("image", Image())
# ds.to_parquet("violin-test.parquet")
import os
import pandas as pd
from datasets import Dataset, Image, Features, Value
# 1. Load Data
df = pd.read_json("data/test.jsonl", lines=True)
# 2. Embedding Function
def __force_embed_images(example):
image_cols = ["ground_truth", "image1_path", "image2_path"]
data_root = os.path.abspath("data")
for col in image_cols:
val = example.get(col)
if val and isinstance(val, str):
full_path = os.path.join(data_root, val.replace('\\', '/'))
if os.path.exists(full_path):
with open(full_path, "rb") as f:
img_bytes = f.read()
example[col] = {"bytes": img_bytes}
else:
example[col] = None
else:
example[col] = None
return example
# 3. Define the TARGET Features
target_features = Features({
"id": Value("string"),
"prompt": Value("string"),
"task": Value("int64"),
"ground_truth": Image(),
"color_1": Value("string"),
"color_2": Value("string"),
"hex_val_1": Value("string"),
"hex_val_2": Value("string"),
"direction": Value("string"),
"shape": Value("string"),
"position": Value("string"),
"size_ratio": Value("string"),
"center_x": Value("string"),
"center_y": Value("string"),
"mask_type": Value("string"),
"image_id": Value("string"),
"image1_path": Image(),
"image2_path": Image(),
})
# 4. Create Dataset without initial features
ds = Dataset.from_pandas(df)
# 5. EXECUTE MAP WITH FEATURES
ds = ds.map(
__force_embed_images,
features=target_features,
num_proc=1
)
# 6. Save to Parquet
ds.to_parquet("violin-test.parquet")
print("Success! All images manually embedded into parquet.") |