Spaces:
Sleeping
Sleeping
Download ImageDepth1.py from bharathj16/Test1: direct link, hf CLI and curl.
- Browser
- Download file 922 Bytes
-
https://huggingface.co/spaces/bharathj16/Test1/resolve/main/ImageDepth1.py
- Command line
-
hf download hf://spaces/bharathj16/Test1/ImageDepth1.py
-
curl -L -o ImageDepth1.py https://huggingface.co/spaces/bharathj16/Test1/resolve/main/ImageDepth1.py
922 Bytes
| from transformers import DPTImageProcessor, DPTForDepthEstimation | |
| import torch | |
| import numpy as np | |
| from PIL import Image | |
| import requests | |
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | |
| image = Image.open(requests.get(url, stream=True).raw) | |
| processor = DPTImageProcessor.from_pretrained("Intel/dpt-large") | |
| model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large") | |
| # prepare image for the model | |
| inputs = processor(images=image, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| predicted_depth = outputs.predicted_depth | |
| # interpolate to original size | |
| prediction = torch.nn.functional.interpolate( | |
| predicted_depth.unsqueeze(1), | |
| size=image.size[::-1], | |
| mode="bicubic", | |
| align_corners=False, | |
| ) | |
| # visualize the prediction | |
| output = prediction.squeeze().cpu().numpy() | |
| formatted = (output * 255 / np.max(output)).astype("uint8") | |
| depth = Image.fromarray(formatted) |