Spaces:
Sleeping
Sleeping
File size: 2,677 Bytes
0534f3d 9b6581c ad17f8d 9b6581c 0534f3d 9b6581c ad17f8d 9b6581c 0534f3d 9b6581c ad17f8d 0534f3d ad17f8d 0534f3d 9b6581c ad17f8d 0534f3d ad17f8d 0534f3d | 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 | import os, glob, random, datetime, tempfile
import pandas as pd
from PIL import Image, ImageDraw
ASSETS_DIR = os.path.join(os.path.dirname(__file__), "assets", "sample_deer")
OUT_DIR = os.path.join(tempfile.gettempdir(), "sample_deer")
os.makedirs(OUT_DIR, exist_ok=True)
def _make_placeholder(idx: int, w=960, h=540):
img = Image.new("RGB", (w, h), (34, 139, 34)) # green field
d = ImageDraw.Draw(img)
msg = f"Placeholder {idx}\nAdd real deer JPGs to assets/sample_deer/"
d.text((24, 24), msg, fill=(255, 255, 255))
fn = f"placeholder_{idx:03d}.jpg"
dst = os.path.join(OUT_DIR, fn)
img.save(dst, "JPEG", quality=92)
return fn
def build_sample_pack(n=150, cams=6, seed=42):
random.seed(seed)
files = sorted(glob.glob(os.path.join(ASSETS_DIR, "*.jpg")) +
glob.glob(os.path.join(ASSETS_DIR, "*.jpeg")) +
glob.glob(os.path.join(ASSETS_DIR, "*.png")))
rows = []
picked = files if len(files) <= n else random.sample(files, k=n)
# Try to copy/normalize any real images
for src in picked:
fn = os.path.basename(src)
dst = os.path.join(OUT_DIR, fn)
try:
img = Image.open(src).convert("RGB")
img.save(dst, "JPEG", quality=92)
cam = f"CAM{random.randint(1, cams):02d}"
ts = (datetime.datetime.now()
- datetime.timedelta(days=random.randint(0, 14),
hours=random.randint(0, 23),
minutes=random.randint(0, 59))).strftime("%Y-%m-%d %H:%M:%S")
rows.append({
"filename": os.path.basename(dst),
"camera_id": cam,
"timestamp": ts,
"source_dataset": "local_assets"
})
except Exception:
continue
# Fallback: generate placeholders if none loaded
if not rows:
count = min(n, 20) # keep it light for the Space
for i in range(count):
fn = _make_placeholder(i + 1)
cam = f"CAM{random.randint(1, cams):02d}"
ts = (datetime.datetime.now()
- datetime.timedelta(days=random.randint(0, 14),
hours=random.randint(0, 23),
minutes=random.randint(0, 59))).strftime("%Y-%m-%d %H:%M:%S")
rows.append({
"filename": fn,
"camera_id": cam,
"timestamp": ts,
"source_dataset": "placeholder"
})
pd.DataFrame(rows).to_csv(os.path.join(OUT_DIR, "metadata.csv"), index=False)
return OUT_DIR
|