Created
October 12, 2024 10:54
-
-
Save ZlodeiBaal/f6fa2d43942f66cb4c5b7ca5c16c2089 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import depth_pro | |
import cv2 | |
import torch | |
import numpy as np | |
global depth | |
def click_event(event, x, y, flags, params): | |
# checking for left mouse clicks | |
if event == cv2.EVENT_LBUTTONDOWN: | |
print(depth[y,x]) | |
def renormalize_to_255(arr): | |
# Find the minimum and maximum of the array | |
min_val = np.min(arr) | |
max_val = np.max(arr) | |
# Renormalize to the range [0, 255] | |
normalized_arr = (arr - min_val) / (max_val - min_val) * 255 | |
# Convert to unsigned 8-bit integer (0-255) | |
normalized_arr = normalized_arr.astype(np.uint8) | |
return normalized_arr | |
# Load model and preprocessing transform | |
device_cuda = torch.device("cuda:0") | |
model, transform = depth_pro.create_model_and_transforms(device=device_cuda) | |
model.eval() | |
# Load and preprocess an image. | |
cam = cv2.VideoCapture(0) | |
#image, _, f_px = depth_pro.load_rgb('E:/Projects/3D/ml-depth-pro/data/example.jpg') | |
#print(f_px) | |
f_px = None | |
ret, image = cam.read() | |
cv2.imshow('Camera', image[:,:,0]) | |
cv2.setMouseCallback('Camera', click_event) | |
while True: | |
ret, image = cam.read() | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
if ret: | |
IMG = transform(image) | |
# Run inference. | |
prediction = model.infer(IMG, f_px=f_px) | |
depth = prediction["depth"].cpu().detach().numpy() | |
depth_img = renormalize_to_255(depth) | |
cv2.imshow('Camera', depth_img) | |
focallength_px = prediction["focallength_px"] # Focal length in pixels. | |
if cv2.waitKey(0) == ord('q'): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment