add control glpn depth processor

This commit is contained in:
Vladimir Mandic
2024-01-13 11:31:20 -05:00
parent c6d78d2b34
commit 615f539a4e
3 changed files with 38 additions and 0 deletions
+1
View File
@@ -31,4 +31,5 @@ class DPTDetector:
output = prediction.squeeze().cpu().numpy()
formatted = (output * 255 / np.max(output)).astype("uint8")
depth = Image.fromarray(formatted)
depth = depth.convert('RGB')
return depth
+35
View File
@@ -0,0 +1,35 @@
from PIL import Image
import numpy as np
import torch
from transformers import AutoImageProcessor, GLPNForDepthEstimation
from modules import devices
image_processor: AutoImageProcessor = None
glpn_model: GLPNForDepthEstimation = None
class GLPNDetector:
def __call__(self, input_image=None):
global image_processor, glpn_model # pylint: disable=global-statement
from modules.control.processors import cache_dir
if image_processor is None:
image_processor = AutoImageProcessor.from_pretrained("vinvino02/glpn-kitti", cache_dir=cache_dir)
if glpn_model is None:
glpn_model = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-kitti", cache_dir=cache_dir)
with devices.inference_context():
inputs = image_processor(images=input_image, return_tensors="pt")
outputs = glpn_model(**inputs)
predicted_depth = outputs.predicted_depth
prediction = torch.nn.functional.interpolate(
predicted_depth.unsqueeze(1),
size=input_image.size[::-1],
mode="bicubic",
align_corners=False,
)
output = prediction.squeeze().cpu().numpy()
formatted = 255 - (output * 255 / np.max(output)).astype("uint8")
depth = Image.fromarray(formatted)
depth = depth.convert('RGB')
return depth
+2
View File
@@ -24,6 +24,7 @@ from modules.control.proc.segment_anything import SamDetector
from modules.control.proc.zoe import ZoeDetector
from modules.control.proc.marigold import MarigoldDetector
from modules.control.proc.dpt import DPTDetector
from modules.control.proc.glpn import GLPNDetector
models = {}
@@ -54,6 +55,7 @@ config = {
'MLSD': {'class': MLSDdetector, 'checkpoint': True, 'params': {'thr_v': 0.1, 'thr_d': 0.1}},
'Shuffle': {'class': ContentShuffleDetector, 'checkpoint': False, 'params': {}},
'DPT Depth Hybrid': {'class': DPTDetector, 'checkpoint': False, 'params': {}},
'GLPN Depth': {'class': GLPNDetector, 'checkpoint': False, 'params': {}},
# 'Midas Depth Large': {'class': MidasDetector, 'checkpoint': True, 'params': {'bg_th': 0.1, 'depth_and_normal': False}, 'load_config': {'pretrained_model_or_path': 'Intel/dpt-large', 'model_type': "dpt_large", 'filename': ''}},
# 'Zoe Depth Zoe': {'class': ZoeDetector, 'checkpoint': True, 'params': {}},
# 'Zoe Depth NK': {'class': ZoeDetector, 'checkpoint': True, 'params': {}, 'load_config': {'pretrained_model_or_path': 'halffried/gyre_zoedepth', 'filename': 'ZoeD_M12_NK.safetensors', 'model_type': "zoedepth_nk"}},