Upload example.py
Browse files- example.py +59 -0
example.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
from PIL import ImageDraw
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
REPO_ID = "Yannik019/llm_pack_detection"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def draw_first_item_location(row: dict):
|
| 13 |
+
image = row["image"].copy()
|
| 14 |
+
first_label = row["labels"][0]
|
| 15 |
+
x = row["annotation_x"][0]
|
| 16 |
+
y = row["annotation_y"][0]
|
| 17 |
+
|
| 18 |
+
draw = ImageDraw.Draw(image)
|
| 19 |
+
radius = 18
|
| 20 |
+
draw.ellipse((x - radius, y - radius, x + radius, y + radius), outline="red", width=6)
|
| 21 |
+
draw.line((x - 28, y, x + 28, y), fill="red", width=4)
|
| 22 |
+
draw.line((x, y - 28, x, y + 28), fill="red", width=4)
|
| 23 |
+
draw.text((x + 24, y - 32), first_label, fill="red")
|
| 24 |
+
|
| 25 |
+
return image, first_label, x, y
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def summarize_row(row: dict, index: int) -> None:
|
| 29 |
+
image, first_label, first_x, first_y = draw_first_item_location(row)
|
| 30 |
+
image_size = getattr(image, "size", None)
|
| 31 |
+
labels = ", ".join(row["labels"])
|
| 32 |
+
|
| 33 |
+
print(f"Row {index}")
|
| 34 |
+
print(f" caption: {row['caption']}")
|
| 35 |
+
print(f" bucket: {row['bucket']}")
|
| 36 |
+
print(f" sample_id: {row['sample_id']}")
|
| 37 |
+
print(f" annotation_count: {row['annotation_count']}")
|
| 38 |
+
print(f" labels: {labels}")
|
| 39 |
+
print(f" first_item: {first_label} @ ({first_x}, {first_y})")
|
| 40 |
+
print(f" image_type: {type(image).__name__}")
|
| 41 |
+
print(f" image_size: {image_size}")
|
| 42 |
+
image.show()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main() -> int:
|
| 46 |
+
dataset = load_dataset(REPO_ID, split="train")
|
| 47 |
+
|
| 48 |
+
print(dataset)
|
| 49 |
+
print(f"Loaded {len(dataset)} rows from {REPO_ID}")
|
| 50 |
+
|
| 51 |
+
limit = min(3, len(dataset))
|
| 52 |
+
for index in range(limit):
|
| 53 |
+
summarize_row(dataset[index], index)
|
| 54 |
+
|
| 55 |
+
return 0
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
raise SystemExit(main())
|