Datasets:
File size: 8,898 Bytes
39cd1fe |
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "f175028c",
"metadata": {},
"source": [
"## The \"Wild Khmer\" Dataset Creation Script"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1ed4f71e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded 10000 images from JSON\n",
"Processing images and labels... This may take a while.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "42c314cacca648d6a0e8b4df6093a9d9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating train split: 0 examples [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 10000/10000 [02:28<00:00, 67.53it/s]\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2b0fef7ef288405c908d8e5ca92348fe",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading dataset shards: 0%| | 0/17 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saving Parquet files...\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bd6d7d400c8f46b5a10501516cbec7b3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Creating parquet from Arrow format: 0%| | 0/92 [00:00<?, ?ba/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "68cb344590af4837bb993bc6e0e1bef0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Creating parquet from Arrow format: 0%| | 0/11 [00:00<?, ?ba/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"DONE β
\n",
"Wild Train file: wild_khmer_train.parquet\n"
]
}
],
"source": [
"import json\n",
"import os\n",
"import pandas as pd\n",
"from PIL import Image\n",
"from tqdm import tqdm\n",
"from datasets import Dataset, Features, Image as datasets_Image, Value\n",
"\n",
"# -------------------------\n",
"# CONFIG\n",
"# -------------------------\n",
"JSON_PATH = \"info.json\" # Your CADT JSON file\n",
"IMAGE_FOLDER = \"images/images\" # Folder with your JPGs\n",
"TRAIN_OUT = \"wild_khmer_train.parquet\"\n",
"TEST_OUT = \"wild_khmer_test.parquet\"\n",
"TEST_SPLIT = 0.1\n",
"\n",
"# -------------------------\n",
"# LOAD DATA\n",
"# -------------------------\n",
"with open(JSON_PATH, 'r', encoding='utf-8') as f:\n",
" via_data = json.load(f)\n",
"print(f\"Loaded {len(via_data)} images from JSON\")\n",
"\n",
"# -------------------------\n",
"# DATA GENERATOR\n",
"# -------------------------\n",
"def generate_examples():\n",
" for key, data in tqdm(via_data.items()):\n",
" filename = data['filename']\n",
" image_path = os.path.join(IMAGE_FOLDER, filename)\n",
"\n",
" if not os.path.exists(image_path):\n",
" continue\n",
"\n",
" # 1. Get Image Dimensions for Normalization\n",
" try:\n",
" with Image.open(image_path) as img:\n",
" width, height = img.size\n",
" # Read raw bytes for embedding\n",
" with open(image_path, \"rb\") as f:\n",
" img_bytes = f.read()\n",
" except Exception as e:\n",
" print(f\"Error loading {filename}: {e}\")\n",
" continue\n",
"\n",
" # 2. Process Regions (Polygons -> Normalized Bounding Boxes)\n",
" regions_list = []\n",
" for region in data['regions']:\n",
" try:\n",
" # Extract coordinates\n",
" xs = region['shape_attributes']['all_points_x']\n",
" ys = region['shape_attributes']['all_points_y']\n",
" label = region['region_attributes']['label']\n",
"\n",
" # Convert Polygon to Bounding Box (ymin, xmin, ymax, xmax)\n",
" xmin, xmax = min(xs), max(xs)\n",
" ymin, ymax = min(ys), max(ys)\n",
"\n",
" # Normalize to 0-1000 scale (Qwen3-VL Standard)\n",
" n_xmin = int((xmin / width) * 1000)\n",
" n_xmax = int((xmax / width) * 1000)\n",
" n_ymin = int((ymin / height) * 1000)\n",
" n_ymax = int((ymax / height) * 1000)\n",
"\n",
" # Format as a dictionary for the grounding task\n",
" regions_list.append({\n",
" \"bbox_2d\": [n_ymin, n_xmin, n_ymax, n_xmax],\n",
" \"label\": label\n",
" })\n",
" except KeyError:\n",
" continue # Skip regions without labels or points\n",
"\n",
" # 3. Create the final text label (JSON string)\n",
" # This will be processed by your convert_to_conversation function\n",
" grounding_json = json.dumps({\"regions\": regions_list}, ensure_ascii=False)\n",
"\n",
" yield {\n",
" \"image\": img_bytes,\n",
" \"text\": grounding_json\n",
" }\n",
"\n",
"# -------------------------\n",
"# DATASET FEATURES\n",
"# -------------------------\n",
"features = Features({\n",
" \"image\": datasets_Image(), # embedded image bytes\n",
" \"text\": Value(\"string\"), # JSON string of boxes and text\n",
"})\n",
"\n",
"# -------------------------\n",
"# CREATE & SAVE\n",
"# -------------------------\n",
"print(\"Processing images and labels... This may take a while.\")\n",
"ds = Dataset.from_generator(generate_examples, features=features)\n",
"\n",
"# Shuffle and split\n",
"ds = ds.train_test_split(test_size=TEST_SPLIT)\n",
"\n",
"print(\"Saving Parquet files...\")\n",
"ds[\"train\"].to_parquet(TRAIN_OUT)\n",
"ds[\"test\"].to_parquet(TEST_OUT)\n",
"\n",
"print(\"DONE β
\")\n",
"print(f\"Wild Train file: {TRAIN_OUT}\")"
]
},
{
"cell_type": "markdown",
"id": "b1f80720",
"metadata": {},
"source": [
"## Open The Image Dataset For Checking"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "57395385",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d2da1e3e954a45fdb05b47c8d38e66b1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading dataset shards: 0%| | 0/17 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'PIL.JpegImagePlugin.JpegImageFile'>\n",
"<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=960x1280 at 0x219E9F9BEC0>\n"
]
}
],
"source": [
"from datasets import Dataset\n",
"ds = Dataset.from_parquet(\"wild_khmer_train.parquet\")\n",
"sample = ds[1][\"image\"]\n",
"print(type(sample))\n",
"print(sample)\n",
"sample.show()\n"
]
},
{
"cell_type": "markdown",
"id": "2307a106",
"metadata": {},
"source": [
"## DUMMY DATASET ROW"
]
},
{
"cell_type": "markdown",
"id": "aa809986",
"metadata": {},
"source": [
"```python\n",
"{\n",
" 'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1200x800>,\n",
" 'text': '{\"regions\": [{\"bbox_2d\": [150, 200, 300, 800], \"label\": \"α’αΆα αΆαααααΆααα·αααααΆα\"}, {\"bbox_2d\": [850, 400, 920, 600], \"label\": \"012 345 678\"}]}'\n",
"}\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|