-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathvideo_processing_midas3.py
209 lines (176 loc) · 6.77 KB
/
video_processing_midas3.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import traceback
import cv2
import numpy as np
import sys
import argparse
from datetime import datetime
import os
# Mono depth using MiDaS project - new 3.0 models and 2.1 models
# https://github.com/intel-isl/MiDaS
# Install steps:
# cd MiDaS; wget https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt
# Status: working
sys.path.insert(0, '../MiDaS/')
import torch
from torchvision.transforms import Compose
from midas.dpt_depth import DPTDepthModel
from midas.midas_net import MidasNet
from midas.midas_net_custom import MidasNet_small
from midas.transforms import Resize, NormalizeImage, PrepareForNet
def init_model(transform):
optimize=True
parser = argparse.ArgumentParser()
parser.add_argument('-mw', '--model_weights',
default='dpt_large-midas-2f21e586.pt',
help='path to the trained weights of model'
)
parser.add_argument('-mt', '--model_type',
default='large',
help='model type: large or hybrid'
)
parser.add_argument('--optimize', dest='optimize', action='store_true')
parser.add_argument('--no-optimize', dest='optimize', action='store_false')
parser.set_defaults(optimize=True)
args, unknown = parser.parse_known_args()
# set torch options
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
print("initialize")
net_w, net_h = 384, 384
# select device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("device: %s" % device)
# # load network
# if args.model_type == "large":
# model_path = "../MiDaS/"+args.model_weights
# model = MidasNet(model_path, non_negative=True)
# net_w, net_h = 384, 384
# elif args.model_type == "small":
# if "small" not in args.model_weights:
# args.model_weights = "model-small-70d6b9c8.pt"
# model_path = "../MiDaS/"+args.model_weights
# model = MidasNet_small(model_path, features=64, backbone="efficientnet_lite3", exportable=True, non_negative=True, blocks={'expand': True})
# net_w, net_h = 256, 256
# else:
# print(f"model_type '{model_type}' not implemented, use: --model_type large")
# assert False
# load network
if args.model_type == "large": # DPT-Large
model = DPTDepthModel(
path="../MiDaS/"+args.model_weights,
backbone="vitl16_384",
non_negative=True,
)
net_w, net_h = 384, 384
resize_mode = "minimal"
normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif args.model_type == "hybrid": #DPT-Hybrid
if "hybrid" not in args.model_weights:
args.model_weights = "dpt_hybrid-midas-501f0c75.pt"
model = DPTDepthModel(
path="../MiDaS/"+args.model_weights,
backbone="vitb_rn50_384",
non_negative=True,
)
net_w, net_h = 384, 384
resize_mode="minimal"
normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif args.model_type == "midas_v21":
if "large" not in args.model_weights:
args.model_weights = "midas_v21-f6b98070.pt"
model_path = "../MiDaS/"+args.model_weights
model = MidasNet(model_path, non_negative=True)
net_w, net_h = 384, 384
resize_mode="upper_bound"
normalization = NormalizeImage(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
elif args.model_type == "midas_v21_small":
if "small" not in args.model_weights:
args.model_weights = "midas_v21_small-70d6b9c8.pt"
model_path = "../MiDaS/"+args.model_weights
model = MidasNet_small(model_path, features=64, backbone="efficientnet_lite3", exportable=True, non_negative=True, blocks={'expand': True})
net_w, net_h = 256, 256
resize_mode="upper_bound"
normalization = NormalizeImage(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
# else:
# print(f"model_type '{model_type}' not implemented, use: --model_type large")
# assert False
transform = Compose(
[
Resize(
net_w,
net_h,
resize_target=None,
keep_aspect_ratio=True,
ensure_multiple_of=32,
resize_method=resize_mode,
image_interpolation_method=cv2.INTER_CUBIC,
),
normalization,
PrepareForNet(),
]
)
model.eval()
if optimize==True:
# rand_example = torch.rand(1, 3, net_h, net_w)
# model(rand_example)
# traced_script_module = torch.jit.trace(model, rand_example)
# model = traced_script_module
if device == torch.device("cuda"):
model = model.to(memory_format=torch.channels_last)
model = model.half()
model.to(device)
return (model, transform, device, args.optimize), args
def process_image(transform,processing_model,img):
global previous_grey, hsv, skip_frames,hsv_roi,roi_hist, term_criteria,x, y, w, h
tracks = []
try:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0
img = get_depth(img,processing_model[0],processing_model[1], processing_model[2], processing_model[3])
img = (img/256).astype(np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img = cv2.applyColorMap(img, cv2.COLORMAP_JET)
except Exception as e:
track = traceback.format_exc()
print(track)
print("MiDaS 3.0 Exception",e)
pass
return tracks,img
def depth_to_image(depth, bits=1):
depth_min = depth.min()
depth_max = depth.max()
max_val = (2**(8*bits))-1
if depth_max - depth_min > np.finfo("float").eps:
out = max_val * (depth - depth_min) / (depth_max - depth_min)
else:
out = 0
if bits == 1:
img = out.astype("uint8")
elif bits == 2:
img = out.astype("uint16")
return img
def get_depth(img, model,transform,device,optimize):
img_input = transform({"image": img})["image"]
# compute
with torch.no_grad():
sample = torch.from_numpy(img_input).to(device).unsqueeze(0)
if optimize==True and device == torch.device("cuda"):
sample = sample.to(memory_format=torch.channels_last)
sample = sample.half()
prediction = model.forward(sample)
prediction = (
torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size=img.shape[:2],
mode="bicubic",
align_corners=False,
)
.squeeze()
.cpu()
.numpy()
)
img = depth_to_image(prediction, bits=2)
return img