Spaces:
Sleeping
Sleeping
Download model_code/image_data.py from oussamab2n/GazeLIFT: direct link, hf CLI and curl.
- Browser
- Download file 879 Bytes
-
https://huggingface.co/spaces/oussamab2n/GazeLIFT/resolve/main/model_code/image_data.py
- Command line
-
hf download hf://spaces/oussamab2n/GazeLIFT/model_code/image_data.py
-
curl -L -o image_data.py https://huggingface.co/spaces/oussamab2n/GazeLIFT/resolve/main/model_code/image_data.py
879 Bytes
| """Match the trusted encoder-cache producer's raw image preprocessing exactly.""" | |
| import numpy as np | |
| from PIL import Image | |
| import torch | |
| MEAN=torch.tensor([.485,.456,.406])[:,None,None] | |
| STD=torch.tensor([.229,.224,.225])[:,None,None] | |
| def load_image(path,resolution,expected_native): | |
| if path.suffix.lower()=='.dcm': | |
| import pydicom | |
| arr=pydicom.dcmread(path).pixel_array.astype(np.float32) | |
| arr=(arr-arr.min())/max(float(arr.max()-arr.min()),1e-8) | |
| image=Image.fromarray((arr*255).astype(np.uint8)).convert('RGB') | |
| else:image=Image.open(path).convert('RGB') | |
| assert image.size==(int(expected_native[-1]),int(expected_native[-2])), 'Image/native-mask shape mismatch' | |
| image=image.resize((resolution,resolution),Image.Resampling.BILINEAR) | |
| return ((torch.from_numpy(np.asarray(image).copy()).permute(2,0,1).float()/255-MEAN)/STD).half() | |