Datasets:
Add load_dataset support with coco/yolo/products configs
Browse files- README.md +80 -16
- norwegian_grocery.py +284 -0
README.md
CHANGED
|
@@ -20,24 +20,94 @@ size_categories:
|
|
| 20 |
|
| 21 |
Object detection dataset for Norwegian grocery store shelf images, from the NorgesGruppen Data competition.
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
## Dataset Summary
|
| 24 |
|
| 25 |
-
- **248 shelf images** from Norwegian grocery stores (
|
| 26 |
- **22,731 bounding box annotations** in COCO format
|
| 27 |
- **356 product categories** (IDs 0-355, where 355 = `unknown_product`)
|
| 28 |
-
- **327
|
|
|
|
| 29 |
|
| 30 |
## Structure
|
| 31 |
|
| 32 |
```
|
|
|
|
| 33 |
train/
|
| 34 |
-
annotations.json
|
| 35 |
-
images/
|
| 36 |
NM_NGD_product_images/
|
| 37 |
-
metadata.json
|
| 38 |
-
{barcode}/
|
| 39 |
scripts/
|
| 40 |
-
coco_to_yolo.py
|
| 41 |
```
|
| 42 |
|
| 43 |
## Annotation Format
|
|
@@ -61,9 +131,9 @@ COCO format (`annotations.json`):
|
|
| 61 |
|
| 62 |
`bbox` is `[x, y, width, height]` in pixels (COCO format).
|
| 63 |
|
| 64 |
-
##
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
```bash
|
| 69 |
python scripts/coco_to_yolo.py \
|
|
@@ -73,16 +143,10 @@ python scripts/coco_to_yolo.py \
|
|
| 73 |
--val-ratio 0.15
|
| 74 |
```
|
| 75 |
|
| 76 |
-
This creates
|
| 77 |
-
- `yolo/images/{train,val}/` - symlinked images
|
| 78 |
-
- `yolo/labels/{train,val}/` - YOLO `.txt` label files
|
| 79 |
-
- `yolo/category_names.json` - category ID to name mapping
|
| 80 |
-
- `configs/dataset.yaml` - YOLOv8 dataset config (with absolute paths for your machine)
|
| 81 |
|
| 82 |
## Usage with the Detection Project in https://github.com/kuben-labs/nmai
|
| 83 |
|
| 84 |
-
Clone this dataset into the detection project:
|
| 85 |
-
|
| 86 |
```bash
|
| 87 |
cd detection/
|
| 88 |
make data # clones from HuggingFace
|
|
|
|
| 20 |
|
| 21 |
Object detection dataset for Norwegian grocery store shelf images, from the NorgesGruppen Data competition.
|
| 22 |
|
| 23 |
+
## Quick Start
|
| 24 |
+
|
| 25 |
+
```python
|
| 26 |
+
from datasets import load_dataset
|
| 27 |
+
|
| 28 |
+
# COCO format (default) - raw competition annotations
|
| 29 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", trust_remote_code=True)
|
| 30 |
+
|
| 31 |
+
# YOLO format - auto-converted on load, no scripts needed
|
| 32 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", name="yolo", trust_remote_code=True)
|
| 33 |
+
|
| 34 |
+
# Product reference images - multi-angle shots per product
|
| 35 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", name="products", trust_remote_code=True)
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Configurations
|
| 39 |
+
|
| 40 |
+
| Config | Default | Description | Splits |
|
| 41 |
+
|--------|---------|-------------|--------|
|
| 42 |
+
| `coco` | Yes | Raw COCO annotations with full metadata | train, validation |
|
| 43 |
+
| `yolo` | | YOLO normalized `[cx, cy, w, h]` (converted on-the-fly) | train, validation |
|
| 44 |
+
| `products` | | Product reference images with metadata | train |
|
| 45 |
+
|
| 46 |
+
### COCO Config
|
| 47 |
+
|
| 48 |
+
Each example contains a shelf image and all its COCO-format annotations:
|
| 49 |
+
|
| 50 |
+
```python
|
| 51 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", trust_remote_code=True)
|
| 52 |
+
example = ds["train"][0]
|
| 53 |
+
|
| 54 |
+
example["image"] # PIL Image
|
| 55 |
+
example["image_id"] # int
|
| 56 |
+
example["width"] # image width in pixels
|
| 57 |
+
example["height"] # image height in pixels
|
| 58 |
+
example["annotations"] # JSON string of COCO annotation list
|
| 59 |
+
# Each annotation: {"id", "category_id", "bbox": [x, y, w, h], "area", "product_code", ...}
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
### YOLO Config
|
| 63 |
+
|
| 64 |
+
Same images, but bounding boxes are auto-converted to YOLO normalized center format:
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", name="yolo", trust_remote_code=True)
|
| 68 |
+
example = ds["train"][0]
|
| 69 |
+
|
| 70 |
+
example["image"] # PIL Image
|
| 71 |
+
example["image_id"] # int
|
| 72 |
+
example["labels"] # [int, ...] - category IDs
|
| 73 |
+
example["bboxes"] # [[cx, cy, w, h], ...] - normalized to [0, 1]
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
### Products Config
|
| 77 |
+
|
| 78 |
+
Reference images for 327 products, organized by product code:
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", name="products", trust_remote_code=True)
|
| 82 |
+
example = ds["train"][0]
|
| 83 |
+
|
| 84 |
+
example["image"] # PIL Image (one angle)
|
| 85 |
+
example["product_code"] # barcode string
|
| 86 |
+
example["product_name"] # e.g. "EVERGOOD CLASSIC KOKMALT 250G"
|
| 87 |
+
example["image_type"] # "main", "front", "back", "left", "right", "top", "bottom"
|
| 88 |
+
example["annotation_count"] # how often this product appears in shelf images
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
## Dataset Summary
|
| 92 |
|
| 93 |
+
- **248 shelf images** from Norwegian grocery stores (Egg, Frokost, Knekkebrod, Varmedrikker)
|
| 94 |
- **22,731 bounding box annotations** in COCO format
|
| 95 |
- **356 product categories** (IDs 0-355, where 355 = `unknown_product`)
|
| 96 |
+
- **327 products** with multi-angle reference photos (1,582 images total)
|
| 97 |
+
- Train/val split: 211 / 37 images (85/15, seed=42)
|
| 98 |
|
| 99 |
## Structure
|
| 100 |
|
| 101 |
```
|
| 102 |
+
norwegian_grocery.py # Custom loading script (auto-discovered by HF)
|
| 103 |
train/
|
| 104 |
+
annotations.json # COCO-format annotations
|
| 105 |
+
images/ # 248 shelf images (.jpg/.jpeg)
|
| 106 |
NM_NGD_product_images/
|
| 107 |
+
metadata.json # Product names, barcodes, annotation counts
|
| 108 |
+
{barcode}/ # Product reference images per barcode
|
| 109 |
scripts/
|
| 110 |
+
coco_to_yolo.py # Convert COCO to YOLO format with train/val split
|
| 111 |
```
|
| 112 |
|
| 113 |
## Annotation Format
|
|
|
|
| 131 |
|
| 132 |
`bbox` is `[x, y, width, height]` in pixels (COCO format).
|
| 133 |
|
| 134 |
+
## For Local YOLO Training
|
| 135 |
|
| 136 |
+
If you need on-disk YOLO files for `ultralytics`, the repo includes `scripts/coco_to_yolo.py`:
|
| 137 |
|
| 138 |
```bash
|
| 139 |
python scripts/coco_to_yolo.py \
|
|
|
|
| 143 |
--val-ratio 0.15
|
| 144 |
```
|
| 145 |
|
| 146 |
+
This creates `yolo/images/{train,val}/`, `yolo/labels/{train,val}/`, and `configs/dataset.yaml`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
## Usage with the Detection Project in https://github.com/kuben-labs/nmai
|
| 149 |
|
|
|
|
|
|
|
| 150 |
```bash
|
| 151 |
cd detection/
|
| 152 |
make data # clones from HuggingFace
|
norwegian_grocery.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""NorgesGruppen Grocery Detection Dataset — valiantlynxz/norwegian-grocery
|
| 2 |
+
|
| 3 |
+
Custom loading script with three configs:
|
| 4 |
+
coco (default) — Raw COCO annotations + shelf images, train/val split
|
| 5 |
+
yolo — On-the-fly COCO-to-YOLO conversion, normalized [cx, cy, w, h]
|
| 6 |
+
products — Multi-angle product reference images with metadata
|
| 7 |
+
|
| 8 |
+
Usage:
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
|
| 11 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", trust_remote_code=True)
|
| 12 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", name="yolo", trust_remote_code=True)
|
| 13 |
+
ds = load_dataset("valiantlynxz/norwegian-grocery", name="products", trust_remote_code=True)
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import json
|
| 17 |
+
import os
|
| 18 |
+
import random
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
_DESCRIPTION = (
|
| 24 |
+
"Object detection dataset for Norwegian grocery store shelf images. "
|
| 25 |
+
"248 shelf images with 22,731 bounding box annotations across 356 product categories, "
|
| 26 |
+
"plus 327 products with multi-angle reference photos."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
_HOMEPAGE = "https://huggingface.co/datasets/valiantlynxz/norwegian-grocery"
|
| 30 |
+
|
| 31 |
+
_LICENSE = "CC-BY-NC-4.0"
|
| 32 |
+
|
| 33 |
+
_VAL_RATIO = 0.15
|
| 34 |
+
_SPLIT_SEED = 42
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class NorwegianGroceryConfig(datasets.BuilderConfig):
|
| 38 |
+
"""BuilderConfig for NorgesGruppen Grocery Detection."""
|
| 39 |
+
|
| 40 |
+
def __init__(self, format="coco", **kwargs):
|
| 41 |
+
super().__init__(**kwargs)
|
| 42 |
+
self.format = format
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class NorwegianGrocery(datasets.GeneratorBasedBuilder):
|
| 46 |
+
"""NorgesGruppen grocery shelf detection dataset."""
|
| 47 |
+
|
| 48 |
+
VERSION = datasets.Version("1.0.0")
|
| 49 |
+
|
| 50 |
+
BUILDER_CONFIG_CLASS = NorwegianGroceryConfig
|
| 51 |
+
|
| 52 |
+
BUILDER_CONFIGS = [
|
| 53 |
+
NorwegianGroceryConfig(
|
| 54 |
+
name="coco",
|
| 55 |
+
version=VERSION,
|
| 56 |
+
description="Raw COCO-format annotations with full metadata",
|
| 57 |
+
format="coco",
|
| 58 |
+
),
|
| 59 |
+
NorwegianGroceryConfig(
|
| 60 |
+
name="yolo",
|
| 61 |
+
version=VERSION,
|
| 62 |
+
description="YOLO format: normalized [cx, cy, w, h], converted on-the-fly",
|
| 63 |
+
format="yolo",
|
| 64 |
+
),
|
| 65 |
+
NorwegianGroceryConfig(
|
| 66 |
+
name="products",
|
| 67 |
+
version=VERSION,
|
| 68 |
+
description="Product reference images with metadata",
|
| 69 |
+
format="products",
|
| 70 |
+
),
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
DEFAULT_CONFIG_NAME = "coco"
|
| 74 |
+
|
| 75 |
+
def _info(self):
|
| 76 |
+
if self.config.format == "coco":
|
| 77 |
+
features = datasets.Features(
|
| 78 |
+
{
|
| 79 |
+
"image": datasets.Image(),
|
| 80 |
+
"image_id": datasets.Value("int64"),
|
| 81 |
+
"file_name": datasets.Value("string"),
|
| 82 |
+
"width": datasets.Value("int32"),
|
| 83 |
+
"height": datasets.Value("int32"),
|
| 84 |
+
"annotations": datasets.Value("string"),
|
| 85 |
+
}
|
| 86 |
+
)
|
| 87 |
+
elif self.config.format == "yolo":
|
| 88 |
+
features = datasets.Features(
|
| 89 |
+
{
|
| 90 |
+
"image": datasets.Image(),
|
| 91 |
+
"image_id": datasets.Value("int64"),
|
| 92 |
+
"file_name": datasets.Value("string"),
|
| 93 |
+
"width": datasets.Value("int32"),
|
| 94 |
+
"height": datasets.Value("int32"),
|
| 95 |
+
"labels": datasets.Sequence(datasets.Value("int32")),
|
| 96 |
+
"bboxes": datasets.Sequence(
|
| 97 |
+
datasets.Sequence(datasets.Value("float32"), length=4)
|
| 98 |
+
),
|
| 99 |
+
}
|
| 100 |
+
)
|
| 101 |
+
else: # products
|
| 102 |
+
features = datasets.Features(
|
| 103 |
+
{
|
| 104 |
+
"image": datasets.Image(),
|
| 105 |
+
"product_code": datasets.Value("string"),
|
| 106 |
+
"product_name": datasets.Value("string"),
|
| 107 |
+
"image_type": datasets.Value("string"),
|
| 108 |
+
"annotation_count": datasets.Value("int32"),
|
| 109 |
+
}
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
return datasets.DatasetInfo(
|
| 113 |
+
description=_DESCRIPTION,
|
| 114 |
+
features=features,
|
| 115 |
+
homepage=_HOMEPAGE,
|
| 116 |
+
license=_LICENSE,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
def _split_generators(self, dl_manager):
|
| 120 |
+
data_dir = os.path.dirname(os.path.abspath(__file__))
|
| 121 |
+
|
| 122 |
+
if self.config.format in ("coco", "yolo"):
|
| 123 |
+
ann_file = os.path.join(data_dir, "train", "annotations.json")
|
| 124 |
+
image_dir = os.path.join(data_dir, "train", "images")
|
| 125 |
+
|
| 126 |
+
# Compute train/val split (deterministic)
|
| 127 |
+
with open(ann_file) as f:
|
| 128 |
+
coco = json.load(f)
|
| 129 |
+
image_ids = sorted(img["id"] for img in coco["images"])
|
| 130 |
+
rng = random.Random(_SPLIT_SEED)
|
| 131 |
+
rng.shuffle(image_ids)
|
| 132 |
+
val_count = max(1, int(len(image_ids) * _VAL_RATIO))
|
| 133 |
+
val_ids = set(image_ids[:val_count])
|
| 134 |
+
train_ids = set(image_ids[val_count:])
|
| 135 |
+
|
| 136 |
+
return [
|
| 137 |
+
datasets.SplitGenerator(
|
| 138 |
+
name=datasets.Split.TRAIN,
|
| 139 |
+
gen_kwargs={
|
| 140 |
+
"ann_file": ann_file,
|
| 141 |
+
"image_dir": image_dir,
|
| 142 |
+
"split_ids": train_ids,
|
| 143 |
+
},
|
| 144 |
+
),
|
| 145 |
+
datasets.SplitGenerator(
|
| 146 |
+
name=datasets.Split.VALIDATION,
|
| 147 |
+
gen_kwargs={
|
| 148 |
+
"ann_file": ann_file,
|
| 149 |
+
"image_dir": image_dir,
|
| 150 |
+
"split_ids": val_ids,
|
| 151 |
+
},
|
| 152 |
+
),
|
| 153 |
+
]
|
| 154 |
+
else: # products
|
| 155 |
+
products_dir = os.path.join(data_dir, "NM_NGD_product_images")
|
| 156 |
+
metadata_file = os.path.join(products_dir, "metadata.json")
|
| 157 |
+
return [
|
| 158 |
+
datasets.SplitGenerator(
|
| 159 |
+
name=datasets.Split.TRAIN,
|
| 160 |
+
gen_kwargs={
|
| 161 |
+
"products_dir": products_dir,
|
| 162 |
+
"metadata_file": metadata_file,
|
| 163 |
+
},
|
| 164 |
+
),
|
| 165 |
+
]
|
| 166 |
+
|
| 167 |
+
def _generate_examples(self, **kwargs):
|
| 168 |
+
if self.config.format in ("coco", "yolo"):
|
| 169 |
+
yield from self._generate_detection_examples(**kwargs)
|
| 170 |
+
else:
|
| 171 |
+
yield from self._generate_product_examples(**kwargs)
|
| 172 |
+
|
| 173 |
+
def _generate_detection_examples(self, ann_file, image_dir, split_ids):
|
| 174 |
+
"""Generate examples for coco and yolo configs."""
|
| 175 |
+
with open(ann_file) as f:
|
| 176 |
+
coco = json.load(f)
|
| 177 |
+
|
| 178 |
+
id_to_img = {img["id"]: img for img in coco["images"]}
|
| 179 |
+
|
| 180 |
+
img_to_anns = {}
|
| 181 |
+
for ann in coco["annotations"]:
|
| 182 |
+
img_to_anns.setdefault(ann["image_id"], []).append(ann)
|
| 183 |
+
|
| 184 |
+
idx = 0
|
| 185 |
+
for img_id in sorted(split_ids):
|
| 186 |
+
img_info = id_to_img.get(img_id)
|
| 187 |
+
if img_info is None:
|
| 188 |
+
continue
|
| 189 |
+
|
| 190 |
+
file_name = img_info["file_name"]
|
| 191 |
+
img_path = os.path.join(image_dir, file_name)
|
| 192 |
+
if not os.path.exists(img_path):
|
| 193 |
+
continue
|
| 194 |
+
|
| 195 |
+
w = img_info["width"]
|
| 196 |
+
h = img_info["height"]
|
| 197 |
+
anns = img_to_anns.get(img_id, [])
|
| 198 |
+
|
| 199 |
+
if self.config.format == "coco":
|
| 200 |
+
yield (
|
| 201 |
+
idx,
|
| 202 |
+
{
|
| 203 |
+
"image": img_path,
|
| 204 |
+
"image_id": img_id,
|
| 205 |
+
"file_name": file_name,
|
| 206 |
+
"width": w,
|
| 207 |
+
"height": h,
|
| 208 |
+
"annotations": json.dumps(anns),
|
| 209 |
+
},
|
| 210 |
+
)
|
| 211 |
+
else: # yolo
|
| 212 |
+
labels = []
|
| 213 |
+
bboxes = []
|
| 214 |
+
for ann in anns:
|
| 215 |
+
bbox = ann["bbox"] # COCO: [x, y, w, h] top-left
|
| 216 |
+
bw, bh = bbox[2], bbox[3]
|
| 217 |
+
|
| 218 |
+
# Skip zero-area boxes
|
| 219 |
+
if bw <= 0 or bh <= 0:
|
| 220 |
+
continue
|
| 221 |
+
|
| 222 |
+
# Convert to YOLO normalized center format
|
| 223 |
+
cx = min(1.0, max(0.0, (bbox[0] + bw / 2) / w))
|
| 224 |
+
cy = min(1.0, max(0.0, (bbox[1] + bh / 2) / h))
|
| 225 |
+
nw = min(1.0, max(0.0, bw / w))
|
| 226 |
+
nh = min(1.0, max(0.0, bh / h))
|
| 227 |
+
|
| 228 |
+
# Skip degenerate boxes
|
| 229 |
+
if nw <= 0 or nh <= 0:
|
| 230 |
+
continue
|
| 231 |
+
|
| 232 |
+
labels.append(ann["category_id"])
|
| 233 |
+
bboxes.append([cx, cy, nw, nh])
|
| 234 |
+
|
| 235 |
+
yield (
|
| 236 |
+
idx,
|
| 237 |
+
{
|
| 238 |
+
"image": img_path,
|
| 239 |
+
"image_id": img_id,
|
| 240 |
+
"file_name": file_name,
|
| 241 |
+
"width": w,
|
| 242 |
+
"height": h,
|
| 243 |
+
"labels": labels,
|
| 244 |
+
"bboxes": bboxes,
|
| 245 |
+
},
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
idx += 1
|
| 249 |
+
|
| 250 |
+
def _generate_product_examples(self, products_dir, metadata_file):
|
| 251 |
+
"""Generate examples for products config."""
|
| 252 |
+
with open(metadata_file) as f:
|
| 253 |
+
meta = json.load(f)
|
| 254 |
+
|
| 255 |
+
idx = 0
|
| 256 |
+
for product in meta["products"]:
|
| 257 |
+
if not product.get("has_images", False):
|
| 258 |
+
continue
|
| 259 |
+
|
| 260 |
+
product_code = product["product_code"]
|
| 261 |
+
product_name = product["product_name"]
|
| 262 |
+
annotation_count = product.get("annotation_count", 0)
|
| 263 |
+
product_dir = os.path.join(products_dir, product_code)
|
| 264 |
+
|
| 265 |
+
if not os.path.isdir(product_dir):
|
| 266 |
+
continue
|
| 267 |
+
|
| 268 |
+
for image_type in product.get("image_types", []):
|
| 269 |
+
# Try common extensions
|
| 270 |
+
for ext in (".jpg", ".jpeg", ".png"):
|
| 271 |
+
img_path = os.path.join(product_dir, image_type + ext)
|
| 272 |
+
if os.path.exists(img_path):
|
| 273 |
+
yield (
|
| 274 |
+
idx,
|
| 275 |
+
{
|
| 276 |
+
"image": img_path,
|
| 277 |
+
"product_code": product_code,
|
| 278 |
+
"product_name": product_name,
|
| 279 |
+
"image_type": image_type,
|
| 280 |
+
"annotation_count": annotation_count,
|
| 281 |
+
},
|
| 282 |
+
)
|
| 283 |
+
idx += 1
|
| 284 |
+
break
|