-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictor.py
49 lines (39 loc) · 1.64 KB
/
predictor.py
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
import torch
from PIL import Image
from misc import colorize
class DepthEstimationModel:
"""
DepthMap'te daha yakın bölgeler beyaz renkte
daha uzak bölgeler ise siyah veya ona yakın renkte gözükür
"""
def __init__(self) -> None:
self.device = self._get_device()
self.model = self._initialize_model(
model_repo="isl-org/ZoeDepth", model_Name="ZoeD_N"
).to(self.device)
def _get_device(self):
return "cuda" if torch.cuda.is_available() else "cpu"
def _initialize_model(
self, model_repo="isl-org/ZoeDepth", model_Name="ZoeD_N"
): # it will work automaticaly when the class is called it will download and returned
torch.hub.help(
"intel-isl/MiDaS", "DPT_BEiT_L_384", force_reload=True
) # Triggers fresh download of MiDaS repo
model = torch.hub.load(
model_repo, model_Name, pretrained=True, skip_validation=False
)
model.eval()
print("Model initialized.")
return model
def save_colored_depth(self, depth_numpy, output_path):
colored = colorize(depth_numpy)
Image.fromarray(colored).save(output_path)
print("Image saved.")
def calculate_depthmap(self, image_path, output_path):
image = Image.open(image_path).convert("RGB")
print("Image read.")
depth_numpy = self.model.infer_pil(image)
self.save_colored_depth(depth_numpy, output_path)
return f"Image saved to {output_path}"
model = DepthEstimationModel() # Create a instance from a class. Nesne veya Object
model.calculate_depthmap("./test_image.png", "output_image.png")