Hello i’m new to python and opencv image processing.
import cv2
import torch
import matplotlib.pyplot as plt
# Download the MiDas
midas = torch.hub.load('intel-isl/MiDaS','MiDaS_small')
midas.to('cuda')
midas.eval()
#Input transformational pipeline
transforms = torch.hub.load('intel-isl/MiDas', 'transforms')
transform = transforms.small_transform
# Hook into OpenCV
cap= cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 50)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 50)
while cap.isOpened():
ret, frame = cap.read()
#Transform imput for midas
img = cv2.resize(frame, (128, 128))
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
src = cv2.cuda_GpuMat()
imgbatch = transform(img).to('cuda')
# Make a prediction
with torch.no_grad():
prediction = midas(imgbatch)
prediction = torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size = img.shape[:2],
mode='bicubic',
align_corners=False
).squeeze()
output = prediction.cpu().numpy()
plt.imshow(output)
cv2.imshow('CV2Frame', frame)
plt.pause(0.000000001)
if cv2.waitKey(10) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
plt.show()