import json import shutil import numpy as np import rasterio from rasterio.warp import transform_bounds from PIL import Image, UnidentifiedImageError from pydantic import BaseModel, Field from typing import Literal from .store import asset, path_for, get, put, now from .geo import polygons, measurements Image.MAX_IMAGE_PIXELS=25_000_000 def ingest_image(path,name,metadata=None): suffix=path.suffix.lower() spatial={}; profile=None if suffix in ('.tif','.tiff'): with rasterio.open(path) as src: if src.width*src.height>25_000_000 or src.count>16: raise ValueError('Maximum 25 million pixels and 16 bands; crop the scene first') data=src.read().astype('float32'); valid=np.isfinite(data).all(axis=0)&(src.dataset_mask()>0) if src.crs: spatial={'crs':str(src.crs),'bbox':list(transform_bounds(src.crs,'EPSG:4326',*src.bounds)), 'transform':list(src.transform)[:6],'resolution':list(src.res)} profile=src.profile rgb=data[[2,1,0]] if src.count>=6 else data[:3] if len(rgb)==1: rgb=np.repeat(rgb,3,axis=0) if len(rgb)!=3: raise ValueError('Need RGB or a multiband image with at least three bands') preview=np.zeros_like(rgb) for i,b in enumerate(rgb): vals=b[valid];lo,hi=np.percentile(vals,[2,98]) if len(vals) else (0,1) preview[i]=np.clip((b-lo)/max(hi-lo,1e-6),0,1)*255 preview=preview.transpose(1,2,0).astype('uint8') else: with Image.open(path) as im: im.load() preview=np.asarray(im.convert('RGB')); data=preview.transpose(2,0,1).astype('float32'); valid=np.ones(preview.shape[:2],bool) rec=asset('image',name,'USER IMPORT',width=int(data.shape[2]),height=int(data.shape[1]),bands=int(data.shape[0]),**spatial) rec.update(metadata or {}) rec['file_suffix']='tif' if suffix in ('.tif','.tiff') else 'image' shutil.copyfile(path,path_for(rec['id'],rec['file_suffix'])) np.savez_compressed(path_for(rec['id'],'image.npz'),data=data,valid=valid) Image.fromarray(preview).save(path_for(rec['id'],'png')) rec['preview_url']=f'/api/files/{rec["id"]}/png' rec['georeferenced']=bool(spatial) return put('assets',rec,rec['id']) class SegmentRequest(BaseModel): image_id:str method:Literal['pretrained-s2','ndwi','rgb-baseline','kaggle-onnx']='pretrained-s2' threshold:float=Field(default=.5,ge=.01,le=.99) green_band:int=Field(default=2,ge=1,le=16) nir_band:int=Field(default=4,ge=1,le=16) confirm_s2_contract:bool=False def tile_predict(data,predict,tile=512,overlap=256): c,h,w=data.shape out=np.zeros((h,w),dtype='float32'); count=np.zeros((h,w),dtype='float32') weight=np.maximum(np.outer(np.hanning(tile),np.hanning(tile)),.01).astype('float32') for y in range(0,h,tile-overlap): for x in range(0,w,tile-overlap): hh,ww=min(tile,h-y),min(tile,w-x) patch=np.pad(data[:,y:y+hh,x:x+ww],((0,0),(0,tile-hh),(0,tile-ww)),mode='edge') p=predict(patch)[0:hh,0:ww] out[y:y+hh,x:x+ww]+=p*weight[:hh,:ww];count[y:y+hh,x:x+ww]+=weight[:hh,:ww] return out/np.maximum(count,1e-9) def pretrained_probability(data): from .sources import MODEL_DIR if not (MODEL_DIR/'model.pth').exists(): raise ValueError('Install the pretrained model from Data sources first') import torch import segmentation_models_pytorch as smp torch.set_num_threads(4) model=smp.UnetPlusPlus(encoder_name='efficientnet-b4',encoder_weights=None,in_channels=6,classes=2) weights=torch.load(MODEL_DIR/'model.pth',map_location='cpu',weights_only=True) for key in ('state_dict','model_state_dict','model'): if isinstance(weights,dict) and key in weights: weights=weights[key];break weights={k.removeprefix('module.'):v for k,v in weights.items()} model.load_state_dict(weights);model.eval() def predict(patch): # Matches upstream geoai timm_segment: divide values by 255 when max > 1. # This is NOT physical reflectance normalization; provenance records this caveat. patch=patch.astype('float32') if patch.max()>1.: patch=patch/255. with torch.inference_mode(): return model(torch.from_numpy(patch[None])).softmax(1)[0,1].cpu().numpy() return tile_predict(data,predict) def segment(req:SegmentRequest): src=get('assets',req.image_id);a=np.load(path_for(req.image_id,'image.npz'));data=a['data'];valid=a['valid'] if not valid.any(): raise ValueError('No valid pixels available for segmentation') clean=np.where(valid[None],data,0) warnings=['Threshold sensitivity is not predictive accuracy','Cloud, terrain-shadow and wet-soil errors require independent review'] if req.method=='pretrained-s2': if data.shape[0]!=6: raise ValueError('Pretrained model requires exactly B2,B3,B4,B8,B11,B12; RGB camera images are incompatible') if src.get('band_contract')!='S2_L2A_6B_DN10000' and not req.confirm_s2_contract: raise ValueError('Confirm Sentinel-2 L2A band order and reflectance ×10000 scaling') p=pretrained_probability(clean);label='AI PREDICTION · pretrained UNet++';score_type='Uncalibrated water softmax' elif req.method=='ndwi': if max(req.green_band,req.nir_band)>len(data) or req.green_band==req.nir_band: raise ValueError('Select distinct valid green and NIR bands') g,n=clean[req.green_band-1],clean[req.nir_band-1] p=(np.clip((g-n)/(np.abs(g)+np.abs(n)+1e-6),-1,1)+1)/2 label='SPECTRAL BASELINE · NDWI';score_type='Rescaled NDWI; not a probability' elif req.method=='kaggle-onnx': from .store import ROOT import onnxruntime as ort model_path=ROOT/'models'/'kaggle-water.onnx' if not model_path.exists(): raise ValueError('Place the Kaggle notebook ONNX export in models/kaggle-water.onnx') if len(data)!=3: raise ValueError('Kaggle RGB model expects three 8-bit RGB bands') session=ort.InferenceSession(str(model_path),providers=['CPUExecutionProvider']) p=tile_predict(clean/255.,lambda x:session.run(None,{session.get_inputs()[0].name:x[None].astype('float32')})[0][0,0],256,128) label='AI PREDICTION · Kaggle RGB U-Net';score_type='Uncalibrated sigmoid' else: rgb=clean[:3]/max(float(np.percentile(clean[:,valid],99)),1.) red,green,blue=rgb p=np.clip(.5+1.8*(blue-red)+.3*(green-red)-.25*np.max(rgb,axis=0),0,1) label='RGB HEURISTIC BASELINE · NOT TRAINED AI';score_type='Color heuristic; not confidence' warnings.append('RGB camera colors are not calibrated satellite reflectances') p=np.clip(p,0,1);p[~valid]=0 from .sources import MODEL_REV rec=put('segmentations',{'image_id':req.image_id,'method':req.method,'label':label,'score_type':score_type,'created_at':now(), 'model_version':MODEL_REV if req.method=='pretrained-s2' else req.method+'-1.0', 'warnings':warnings,'source':src,'threshold':req.threshold,'request':req.model_dump()}) np.savez_compressed(path_for(rec['id'],'prob.npz'),p=p,valid=valid) return threshold_result(rec['id'],req.threshold) def threshold_result(id,threshold): rec=get('segmentations',id);src=rec['source'];a=np.load(path_for(id,'prob.npz'));p=a['p'];valid=a['valid'];mask=(p>=threshold)&valid rgba=np.zeros((*p.shape,4),dtype='uint8');rgba[mask]=[34,211,238,210] Image.fromarray(rgba).save(path_for(id,'png'));Image.fromarray((mask*255).astype('uint8')).save(path_for(id,'mask.png')) rec.update({'threshold':threshold,'coverage_pct':float(mask.sum()/max(valid.sum(),1)*100),'mean_water_score':float(p[mask].mean()) if mask.any() else None, 'histogram':np.histogram(p[valid],bins=10,range=(0,1))[0].tolist(),'water_pixels':int(mask.sum()),'valid_pixels':int(valid.sum()), 'preview_url':f'/api/files/{id}/png','mask_url':f'/api/files/{id}/mask.png','geojson':None,'area_m2':None}) if src.get('georeferenced'): from affine import Affine t=Affine(*src['transform']); fc=polygons(mask,t,src['crs'],{'source':rec['label'],'segmentation_id':id}) rec['geojson']=fc;rec['area_m2']=measurements(fc)['area_m2'] for suffix,arr,dtype,nodata in [('mask.tif',np.where(valid,mask,255),'uint8',255),('prob.tif',np.where(valid,p,-9999),'float32',-9999)]: with rasterio.open(path_for(id,suffix),'w',driver='GTiff',width=p.shape[1],height=p.shape[0],count=1,dtype=dtype,transform=t,crs=src['crs'],nodata=nodata,compress='deflate') as dst: dst.write(arr.astype(dtype),1) path_for(id,'geojson').write_text(json.dumps(fc)) return put('segmentations',rec,id) def metrics(prediction,truth,valid=None): p=np.asarray(prediction).astype(bool);t=np.asarray(truth).astype(bool) if p.shape!=t.shape: raise ValueError('Prediction and truth shapes must match') v=np.ones_like(p,dtype=bool) if valid is None else valid tp=int((p&t&v).sum());fp=int((p&~t&v).sum());fn=int((~p&t&v).sum());tn=int((~p&~t&v).sum()) ratio=lambda x,y: x/y if y else None return {'iou':ratio(tp,tp+fp+fn),'dice':ratio(2*tp,2*tp+fp+fn),'precision':ratio(tp,tp+fp),'recall':ratio(tp,tp+fn), 'f1':ratio(2*tp,2*tp+fp+fn),'false_positive_rate':ratio(fp,fp+tn),'false_negative_rate':ratio(fn,fn+tp),'tp':tp,'fp':fp,'fn':fn,'tn':tn}