agri-vision-detector / build_cache_generic.py
Solshine's picture
Self-contained release: phone_site with runtime, RUN.md, d1 GGUFs, scripts, weights, licences by upstream terms
f79b7a1 verified
Raw History Blame Contribute Delete
1.02 kB
"""Resize images listed in a meta jsonl (field 'path') to SxS uint8 -> <prefix>_<S>.npy (row order == meta order), bad indices -> <prefix>_bad.json.
Usage: build_cache_generic.py META.jsonl PREFIX [S=224]"""
import json, sys, numpy as np
from multiprocessing import Pool
from PIL import Image
META,PFX=sys.argv[1],sys.argv[2]; S=int(sys.argv[3]) if len(sys.argv)>3 else 224
rows=[json.loads(l) for l in open(META)]
def f(i):
try:
im=Image.open(rows[i]["path"]); im.draft("RGB",(S*2,S*2)); return i,np.asarray(im.convert("RGB").resize((S,S),Image.BILINEAR))
except Exception as e: return i,None
if __name__=="__main__":
mm=np.lib.format.open_memmap(f"{PFX}_{S}.npy",mode="w+",dtype=np.uint8,shape=(len(rows),S,S,3)); bad=[]
with Pool(4) as p:
for k,(i,a) in enumerate(p.imap_unordered(f,range(len(rows)),chunksize=64)):
if a is None: bad.append(i)
else: mm[i]=a
mm.flush(); json.dump(bad,open(f"{PFX}_bad.json","w")); print("done",len(rows),"bad",len(bad))