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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
import torch
|
7 |
+
import torch.nn.functional as F
|
8 |
+
from torchvision import transforms
|
9 |
+
from torchvision.transforms import Compose
|
10 |
+
import tempfile
|
11 |
+
from functools import partial
|
12 |
+
import spaces
|
13 |
+
from zipfile import ZipFile
|
14 |
+
from vincenty import vincenty
|
15 |
+
import json
|
16 |
+
from collections import Counter
|
17 |
+
import mediapy
|
18 |
+
|
19 |
+
#from depth_anything.dpt import DepthAnything
|
20 |
+
#from depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
|
21 |
+
from huggingface_hub import hf_hub_download
|
22 |
+
from depth_anything_v2.dpt import DepthAnythingV2
|
23 |
+
|
24 |
+
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
25 |
+
model_configs = {
|
26 |
+
'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
|
27 |
+
'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
|
28 |
+
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
29 |
+
'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
|
30 |
+
}
|
31 |
+
encoder2name = {
|
32 |
+
'vits': 'Small',
|
33 |
+
'vitb': 'Base',
|
34 |
+
'vitl': 'Large',
|
35 |
+
'vitg': 'Giant', # we are undergoing company review procedures to release our giant model checkpoint
|
36 |
+
}
|
37 |
+
|
38 |
+
edge = []
|
39 |
+
gradient = None
|
40 |
+
params = { "fnum":0, "l":16 }
|
41 |
+
dcolor = []
|
42 |
+
pcolors = []
|
43 |
+
frame_selected = 0
|
44 |
+
frames = []
|
45 |
+
depths = []
|
46 |
+
masks = []
|
47 |
+
locations = []
|
48 |
+
mesh = []
|
49 |
+
mesh_n = []
|
50 |
+
scene = None
|
51 |
+
|
52 |
+
def zip_files(files_in, files_out):
|
53 |
+
with ZipFile("depth_result.zip", "w") as zipObj:
|
54 |
+
for idx, file in enumerate(files_in):
|
55 |
+
zipObj.write(file, file.split("/")[-1])
|
56 |
+
for idx, file in enumerate(files_out):
|
57 |
+
zipObj.write(file, file.split("/")[-1])
|
58 |
+
return "depth_result.zip"
|
59 |
+
|
60 |
+
def create_video(frames, fps, type):
|
61 |
+
print("building video result")
|
62 |
+
imgs = []
|
63 |
+
for j, img in enumerate(frames):
|
64 |
+
imgs.append(cv2.cvtColor(cv2.imread(img).astype(np.uint8), cv2.COLOR_BGR2RGB))
|
65 |
+
|
66 |
+
mediapy.write_video(type + "_result.mp4", imgs, fps=fps)
|
67 |
+
return type + "_result.mp4"
|
68 |
+
|
69 |
+
@torch.no_grad()
|
70 |
+
#@spaces.GPU
|
71 |
+
def predict_depth(image, model):
|
72 |
+
return model.infer_image(image)
|
73 |
+
|
74 |
+
#def predict_depth(model, image):
|
75 |
+
# return model(image)["depth"]
|
76 |
+
|
77 |
+
def make_video(video_path, outdir='./vis_video_depth', encoder='vits'):
|
78 |
+
if encoder not in ["vitl","vitb","vits","vitg"]:
|
79 |
+
encoder = "vits"
|
80 |
+
|
81 |
+
model_name = encoder2name[encoder]
|
82 |
+
model = DepthAnythingV2(**model_configs[encoder])
|
83 |
+
filepath = hf_hub_download(repo_id=f"depth-anything/Depth-Anything-V2-{model_name}", filename=f"depth_anything_v2_{encoder}.pth", repo_type="model")
|
84 |
+
state_dict = torch.load(filepath, map_location="cpu")
|
85 |
+
model.load_state_dict(state_dict)
|
86 |
+
model = model.to(DEVICE).eval()
|
87 |
+
|
88 |
+
#mapper = {"vits":"small","vitb":"base","vitl":"large"}
|
89 |
+
# DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
90 |
+
# model = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(DEVICE).eval()
|
91 |
+
# Define path for temporary processed frames
|
92 |
+
#temp_frame_dir = tempfile.mkdtemp()
|
93 |
+
|
94 |
+
#margin_width = 50
|
95 |
+
#to_tensor_transform = transforms.ToTensor()
|
96 |
+
|
97 |
+
#DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
98 |
+
# depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_{}14'.format(encoder)).to(DEVICE).eval()
|
99 |
+
#depth_anything = pipeline(task = "depth-estimation", model=f"nielsr/depth-anything-{mapper[encoder]}")
|
100 |
+
|
101 |
+
# total_params = sum(param.numel() for param in depth_anything.parameters())
|
102 |
+
# print('Total parameters: {:.2f}M'.format(total_params / 1e6))
|
103 |
+
|
104 |
+
#transform = Compose([
|
105 |
+
# Resize(
|
106 |
+
# width=518,
|
107 |
+
# height=518,
|
108 |
+
# resize_target=False,
|
109 |
+
# keep_aspect_ratio=True,
|
110 |
+
# ensure_multiple_of=14,
|
111 |
+
# resize_method='lower_bound',
|
112 |
+
# image_interpolation_method=cv2.INTER_CUBIC,
|
113 |
+
# ),
|
114 |
+
# NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
115 |
+
# PrepareForNet(),
|
116 |
+
#])
|
117 |
+
|
118 |
+
if os.path.isfile(video_path):
|
119 |
+
if video_path.endswith('txt'):
|
120 |
+
with open(video_path, 'r') as f:
|
121 |
+
lines = f.read().splitlines()
|
122 |
+
else:
|
123 |
+
filenames = [video_path]
|
124 |
+
else:
|
125 |
+
filenames = os.listdir(video_path)
|
126 |
+
filenames = [os.path.join(video_path, filename) for filename in filenames if not filename.startswith('.')]
|
127 |
+
filenames.sort()
|
128 |
+
|
129 |
+
# os.makedirs(outdir, exist_ok=True)
|
130 |
+
|
131 |
+
for k, filename in enumerate(filenames):
|
132 |
+
file_size = os.path.getsize(filename)/1024/1024
|
133 |
+
if file_size > 128.0:
|
134 |
+
print(f'File size of {filename} larger than 128Mb, sorry!')
|
135 |
+
return filename
|
136 |
+
print('Progress {:}/{:},'.format(k+1, len(filenames)), 'Processing', filename)
|
137 |
+
|
138 |
+
raw_video = cv2.VideoCapture(filename)
|
139 |
+
frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
140 |
+
frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
|
141 |
+
if frame_rate < 1:
|
142 |
+
frame_rate = 1
|
143 |
+
cframes = int(raw_video.get(cv2.CAP_PROP_FRAME_COUNT))
|
144 |
+
print(f'frames: {cframes}, fps: {frame_rate}')
|
145 |
+
# output_width = frame_width * 2 + margin_width
|
146 |
+
|
147 |
+
#filename = os.path.basename(filename)
|
148 |
+
# output_path = os.path.join(outdir, filename[:filename.rfind('.')] + '_video_depth.mp4')
|
149 |
+
#with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
|
150 |
+
# output_path = tmpfile.name
|
151 |
+
#out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"avc1"), frame_rate, (output_width, frame_height))
|
152 |
+
#fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
153 |
+
#out = cv2.VideoWriter(output_path, fourcc, frame_rate, (output_width, frame_height))
|
154 |
+
global masks
|
155 |
+
count = 0
|
156 |
+
n = 0
|
157 |
+
depth_frames = []
|
158 |
+
orig_frames = []
|
159 |
+
thumbnail_old = []
|
160 |
+
|
161 |
+
while raw_video.isOpened():
|
162 |
+
ret, raw_frame = raw_video.read()
|
163 |
+
if not ret:
|
164 |
+
break
|
165 |
+
else:
|
166 |
+
print(count)
|
167 |
+
|
168 |
+
frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB) / 255.0
|
169 |
+
frame_pil = Image.fromarray((frame * 255).astype(np.uint8))
|
170 |
+
#frame = transform({'image': frame})['image']
|
171 |
+
#frame = torch.from_numpy(frame).unsqueeze(0).to(DEVICE)
|
172 |
+
raw_frame_bg = cv2.medianBlur(raw_frame, 255)
|
173 |
+
|
174 |
+
#
|
175 |
+
depth = predict_depth(raw_frame[:, :, ::-1], model)
|
176 |
+
depth_gray = ((depth - depth.min()) / (depth.max() - depth.min()) * 255.0).astype(np.uint8)
|
177 |
+
#
|
178 |
+
|
179 |
+
#depth = to_tensor_transform(predict_depth(depth_anything, frame_pil))
|
180 |
+
#depth = F.interpolate(depth[None], (frame_height, frame_width), mode='bilinear', align_corners=False)[0, 0]
|
181 |
+
#depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
182 |
+
#depth = depth.cpu().numpy().astype(np.uint8)
|
183 |
+
#depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_BONE)
|
184 |
+
#depth_gray = cv2.cvtColor(depth_color, cv2.COLOR_RGBA2GRAY)
|
185 |
+
|
186 |
+
# Remove white border around map:
|
187 |
+
# define lower and upper limits of white
|
188 |
+
#white_lo = np.array([250,250,250])
|
189 |
+
#white_hi = np.array([255,255,255])
|
190 |
+
# mask image to only select white
|
191 |
+
mask = cv2.inRange(depth_gray[0:int(depth_gray.shape[0]/8*6.5)-1, 0:depth_gray.shape[1]], 250, 255)
|
192 |
+
# change image to black where we found white
|
193 |
+
depth_gray[0:int(depth_gray.shape[0]/8*6.5)-1, 0:depth_gray.shape[1]][mask>0] = 0
|
194 |
+
|
195 |
+
mask = cv2.inRange(depth_gray[int(depth_gray.shape[0]/8*6.5):depth_gray.shape[0], 0:depth_gray.shape[1]], 160, 255)
|
196 |
+
depth_gray[int(depth_gray.shape[0]/8*6.5):depth_gray.shape[0], 0:depth_gray.shape[1]][mask>0] = 160
|
197 |
+
|
198 |
+
depth_color = cv2.cvtColor(depth_gray, cv2.COLOR_GRAY2BGR)
|
199 |
+
# split_region = np.ones((frame_height, margin_width, 3), dtype=np.uint8) * 255
|
200 |
+
# combined_frame = cv2.hconcat([raw_frame, split_region, depth_color])
|
201 |
+
|
202 |
+
# out.write(combined_frame)
|
203 |
+
# frame_path = os.path.join(temp_frame_dir, f"frame_{count:05d}.png")
|
204 |
+
# cv2.imwrite(frame_path, combined_frame)
|
205 |
+
|
206 |
+
#raw_frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2BGRA)
|
207 |
+
#raw_frame[:, :, 3] = 255
|
208 |
+
|
209 |
+
if cframes < 16:
|
210 |
+
thumbnail = cv2.cvtColor(cv2.resize(raw_frame, (16,32)), cv2.COLOR_BGR2GRAY).flatten()
|
211 |
+
if len(thumbnail_old) > 0:
|
212 |
+
diff = thumbnail - thumbnail_old
|
213 |
+
#print(diff)
|
214 |
+
c = Counter(diff)
|
215 |
+
value, cc = c.most_common()[0]
|
216 |
+
if value == 0 and cc > int(16*32*0.8):
|
217 |
+
count += 1
|
218 |
+
continue
|
219 |
+
thumbnail_old = thumbnail
|
220 |
+
|
221 |
+
cv2.imwrite(f"f{count}.png", raw_frame)
|
222 |
+
orig_frames.append(f"f{count}.png")
|
223 |
+
|
224 |
+
cv2.imwrite(f"f{count}_dmap.png", depth_color)
|
225 |
+
depth_frames.append(f"f{count}_dmap.png")
|
226 |
+
|
227 |
+
cv2.imwrite(f"f{count}_mask.png", depth_gray)
|
228 |
+
masks.append(f"f{count}_mask.png")
|
229 |
+
count += 1
|
230 |
+
|
231 |
+
#final_vid = create_video(orig_frames, frame_rate, "orig")
|
232 |
+
final_vid = create_video(depth_frames, frame_rate, "depth")
|
233 |
+
|
234 |
+
final_zip = zip_files(orig_frames, depth_frames)
|
235 |
+
raw_video.release()
|
236 |
+
# out.release()
|
237 |
+
cv2.destroyAllWindows()
|
238 |
+
|
239 |
+
global gradient
|
240 |
+
global frame_selected
|
241 |
+
global depths
|
242 |
+
global frames
|
243 |
+
frames = orig_frames
|
244 |
+
depths = depth_frames
|
245 |
+
|
246 |
+
if depth_color.shape[0] == 2048: #height
|
247 |
+
gradient = cv2.imread('./gradient_large.png').astype(np.uint8)
|
248 |
+
elif depth_color.shape[0] == 1024:
|
249 |
+
gradient = cv2.imread('./gradient.png').astype(np.uint8)
|
250 |
+
else:
|
251 |
+
gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
|
252 |
+
|
253 |
+
return final_vid, final_zip, frames, masks[frame_selected], depths #output_path
|
254 |
+
|
255 |
+
def depth_edges_mask(depth):
|
256 |
+
"""Returns a mask of edges in the depth map.
|
257 |
+
Args:
|
258 |
+
depth: 2D numpy array of shape (H, W) with dtype float32.
|
259 |
+
Returns:
|
260 |
+
mask: 2D numpy array of shape (H, W) with dtype bool.
|
261 |
+
"""
|
262 |
+
# Compute the x and y gradients of the depth map.
|
263 |
+
depth_dx, depth_dy = np.gradient(depth)
|
264 |
+
# Compute the gradient magnitude.
|
265 |
+
depth_grad = np.sqrt(depth_dx ** 2 + depth_dy ** 2)
|
266 |
+
# Compute the edge mask.
|
267 |
+
mask = depth_grad > 0.05
|
268 |
+
return mask
|
269 |
+
|
270 |
+
def pano_depth_to_world_points(depth):
|
271 |
+
"""
|
272 |
+
360 depth to world points
|
273 |
+
given 2D depth is an equirectangular projection of a spherical image
|
274 |
+
Treat depth as radius
|
275 |
+
longitude : -pi to pi
|
276 |
+
latitude : -pi/2 to pi/2
|
277 |
+
"""
|
278 |
+
|
279 |
+
# Convert depth to radius
|
280 |
+
radius = (255 - depth.flatten())
|
281 |
+
|
282 |
+
lon = np.linspace(0, np.pi*2, depth.shape[1])
|
283 |
+
lat = np.linspace(0, np.pi, depth.shape[0])
|
284 |
+
lon, lat = np.meshgrid(lon, lat)
|
285 |
+
lon = lon.flatten()
|
286 |
+
lat = lat.flatten()
|
287 |
+
|
288 |
+
pts3d = [[0,0,0]]
|
289 |
+
uv = [[0,0]]
|
290 |
+
nl = [[0,0,0]]
|
291 |
+
for i in range(0, 1): #(0,2)
|
292 |
+
for j in range(0, 1): #(0,2)
|
293 |
+
#rnd_lon = (np.random.rand(depth.shape[0]*depth.shape[1]) - 0.5) / 8
|
294 |
+
#rnd_lat = (np.random.rand(depth.shape[0]*depth.shape[1]) - 0.5) / 8
|
295 |
+
d_lon = lon + i/2 * np.pi*2 / depth.shape[1]
|
296 |
+
d_lat = lat + j/2 * np.pi / depth.shape[0]
|
297 |
+
|
298 |
+
nx = np.cos(d_lon) * np.sin(d_lat)
|
299 |
+
ny = np.cos(d_lat)
|
300 |
+
nz = np.sin(d_lon) * np.sin(d_lat)
|
301 |
+
|
302 |
+
# Convert to cartesian coordinates
|
303 |
+
x = radius * nx
|
304 |
+
y = radius * ny
|
305 |
+
z = radius * nz
|
306 |
+
|
307 |
+
pts = np.stack([x, y, z], axis=1)
|
308 |
+
uvs = np.stack([lon/np.pi/2, lat/np.pi], axis=1)
|
309 |
+
nls = np.stack([-nx, -ny, -nz], axis=1)
|
310 |
+
|
311 |
+
pts3d = np.concatenate((pts3d, pts), axis=0)
|
312 |
+
uv = np.concatenate((uv, uvs), axis=0)
|
313 |
+
nl = np.concatenate((nl, nls), axis=0)
|
314 |
+
#print(f'i: {i}, j: {j}')
|
315 |
+
j = j+1
|
316 |
+
i = i+1
|
317 |
+
|
318 |
+
return [pts3d, uv, nl]
|
319 |
+
|
320 |
+
def rgb2gray(rgb):
|
321 |
+
return np.dot(rgb[...,:3], [0.333, 0.333, 0.333])
|
322 |
+
|
323 |
+
def get_mesh(image, depth, blur_data, loadall):
|
324 |
+
global depths
|
325 |
+
global pcolors
|
326 |
+
global frame_selected
|
327 |
+
global mesh
|
328 |
+
global mesh_n
|
329 |
+
global scene
|
330 |
+
if loadall == False:
|
331 |
+
mesh = []
|
332 |
+
mesh_n = []
|
333 |
+
fnum = frame_selected
|
334 |
+
|
335 |
+
#print(image[fnum][0])
|
336 |
+
#print(depth["composite"])
|
337 |
+
|
338 |
+
depthc = cv2.imread(depths[frame_selected], cv2.IMREAD_UNCHANGED).astype(np.uint8)
|
339 |
+
blur_img = blur_image(cv2.imread(image[fnum][0], cv2.IMREAD_UNCHANGED).astype(np.uint8), depthc, blur_data)
|
340 |
+
gdepth = cv2.cvtColor(depthc, cv2.COLOR_RGB2GRAY) #rgb2gray(depthc)
|
341 |
+
|
342 |
+
print('depth to gray - ok')
|
343 |
+
points = pano_depth_to_world_points(gdepth)
|
344 |
+
pts3d = points[0]
|
345 |
+
uv = points[1]
|
346 |
+
nl = points[2]
|
347 |
+
print('radius from depth - ok')
|
348 |
+
|
349 |
+
# Create a trimesh mesh from the points
|
350 |
+
# Each pixel is connected to its 4 neighbors
|
351 |
+
# colors are the RGB values of the image
|
352 |
+
uvs = uv.reshape(-1, 2)
|
353 |
+
#print(uvs)
|
354 |
+
#verts = pts3d.reshape(-1, 3)
|
355 |
+
verts = [[0,0,0]]
|
356 |
+
normals = nl.reshape(-1, 3)
|
357 |
+
rgba = cv2.cvtColor(blur_img, cv2.COLOR_RGB2RGBA)
|
358 |
+
colors = rgba.reshape(-1, 4)
|
359 |
+
clrs = [[128,128,128,0]]
|
360 |
+
|
361 |
+
#for i in range(0,1): #(0,4)
|
362 |
+
#clrs = np.concatenate((clrs, colors), axis=0)
|
363 |
+
#i = i+1
|
364 |
+
#verts, clrs
|
365 |
+
|
366 |
+
#pcd = o3d.geometry.TriangleMesh.create_tetrahedron()
|
367 |
+
#pcd.compute_vertex_normals()
|
368 |
+
#pcd.paint_uniform_color((1.0, 1.0, 1.0))
|
369 |
+
#mesh.append(pcd)
|
370 |
+
#print(mesh[len(mesh)-1])
|
371 |
+
if not str(fnum) in mesh_n:
|
372 |
+
mesh_n.append(str(fnum))
|
373 |
+
print('mesh - ok')
|
374 |
+
|
375 |
+
# Save as glb
|
376 |
+
glb_file = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
|
377 |
+
#o3d.io.write_triangle_mesh(glb_file.name, pcd)
|
378 |
+
print('file - ok')
|
379 |
+
return "./TriangleWithoutIndices.gltf", glb_file.name, ",".join(mesh_n)
|
380 |
+
|
381 |
+
def blur_image(image, depth, blur_data):
|
382 |
+
blur_a = blur_data.split()
|
383 |
+
print(f'blur data {blur_data}')
|
384 |
+
|
385 |
+
blur_frame = image.copy()
|
386 |
+
j = 0
|
387 |
+
while j < 256:
|
388 |
+
i = 255 - j
|
389 |
+
blur_lo = np.array([i,i,i])
|
390 |
+
blur_hi = np.array([i+1,i+1,i+1])
|
391 |
+
blur_mask = cv2.inRange(depth, blur_lo, blur_hi)
|
392 |
+
|
393 |
+
print(f'kernel size {int(blur_a[j])}')
|
394 |
+
blur = cv2.GaussianBlur(image, (int(blur_a[j]), int(blur_a[j])), 0)
|
395 |
+
|
396 |
+
blur_frame[blur_mask>0] = blur[blur_mask>0]
|
397 |
+
j = j + 1
|
398 |
+
|
399 |
+
return blur_frame
|
400 |
+
|
401 |
+
def loadfile(f):
|
402 |
+
return f
|
403 |
+
|
404 |
+
def show_json(txt):
|
405 |
+
data = json.loads(txt)
|
406 |
+
print(txt)
|
407 |
+
i=0
|
408 |
+
while i < len(data[2]):
|
409 |
+
data[2][i] = data[2][i]["image"]["path"]
|
410 |
+
data[4][i] = data[4][i]["path"]
|
411 |
+
i=i+1
|
412 |
+
return data[0]["video"]["path"], data[1]["path"], data[2], data[3]["background"]["path"], data[4], data[5]
|
413 |
+
|
414 |
+
|
415 |
+
def select_frame(d, evt: gr.SelectData):
|
416 |
+
global dcolor
|
417 |
+
global frame_selected
|
418 |
+
global masks
|
419 |
+
global edge
|
420 |
+
|
421 |
+
if evt.index != frame_selected:
|
422 |
+
edge = []
|
423 |
+
mask = cv2.imread(depths[frame_selected]).astype(np.uint8)
|
424 |
+
cv2.imwrite(masks[frame_selected], cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY))
|
425 |
+
frame_selected = evt.index
|
426 |
+
|
427 |
+
if len(dcolor) == 0:
|
428 |
+
bg = [127, 127, 127, 255]
|
429 |
+
else:
|
430 |
+
bg = "[" + str(dcolor[frame_selected])[1:-1] + ", 255]"
|
431 |
+
|
432 |
+
return masks[frame_selected], frame_selected, bg
|
433 |
+
|
434 |
+
def switch_rows(v):
|
435 |
+
global frames
|
436 |
+
global depths
|
437 |
+
if v == True:
|
438 |
+
print(depths[0])
|
439 |
+
return depths
|
440 |
+
else:
|
441 |
+
print(frames[0])
|
442 |
+
return frames
|
443 |
+
|
444 |
+
def optimize(v, d):
|
445 |
+
global pcolors
|
446 |
+
global dcolor
|
447 |
+
global frame_selected
|
448 |
+
global frames
|
449 |
+
global depths
|
450 |
+
|
451 |
+
if v == True:
|
452 |
+
ddepth = cv2.CV_16S
|
453 |
+
kernel_size = 3
|
454 |
+
l = 16
|
455 |
+
|
456 |
+
dcolor = []
|
457 |
+
for k, f in enumerate(frames):
|
458 |
+
frame = cv2.imread(frames[k]).astype(np.uint8)
|
459 |
+
|
460 |
+
# convert to np.float32
|
461 |
+
f = np.float32(frame.reshape((-1,3)))
|
462 |
+
# define criteria, number of clusters(K) and apply kmeans()
|
463 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 4, 1.0)
|
464 |
+
ret,label,center=cv2.kmeans(f,l,None,criteria,4,cv2.KMEANS_RANDOM_CENTERS)
|
465 |
+
# Now convert back into uint8, and make original image
|
466 |
+
center = np.uint8(center)
|
467 |
+
res = center[label.flatten()]
|
468 |
+
frame = res.reshape((frame.shape))
|
469 |
+
|
470 |
+
depth = cv2.imread(depths[k]).astype(np.uint8)
|
471 |
+
mask = cv2.cvtColor(depth, cv2.COLOR_RGB2GRAY)
|
472 |
+
dcolor.append(bincount(frame[mask==0]))
|
473 |
+
print(dcolor[k])
|
474 |
+
clrs = Image.fromarray(frame.astype(np.uint8)).convert('RGB').getcolors()
|
475 |
+
i=0
|
476 |
+
while i<len(clrs):
|
477 |
+
clrs[i] = list(clrs[i][1])
|
478 |
+
clrs[i].append(255)
|
479 |
+
i=i+1
|
480 |
+
print(clrs)
|
481 |
+
pcolors = clrs
|
482 |
+
|
483 |
+
#mask = cv2.convertScaleAbs(cv2.Laplacian(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY), ddepth, ksize=kernel_size))
|
484 |
+
#mask[mask>0] = 255
|
485 |
+
#frame[mask==0] = (0, 0, 0)
|
486 |
+
cv2.imwrite(frames[k], frame)
|
487 |
+
|
488 |
+
#depth[mask==0] = (255,255,255)
|
489 |
+
mask = cv2.inRange(frame, np.array([dcolor[k][0]-8, dcolor[k][1]-8, dcolor[k][2]-8]), np.array([dcolor[k][0]+8, dcolor[k][1]+8, dcolor[k][2]+8]))
|
490 |
+
depth[mask>0] = (255,255,255)
|
491 |
+
depth[depth.shape[0]-1:depth.shape[0], 0:depth.shape[1]] = (160, 160, 160)
|
492 |
+
depth[0:1, 0:depth.shape[1]] = (0, 0, 0)
|
493 |
+
cv2.imwrite(depths[k], depth)
|
494 |
+
|
495 |
+
if d == False:
|
496 |
+
return frames, "[" + str(dcolor[frame_selected])[1:-1] + ", 255]"
|
497 |
+
else:
|
498 |
+
return depths, "[" + str(dcolor[frame_selected])[1:-1] + ", 255]"
|
499 |
+
|
500 |
+
def bincount(a):
|
501 |
+
a2D = a.reshape(-1,a.shape[-1])
|
502 |
+
col_range = (256, 256, 256) # generically : a2D.max(0)+1
|
503 |
+
a1D = np.ravel_multi_index(a2D.T, col_range)
|
504 |
+
return list(reversed(np.unravel_index(np.bincount(a1D).argmax(), col_range)))
|
505 |
+
|
506 |
+
def reset_mask():
|
507 |
+
global frame_selected
|
508 |
+
global masks
|
509 |
+
global depths
|
510 |
+
global edge
|
511 |
+
|
512 |
+
edge = []
|
513 |
+
mask = cv2.imread(depths[frame_selected]).astype(np.uint8)
|
514 |
+
cv2.imwrite(masks[frame_selected], cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY))
|
515 |
+
return masks[frame_selected], depths
|
516 |
+
|
517 |
+
def apply_mask(d, b):
|
518 |
+
global frames
|
519 |
+
global frame_selected
|
520 |
+
global masks
|
521 |
+
global depths
|
522 |
+
global edge
|
523 |
+
|
524 |
+
edge = []
|
525 |
+
mask = cv2.cvtColor(d["layers"][0], cv2.COLOR_RGBA2GRAY)
|
526 |
+
mask[mask<255] = 0
|
527 |
+
b = b*2+1
|
528 |
+
dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
|
529 |
+
mask = cv2.dilate(mask, dilation)
|
530 |
+
mask_b = cv2.GaussianBlur(mask, (b,b), 0)
|
531 |
+
b = b*2+1
|
532 |
+
dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
|
533 |
+
dmask = cv2.dilate(mask, dilation)
|
534 |
+
dmask_b = cv2.GaussianBlur(dmask, (b,b), 0)
|
535 |
+
|
536 |
+
for k, mk in enumerate(masks):
|
537 |
+
if k != frame_selected and k < len(depths):
|
538 |
+
cv2.imwrite(masks[k], dmask)
|
539 |
+
frame = cv2.imread(frames[k], cv2.IMREAD_UNCHANGED).astype(np.uint8)
|
540 |
+
frame[:, :, 3] = dmask_b
|
541 |
+
cv2.imwrite(frames[k], frame)
|
542 |
+
|
543 |
+
frame = cv2.imread(frames[frame_selected], cv2.IMREAD_UNCHANGED).astype(np.uint8)
|
544 |
+
frame[:, :, 3] = 255 - mask_b
|
545 |
+
cv2.imwrite(frames[frame_selected], frame)
|
546 |
+
|
547 |
+
cv2.imwrite(masks[frame_selected], mask) #d["background"]
|
548 |
+
return masks[frame_selected], depths, frames
|
549 |
+
|
550 |
+
def draw_mask(l, t, v, d, evt: gr.EventData):
|
551 |
+
global depths
|
552 |
+
global params
|
553 |
+
global frame_selected
|
554 |
+
global masks
|
555 |
+
global gradient
|
556 |
+
global edge
|
557 |
+
|
558 |
+
points = json.loads(v)
|
559 |
+
pts = np.array(points, np.int32)
|
560 |
+
pts = pts.reshape((-1,1,2))
|
561 |
+
|
562 |
+
if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l:
|
563 |
+
if len(edge) > 0:
|
564 |
+
d["background"] = cv2.imread(depths[frame_selected]).astype(np.uint8)
|
565 |
+
|
566 |
+
if d["background"].shape[0] == 2048: #height
|
567 |
+
gradient = cv2.imread('./gradient_large.png').astype(np.uint8)
|
568 |
+
elif d["background"].shape[0] == 1024:
|
569 |
+
gradient = cv2.imread('./gradient.png').astype(np.uint8)
|
570 |
+
else:
|
571 |
+
gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
|
572 |
+
|
573 |
+
bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY)
|
574 |
+
|
575 |
+
diff = np.abs(bg.astype(np.int16)-cv2.cvtColor(gradient, cv2.COLOR_RGBA2GRAY).astype(np.int16)).astype(np.uint8)
|
576 |
+
mask = cv2.inRange(diff, 0, t)
|
577 |
+
#kernel = np.ones((c,c),np.float32)/(c*c)
|
578 |
+
#mask = cv2.filter2D(mask,-1,kernel)
|
579 |
+
dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15-(t*2+1), 15-(t*2+1)), (t, t))
|
580 |
+
mask = cv2.dilate(mask, dilation)
|
581 |
+
|
582 |
+
#indices = np.arange(0,256) # List of all colors
|
583 |
+
#divider = np.linspace(0,255,l+1)[1] # we get a divider
|
584 |
+
#quantiz = np.intp(np.linspace(0,255,l)) # we get quantization colors
|
585 |
+
#color_levels = np.clip(np.intp(indices/divider),0,l-1) # color levels 0,1,2..
|
586 |
+
#palette = quantiz[color_levels]
|
587 |
+
|
588 |
+
#for i in range(l):
|
589 |
+
# bg[(bg >= i*255/l) & (bg < (i+1)*255/l)] = i*255/(l-1)
|
590 |
+
#bg = cv2.convertScaleAbs(palette[bg]).astype(np.uint8) # Converting image back to uint
|
591 |
+
|
592 |
+
res = np.float32(bg.reshape((-1,1)))
|
593 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 4, 1.0)
|
594 |
+
ret,label,center=cv2.kmeans(res,l,None,criteria,4,cv2.KMEANS_PP_CENTERS)
|
595 |
+
center = np.uint8(center)
|
596 |
+
res = center[label.flatten()]
|
597 |
+
bg = res.reshape((bg.shape))
|
598 |
+
|
599 |
+
bg[mask>0] = 0
|
600 |
+
bg[bg==255] = 0
|
601 |
+
|
602 |
+
params["fnum"] = frame_selected
|
603 |
+
params["l"] = l
|
604 |
+
|
605 |
+
d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_GRAY2RGBA)
|
606 |
+
edge = bg.copy()
|
607 |
+
else:
|
608 |
+
bg = edge.copy()
|
609 |
+
|
610 |
+
x = points[len(points)-1][0]
|
611 |
+
y = points[len(points)-1][1]
|
612 |
+
|
613 |
+
#int(t*256/l)
|
614 |
+
mask = cv2.floodFill(bg, None, (x, y), 1, 0, 256, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
|
615 |
+
# 255 << 8 tells to fill with the value 255)
|
616 |
+
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
617 |
+
|
618 |
+
d["layers"][0][mask>0] = (255,255,255,255)
|
619 |
+
|
620 |
+
return gr.ImageEditor(value=d)
|
621 |
+
|
622 |
+
|
623 |
+
def findNormals(format):
|
624 |
+
global depths
|
625 |
+
d_im = cv2.cvtColor(cv2.imread(depths[frame_selected]).astype(np.uint8), cv2.COLOR_BGR2GRAY)
|
626 |
+
zy, zx = np.gradient(d_im)
|
627 |
+
# You may also consider using Sobel to get a joint Gaussian smoothing and differentation
|
628 |
+
# to reduce noise
|
629 |
+
#zx = cv2.Sobel(d_im, cv2.CV_64F, 1, 0, ksize=5)
|
630 |
+
#zy = cv2.Sobel(d_im, cv2.CV_64F, 0, 1, ksize=5)
|
631 |
+
|
632 |
+
if format == "opengl":
|
633 |
+
zy = -zy
|
634 |
+
|
635 |
+
normal = np.dstack((np.ones_like(d_im), -zy, -zx))
|
636 |
+
n = np.linalg.norm(normal, axis=2)
|
637 |
+
normal[:, :, 0] /= n
|
638 |
+
normal[:, :, 1] /= n
|
639 |
+
normal[:, :, 2] /= n
|
640 |
+
|
641 |
+
# offset and rescale values to be in 0-255
|
642 |
+
normal += 1
|
643 |
+
normal /= 2
|
644 |
+
normal *= 255
|
645 |
+
|
646 |
+
return (normal[:, :, ::-1]).astype(np.uint8)
|
647 |
+
|
648 |
+
|
649 |
+
load_model="""
|
650 |
+
async(c, o, b, p, d, n, m)=>{
|
651 |
+
var intv = setInterval(function(){
|
652 |
+
if (document.getElementById("iframe3D")===null || typeof document.getElementById("iframe3D")==="undefined") {
|
653 |
+
try {
|
654 |
+
if (typeof BABYLON !== "undefined" && BABYLON.Engine && BABYLON.Engine.LastCreatedScene) {
|
655 |
+
BABYLON.Engine.LastCreatedScene.onAfterRenderObservable.add(function() { //onDataLoadedObservable
|
656 |
+
|
657 |
+
var then = new Date().getTime();
|
658 |
+
var now, delta;
|
659 |
+
const interval = 1000 / 25;
|
660 |
+
const tolerance = 0.1;
|
661 |
+
BABYLON.Engine.LastCreatedScene.getEngine().stopRenderLoop();
|
662 |
+
BABYLON.Engine.LastCreatedScene.getEngine().runRenderLoop(function () {
|
663 |
+
now = new Date().getTime();
|
664 |
+
delta = now - then;
|
665 |
+
then = now - (delta % interval);
|
666 |
+
if (delta >= interval - tolerance) {
|
667 |
+
BABYLON.Engine.LastCreatedScene.render();
|
668 |
+
}
|
669 |
+
});
|
670 |
+
|
671 |
+
var bg = JSON.parse(document.getElementById("bgcolor").getElementsByTagName("textarea")[0].value);
|
672 |
+
BABYLON.Engine.LastCreatedScene.getEngine().setHardwareScalingLevel(1.0);
|
673 |
+
for (var i=0; i<bg.length; i++) {
|
674 |
+
bg[i] /= 255;
|
675 |
+
}
|
676 |
+
BABYLON.Engine.LastCreatedScene.clearColor = new BABYLON.Color4(bg[0], bg[1], bg[2], bg[3]);
|
677 |
+
BABYLON.Engine.LastCreatedScene.ambientColor = new BABYLON.Color4(255,255,255,255);
|
678 |
+
//BABYLON.Engine.LastCreatedScene.autoClear = false;
|
679 |
+
//BABYLON.Engine.LastCreatedScene.autoClearDepthAndStencil = false;
|
680 |
+
for (var i=0; i<BABYLON.Engine.LastCreatedScene.getNodes().length; i++) {
|
681 |
+
if (BABYLON.Engine.LastCreatedScene.getNodes()[i].material) {
|
682 |
+
BABYLON.Engine.LastCreatedScene.getNodes()[i].material.pointSize = Math.ceil(Math.log2(Math.PI/document.getElementById("zoom").value));
|
683 |
+
}
|
684 |
+
}
|
685 |
+
BABYLON.Engine.LastCreatedScene.getAnimationRatio();
|
686 |
+
//BABYLON.Engine.LastCreatedScene.activeCamera.inertia = 0.0;
|
687 |
+
});
|
688 |
+
if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
|
689 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata = {
|
690 |
+
pipeline: new BABYLON.DefaultRenderingPipeline("default", true, BABYLON.Engine.LastCreatedScene, [BABYLON.Engine.LastCreatedScene.activeCamera])
|
691 |
+
}
|
692 |
+
}
|
693 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.samples = 4;
|
694 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast = 1.0;
|
695 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure = 1.0;
|
696 |
+
|
697 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.fov = document.getElementById("zoom").value;
|
698 |
+
|
699 |
+
document.getElementById("model3D").getElementsByTagName("canvas")[0].style.filter = "blur(" + Math.ceil(Math.log2(Math.PI/document.getElementById("zoom").value))/2.0*Math.sqrt(2.0) + "px)";
|
700 |
+
document.getElementById("model3D").getElementsByTagName("canvas")[0].oncontextmenu = function(e){e.preventDefault();}
|
701 |
+
document.getElementById("model3D").getElementsByTagName("canvas")[0].ondrag = function(e){e.preventDefault();}
|
702 |
+
|
703 |
+
if (o.indexOf(""+n) < 0) {
|
704 |
+
if (o != "") { o += ","; }
|
705 |
+
o += n;
|
706 |
+
}
|
707 |
+
//alert(o);
|
708 |
+
var o_ = o.split(",");
|
709 |
+
var q = BABYLON.Engine.LastCreatedScene.meshes;
|
710 |
+
for(i = 0; i < q.length; i++) {
|
711 |
+
let mesh = q[i];
|
712 |
+
mesh.dispose(false, true);
|
713 |
+
}
|
714 |
+
var dome = [];
|
715 |
+
for (var j=0; j<o_.length; j++) {
|
716 |
+
o_[j] = parseInt(o_[j]);
|
717 |
+
dome[j] = new BABYLON.PhotoDome("dome"+j, p[o_[j]].image.url,
|
718 |
+
{
|
719 |
+
resolution: 16,
|
720 |
+
size: 512
|
721 |
+
}, BABYLON.Engine.LastCreatedScene);
|
722 |
+
var q = BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-2]._children;
|
723 |
+
for(i = 0; i < q.length; i++) {
|
724 |
+
let mesh = q[i];
|
725 |
+
mesh.dispose(false, true);
|
726 |
+
}
|
727 |
+
//BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.needDepthPrePass = true;
|
728 |
+
//BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].scaling.z = -1;
|
729 |
+
BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].alphaIndex = o_.length-j;
|
730 |
+
BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.diffuseTexture.hasAlpha = true;
|
731 |
+
BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.useAlphaFromDiffuseTexture = true;
|
732 |
+
BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].applyDisplacementMap(m[o_[j]].url, 0, 255, function(m){try{alert(BABYLON.Engine.Version);}catch(e){alert(e);}}, null, null, true, function(e){alert(e);});
|
733 |
+
}
|
734 |
+
clearInterval(intv);
|
735 |
+
}
|
736 |
+
} catch(e) {alert(e);}
|
737 |
+
} else if (BABYLON || BABYLON == null) {
|
738 |
+
try {
|
739 |
+
BABYLON = null;
|
740 |
+
if (document.getElementById("model3D").getElementsByTagName("canvas")[0]) {
|
741 |
+
document.getElementById("model3D").getElementsByTagName("canvas")[0].remove();
|
742 |
+
}
|
743 |
+
document.getElementById("iframe3D").src = "index.htm";
|
744 |
+
document.getElementById("iframe3D").onload = function() {
|
745 |
+
if (o.indexOf(""+n) < 0) {
|
746 |
+
if (o != "") { o += ","; }
|
747 |
+
o += n;
|
748 |
+
}
|
749 |
+
alert(o);
|
750 |
+
var o_ = o.split(",");
|
751 |
+
document.getElementById("iframe3D").contentDocument.getElementById("coords").value = c;
|
752 |
+
document.getElementById("iframe3D").contentDocument.getElementById("order").value = o;
|
753 |
+
document.getElementById("iframe3D").contentDocument.getElementById("bgcolor").value = b;
|
754 |
+
document.getElementById("iframe3D").contentDocument.getElementById("bgimage").value = "";
|
755 |
+
document.getElementById("iframe3D").contentDocument.getElementById("bgdepth").value = "";
|
756 |
+
for (var j=0; j<o_.length; j++) {
|
757 |
+
o_[j] = parseInt(o_[j]);
|
758 |
+
alert(o_[j]);
|
759 |
+
document.getElementById("iframe3D").contentDocument.getElementById("bgimage").value += p[o_[j]].image.url + ",";
|
760 |
+
document.getElementById("iframe3D").contentDocument.getElementById("bgdepth").value += m[o_[j]].url + ",";
|
761 |
+
}
|
762 |
+
}
|
763 |
+
toggleDisplay("model");
|
764 |
+
|
765 |
+
clearInterval(intv);
|
766 |
+
} catch(e) {alert(e)}
|
767 |
+
}
|
768 |
+
}, 40);
|
769 |
+
}
|
770 |
+
"""
|
771 |
+
|
772 |
+
js = """
|
773 |
+
async()=>{
|
774 |
+
console.log('Hi');
|
775 |
+
|
776 |
+
const chart = document.getElementById('chart');
|
777 |
+
const blur_in = document.getElementById('blur_in').getElementsByTagName('textarea')[0];
|
778 |
+
var md = false;
|
779 |
+
var xold = 128;
|
780 |
+
var yold = 32;
|
781 |
+
var a = new Array(256);
|
782 |
+
var l;
|
783 |
+
|
784 |
+
for (var i=0; i<256; i++) {
|
785 |
+
const hr = document.createElement('hr');
|
786 |
+
hr.style.backgroundColor = 'hsl(0,0%,' + (100-i/256*100) + '%)';
|
787 |
+
chart.appendChild(hr);
|
788 |
+
}
|
789 |
+
|
790 |
+
function resetLine() {
|
791 |
+
a.fill(1);
|
792 |
+
for (var i=0; i<256; i++) {
|
793 |
+
chart.childNodes[i].style.height = a[i] + 'px';
|
794 |
+
chart.childNodes[i].style.marginTop = '32px';
|
795 |
+
}
|
796 |
+
}
|
797 |
+
resetLine();
|
798 |
+
window.resetLine = resetLine;
|
799 |
+
|
800 |
+
function pointerDown(x, y) {
|
801 |
+
md = true;
|
802 |
+
xold = parseInt(x - chart.getBoundingClientRect().x);
|
803 |
+
yold = parseInt(y - chart.getBoundingClientRect().y);
|
804 |
+
chart.title = xold + ',' + yold;
|
805 |
+
}
|
806 |
+
window.pointerDown = pointerDown;
|
807 |
+
|
808 |
+
function pointerUp() {
|
809 |
+
md = false;
|
810 |
+
var evt = document.createEvent('Event');
|
811 |
+
evt.initEvent('input', true, false);
|
812 |
+
blur_in.dispatchEvent(evt);
|
813 |
+
chart.title = '';
|
814 |
+
}
|
815 |
+
window.pointerUp = pointerUp;
|
816 |
+
|
817 |
+
function lerp(y1, y2, mu) { return y1*(1-mu)+y2*mu; }
|
818 |
+
|
819 |
+
function drawLine(x, y) {
|
820 |
+
x = parseInt(x - chart.getBoundingClientRect().x);
|
821 |
+
y = parseInt(y - chart.getBoundingClientRect().y);
|
822 |
+
if (md === true && y >= 0 && y < 64 && x >= 0 && x < 256) {
|
823 |
+
if (y < 32) {
|
824 |
+
a[x] = Math.abs(32-y)*2 + 1;
|
825 |
+
chart.childNodes[x].style.height = a[x] + 'px';
|
826 |
+
chart.childNodes[x].style.marginTop = y + 'px';
|
827 |
+
|
828 |
+
for (var i=Math.min(xold, x)+1; i<Math.max(xold, x); i++) {
|
829 |
+
l = parseInt(lerp( yold, y, (i-xold)/(x-xold) ));
|
830 |
+
|
831 |
+
if (l < 32) {
|
832 |
+
a[i] = Math.abs(32-l)*2 + 1;
|
833 |
+
chart.childNodes[i].style.height = a[i] + 'px';
|
834 |
+
chart.childNodes[i].style.marginTop = l + 'px';
|
835 |
+
} else if (l < 64) {
|
836 |
+
a[i] = Math.abs(l-32)*2 + 1;
|
837 |
+
chart.childNodes[i].style.height = a[i] + 'px';
|
838 |
+
chart.childNodes[i].style.marginTop = (64-l) + 'px';
|
839 |
+
}
|
840 |
+
}
|
841 |
+
} else if (y < 64) {
|
842 |
+
a[x] = Math.abs(y-32)*2 + 1;
|
843 |
+
chart.childNodes[x].style.height = a[x] + 'px';
|
844 |
+
chart.childNodes[x].style.marginTop = (64-y) + 'px';
|
845 |
+
|
846 |
+
for (var i=Math.min(xold, x)+1; i<Math.max(xold, x); i++) {
|
847 |
+
l = parseInt(lerp( yold, y, (i-xold)/(x-xold) ));
|
848 |
+
|
849 |
+
if (l < 32) {
|
850 |
+
a[i] = Math.abs(32-l)*2 + 1;
|
851 |
+
chart.childNodes[i].style.height = a[i] + 'px';
|
852 |
+
chart.childNodes[i].style.marginTop = l + 'px';
|
853 |
+
} else if (l < 64) {
|
854 |
+
a[i] = Math.abs(l-32)*2 + 1;
|
855 |
+
chart.childNodes[i].style.height = a[i] + 'px';
|
856 |
+
chart.childNodes[i].style.marginTop = (64-l) + 'px';
|
857 |
+
}
|
858 |
+
}
|
859 |
+
}
|
860 |
+
blur_in.value = a.join(' ');
|
861 |
+
xold = x;
|
862 |
+
yold = y;
|
863 |
+
chart.title = xold + ',' + yold;
|
864 |
+
}
|
865 |
+
}
|
866 |
+
window.drawLine = drawLine;
|
867 |
+
|
868 |
+
}
|
869 |
+
"""
|
870 |
+
|
871 |
+
css = """
|
872 |
+
#img-display-container {
|
873 |
+
max-height: 100vh;
|
874 |
+
}
|
875 |
+
#img-display-input {
|
876 |
+
max-height: 80vh;
|
877 |
+
}
|
878 |
+
#img-display-output {
|
879 |
+
max-height: 80vh;
|
880 |
+
}
|
881 |
+
"""
|
882 |
+
|
883 |
+
title = "# Depth Anything V2 Video"
|
884 |
+
description = """**Depth Anything V2** on full video files.
|
885 |
+
Please refer to our [paper](https://arxiv.org/abs/2406.09414), [project page](https://depth-anything-v2.github.io), and [github](https://github.com/DepthAnything/Depth-Anything-V2) for more details."""
|
886 |
+
|
887 |
+
|
888 |
+
#transform = Compose([
|
889 |
+
# Resize(
|
890 |
+
# width=518,
|
891 |
+
# height=518,
|
892 |
+
# resize_target=False,
|
893 |
+
# keep_aspect_ratio=True,
|
894 |
+
# ensure_multiple_of=14,
|
895 |
+
# resize_method='lower_bound',
|
896 |
+
# image_interpolation_method=cv2.INTER_CUBIC,
|
897 |
+
# ),
|
898 |
+
# NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
899 |
+
# PrepareForNet(),
|
900 |
+
#])
|
901 |
+
|
902 |
+
# @torch.no_grad()
|
903 |
+
# def predict_depth(model, image):
|
904 |
+
# return model(image)
|
905 |
+
|
906 |
+
with gr.Blocks(css=css, js=js) as demo:
|
907 |
+
gr.Markdown(title)
|
908 |
+
gr.Markdown(description)
|
909 |
+
gr.Markdown("### Video Depth Prediction demo")
|
910 |
+
|
911 |
+
with gr.Row():
|
912 |
+
with gr.Column():
|
913 |
+
input_json = gr.Textbox(elem_id="json_in", value="{}", label="JSON", interactive=False)
|
914 |
+
input_url = gr.Textbox(elem_id="url_in", value="./examples/streetview.mp4", label="URL")
|
915 |
+
input_video = gr.Video(label="Input Video", format="mp4")
|
916 |
+
input_url.input(fn=loadfile, inputs=[input_url], outputs=[input_video])
|
917 |
+
submit = gr.Button("Submit")
|
918 |
+
output_frame = gr.Gallery(label="Frames", preview=True, columns=8192, interactive=False)
|
919 |
+
output_switch = gr.Checkbox(label="Show depths")
|
920 |
+
with gr.Accordion(label="Depths", open=False):
|
921 |
+
output_depth = gr.Files(label="Depth files", interactive=False)
|
922 |
+
output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
|
923 |
+
optimize_switch = gr.Checkbox(label="Optimize")
|
924 |
+
bgcolor = gr.Textbox(elem_id="bgcolor", value="[127, 127, 127, 255]", label="Background color", interactive=False)
|
925 |
+
optimize_switch.input(fn=optimize, inputs=[optimize_switch, output_switch], outputs=[output_frame, bgcolor])
|
926 |
+
output_mask = gr.ImageEditor(layers=False, sources=('upload', 'clipboard'), show_download_button=True, type="numpy", interactive=True, transforms=(None,), eraser=gr.Eraser(), brush=gr.Brush(default_size=0, colors=['black', '#505050', '#a0a0a0', 'white']), elem_id="image_edit")
|
927 |
+
with gr.Row():
|
928 |
+
selector = gr.HTML(value="""
|
929 |
+
<a href='#' id='selector' onclick='if (this.style.fontWeight!=\"bold\") {
|
930 |
+
this.style.fontWeight=\"bold\";
|
931 |
+
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
|
932 |
+
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].ondrag = function(e){e.preventDefault();}
|
933 |
+
|
934 |
+
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onclick = function(e) {
|
935 |
+
var x = parseInt((e.clientX-e.target.getBoundingClientRect().x)*e.target.width/e.target.getBoundingClientRect().width);
|
936 |
+
var y = parseInt((e.clientY-e.target.getBoundingClientRect().y)*e.target.height/e.target.getBoundingClientRect().height);
|
937 |
+
|
938 |
+
var p = document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value.slice(1, -1);
|
939 |
+
if (p != \"\") { p += \", \"; }
|
940 |
+
p += \"[\" + x + \", \" + y + \"]\";
|
941 |
+
document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[\" + p + \"]\";
|
942 |
+
|
943 |
+
var evt = document.createEvent(\"Event\");
|
944 |
+
evt.initEvent(\"input\", true, false);
|
945 |
+
document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
|
946 |
+
}
|
947 |
+
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerdown = function(e) {
|
948 |
+
|
949 |
+
document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].style.borderColor = \"#a0a0a0\";
|
950 |
+
|
951 |
+
}
|
952 |
+
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerup = function(e) {
|
953 |
+
|
954 |
+
document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].style.borderColor = \"#ffffff\";
|
955 |
+
|
956 |
+
}
|
957 |
+
} else {
|
958 |
+
this.style.fontWeight=\"normal\";
|
959 |
+
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onclick = null;
|
960 |
+
|
961 |
+
}' title='Select point' style='text-decoration:none;color:white;'>⊹ Select point</a> <a href='#' id='clear_select' onclick='
|
962 |
+
|
963 |
+
document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[]\";
|
964 |
+
|
965 |
+
' title='Clear selection' style='text-decoration:none;color:white;'>✕ Clear</a>""")
|
966 |
+
apply = gr.Button("Apply", size='sm')
|
967 |
+
reset = gr.Button("Reset", size='sm')
|
968 |
+
with gr.Accordion(label="Edge", open=False):
|
969 |
+
levels = gr.Slider(label="Color levels", value=16, maximum=32, minimum=2, step=1)
|
970 |
+
tolerance = gr.Slider(label="Tolerance", value=1, maximum=7, minimum=0, step=1)
|
971 |
+
bsize = gr.Slider(label="Border size", value=15, maximum=256, minimum=1, step=2)
|
972 |
+
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
973 |
+
mouse.input(fn=draw_mask, show_progress="minimal", inputs=[levels, tolerance, mouse, output_mask], outputs=[output_mask])
|
974 |
+
apply.click(fn=apply_mask, inputs=[output_mask, bsize], outputs=[output_mask, output_depth, output_frame])
|
975 |
+
reset.click(fn=reset_mask, inputs=None, outputs=[output_mask, output_depth])
|
976 |
+
|
977 |
+
normals_out = gr.Image(label="Normal map", interactive=False)
|
978 |
+
format_normals = gr.Radio(choices=["directx", "opengl"])
|
979 |
+
find_normals = gr.Button("Find normals")
|
980 |
+
find_normals.click(fn=findNormals, inputs=[format_normals], outputs=[normals_out])
|
981 |
+
|
982 |
+
with gr.Column():
|
983 |
+
model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl"), ("giant", "vitg")], type="value", value="vits", label='Model Type')
|
984 |
+
processed_video = gr.Video(label="Output Video", format="mp4", interactive=False)
|
985 |
+
processed_zip = gr.File(label="Output Archive", interactive=False)
|
986 |
+
result = gr.Model3D(label="3D Mesh", clear_color=[0.5, 0.5, 0.5, 0.0], camera_position=[0, 90, 0], zoom_speed=2.0, pan_speed=2.0, interactive=True, elem_id="model3D") #, display_mode="point_cloud"
|
987 |
+
chart_c = gr.HTML(elem_id="chart_c", value="""<div id='chart' onpointermove='window.drawLine(event.clientX, event.clientY);' onpointerdown='window.pointerDown(event.clientX, event.clientY);' onpointerup='window.pointerUp();' onpointerleave='window.pointerUp();' onpointercancel='window.pointerUp();' onclick='window.resetLine();'></div>
|
988 |
+
<style>
|
989 |
+
body {
|
990 |
+
user-select: none;
|
991 |
+
}
|
992 |
+
#chart hr {
|
993 |
+
width: 1px;
|
994 |
+
height: 1px;
|
995 |
+
clear: none;
|
996 |
+
border: 0;
|
997 |
+
padding:0;
|
998 |
+
display: inline-block;
|
999 |
+
position: relative;
|
1000 |
+
vertical-align: top;
|
1001 |
+
margin-top:32px;
|
1002 |
+
}
|
1003 |
+
#chart {
|
1004 |
+
padding:0;
|
1005 |
+
margin:0;
|
1006 |
+
width:256px;
|
1007 |
+
height:64px;
|
1008 |
+
background-color:#808080;
|
1009 |
+
touch-action: none;
|
1010 |
+
}
|
1011 |
+
</style>
|
1012 |
+
""")
|
1013 |
+
average = gr.HTML(value="""<label for='average'>Average</label><input id='average' type='range' style='width:256px;height:1em;' value='1' min='1' max='15' step='2' onclick='
|
1014 |
+
var pts_a = document.getElementById(\"blur_in\").getElementsByTagName(\"textarea\")[0].value.split(\" \");
|
1015 |
+
for (var i=0; i<256; i++) {
|
1016 |
+
var avg = 0;
|
1017 |
+
var div = this.value;
|
1018 |
+
for (var j = i-parseInt(this.value/2); j <= i+parseInt(this.value/2); j++) {
|
1019 |
+
if (pts_a[j]) {
|
1020 |
+
avg += parseInt(pts_a[j]);
|
1021 |
+
} else if (div > 1) {
|
1022 |
+
div--;
|
1023 |
+
}
|
1024 |
+
}
|
1025 |
+
pts_a[i] = Math.round((avg / div - 1) / 2) * 2 + 1;
|
1026 |
+
|
1027 |
+
document.getElementById(\"chart\").childNodes[i].style.height = pts_a[i] + \"px\";
|
1028 |
+
document.getElementById(\"chart\").childNodes[i].style.marginTop = (64-pts_a[i])/2 + \"px\";
|
1029 |
+
}
|
1030 |
+
document.getElementById(\"blur_in\").getElementsByTagName(\"textarea\")[0].value = pts_a.join(\" \");
|
1031 |
+
|
1032 |
+
var evt = document.createEvent(\"Event\");
|
1033 |
+
evt.initEvent(\"input\", true, false);
|
1034 |
+
document.getElementById(\"blur_in\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
|
1035 |
+
' oninput='
|
1036 |
+
this.parentNode.childNodes[2].innerText = this.value;
|
1037 |
+
' onchange='this.click();'/><span>1</span>""")
|
1038 |
+
with gr.Accordion(label="Blur levels", open=False):
|
1039 |
+
blur_in = gr.Textbox(elem_id="blur_in", label="Kernel size", show_label=False, interactive=False, value="1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1")
|
1040 |
+
with gr.Accordion(label="Locations", open=False):
|
1041 |
+
selected = gr.Number(elem_id="fnum", value=0, minimum=0, maximum=256, interactive=False)
|
1042 |
+
output_frame.select(fn=select_frame, inputs=[output_mask], outputs=[output_mask, selected, bgcolor])
|
1043 |
+
example_coords = """[
|
1044 |
+
{"lat": 50.07379596793083, "lng": 14.437146122950555, "heading": 152.70303, "pitch": 2.607833999999997},
|
1045 |
+
{"lat": 50.073799567020004, "lng": 14.437146774240507, "heading": 151.12973, "pitch": 2.8672300000000064},
|
1046 |
+
{"lat": 50.07377647505558, "lng": 14.437161000659017, "heading": 151.41025, "pitch": 3.4802200000000028},
|
1047 |
+
{"lat": 50.07379496839027, "lng": 14.437148958238538, "heading": 151.93391, "pitch": 2.843050000000005},
|
1048 |
+
{"lat": 50.073823157821664, "lng": 14.437124189538856, "heading": 152.95769, "pitch": 4.233024999999998}
|
1049 |
+
]"""
|
1050 |
+
coords = gr.Textbox(elem_id="coords", value=example_coords, label="Coordinates", interactive=False)
|
1051 |
+
mesh_order = gr.Textbox(elem_id="order", value="", label="Order", interactive=False)
|
1052 |
+
|
1053 |
+
result_file = gr.File(elem_id="file3D", label="3D file", interactive=False)
|
1054 |
+
html = gr.HTML(value="""<label for='zoom'>Zoom</label><input id='zoom' type='range' style='width:256px;height:1em;' value='0.8' min='0.157' max='1.57' step='0.001' oninput='
|
1055 |
+
if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
|
1056 |
+
var evt = document.createEvent(\"Event\");
|
1057 |
+
evt.initEvent(\"click\", true, false);
|
1058 |
+
document.getElementById(\"reset_cam\").dispatchEvent(evt);
|
1059 |
+
}
|
1060 |
+
BABYLON.Engine.LastCreatedScene.getNodes()[parseInt(document.getElementById(\"fnum\").getElementsByTagName(\"input\")[0].value)+1].material.pointSize = Math.ceil(Math.log2(Math.PI/this.value));
|
1061 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.fov = this.value;
|
1062 |
+
this.parentNode.childNodes[2].innerText = BABYLON.Engine.LastCreatedScene.activeCamera.fov;
|
1063 |
+
|
1064 |
+
document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0].style.filter = \"blur(\" + BABYLON.Engine.LastCreatedScene.getNodes()[parseInt(document.getElementById(\"fnum\").getElementsByTagName(\"input\")[0].value)+1].material.pointSize/2.0*Math.sqrt(2.0) + \"px)\";
|
1065 |
+
'/><span>0.8</span>""")
|
1066 |
+
camera = gr.HTML(value="""<a href='#' id='reset_cam' onclick='
|
1067 |
+
if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
|
1068 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata = {
|
1069 |
+
screenshot: true,
|
1070 |
+
pipeline: new BABYLON.DefaultRenderingPipeline(\"default\", true, BABYLON.Engine.LastCreatedScene, [BABYLON.Engine.LastCreatedScene.activeCamera])
|
1071 |
+
}
|
1072 |
+
}
|
1073 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.radius = 0;
|
1074 |
+
BABYLON.Engine.LastCreatedScene.getNodes()[parseInt(document.getElementById(\"fnum\").getElementsByTagName(\"input\")[0].value)+1].material.pointSize = Math.ceil(Math.log2(Math.PI/document.getElementById(\"zoom\").value));
|
1075 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.samples = 4;
|
1076 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.fov = document.getElementById(\"zoom\").value;
|
1077 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast = document.getElementById(\"contrast\").value;
|
1078 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure = document.getElementById(\"exposure\").value;
|
1079 |
+
|
1080 |
+
document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0].style.filter = \"blur(\" + Math.ceil(Math.log2(Math.PI/document.getElementById(\"zoom\").value))/2.0*Math.sqrt(2.0) + \"px)\";
|
1081 |
+
document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
|
1082 |
+
document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0].ondrag = function(e){e.preventDefault();}
|
1083 |
+
'>reset camera</a>""")
|
1084 |
+
contrast = gr.HTML(value="""<label for='contrast'>Contrast</label><input id='contrast' type='range' style='width:256px;height:1em;' value='1.0' min='0' max='2' step='0.001' oninput='
|
1085 |
+
if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
|
1086 |
+
var evt = document.createEvent(\"Event\");
|
1087 |
+
evt.initEvent(\"click\", true, false);
|
1088 |
+
document.getElementById(\"reset_cam\").dispatchEvent(evt);
|
1089 |
+
}
|
1090 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast = this.value;
|
1091 |
+
this.parentNode.childNodes[2].innerText = BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast;
|
1092 |
+
'/><span>1.0</span>""")
|
1093 |
+
exposure = gr.HTML(value="""<label for='exposure'>Exposure</label><input id='exposure' type='range' style='width:256px;height:1em;' value='1.0' min='0' max='2' step='0.001' oninput='
|
1094 |
+
if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
|
1095 |
+
var evt = document.createEvent(\"Event\");
|
1096 |
+
evt.initEvent(\"click\", true, false);
|
1097 |
+
document.getElementById(\"reset_cam\").dispatchEvent(evt);
|
1098 |
+
}
|
1099 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure = this.value;
|
1100 |
+
this.parentNode.childNodes[2].innerText = BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure;
|
1101 |
+
'/><span>1.0</span>""")
|
1102 |
+
canvas = gr.HTML(value="""<a href='#' onclick='
|
1103 |
+
if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
|
1104 |
+
var evt = document.createEvent(\"Event\");
|
1105 |
+
evt.initEvent(\"click\", true, false);
|
1106 |
+
document.getElementById(\"reset_cam\").dispatchEvent(evt);
|
1107 |
+
}
|
1108 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.screenshot = true;
|
1109 |
+
|
1110 |
+
BABYLON.Engine.LastCreatedScene.getEngine().onEndFrameObservable.add(function() {
|
1111 |
+
if (BABYLON.Engine.LastCreatedScene.activeCamera.metadata.screenshot === true) {
|
1112 |
+
BABYLON.Engine.LastCreatedScene.activeCamera.metadata.screenshot = false;
|
1113 |
+
try {
|
1114 |
+
BABYLON.Tools.CreateScreenshotUsingRenderTarget(BABYLON.Engine.LastCreatedScene.getEngine(), BABYLON.Engine.LastCreatedScene.activeCamera,
|
1115 |
+
{ precision: 1.0 }, (durl) => {
|
1116 |
+
var cnvs = document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0]; //.getContext(\"webgl2\");
|
1117 |
+
var svgd = `<svg id=\"svg_out\" viewBox=\"0 0 ` + cnvs.width + ` ` + cnvs.height + `\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">
|
1118 |
+
<defs>
|
1119 |
+
<filter id=\"blur\" x=\"0\" y=\"0\" xmlns=\"http://www.w3.org/2000/svg\">
|
1120 |
+
<feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"` + BABYLON.Engine.LastCreatedScene.getNodes()[1].material.pointSize/2.0*Math.sqrt(2.0) + `\" />
|
1121 |
+
</filter>
|
1122 |
+
</defs>
|
1123 |
+
<image filter=\"url(#blur)\" id=\"svg_img\" x=\"0\" y=\"0\" width=\"` + cnvs.width + `\" height=\"` + cnvs.height + `\" xlink:href=\"` + durl + `\"/>
|
1124 |
+
</svg>`;
|
1125 |
+
document.getElementById(\"cnv_out\").width = cnvs.width;
|
1126 |
+
document.getElementById(\"cnv_out\").height = cnvs.height;
|
1127 |
+
document.getElementById(\"img_out\").src = \"data:image/svg+xml;base64,\" + btoa(svgd);
|
1128 |
+
}
|
1129 |
+
);
|
1130 |
+
} catch(e) { alert(e); }
|
1131 |
+
// https://forum.babylonjs.com/t/best-way-to-save-to-jpeg-snapshots-of-scene/17663/11
|
1132 |
+
}
|
1133 |
+
});
|
1134 |
+
'/>snapshot</a><br/><img src='' id='img_out' onload='
|
1135 |
+
var ctxt = document.getElementById(\"cnv_out\").getContext(\"2d\");
|
1136 |
+
ctxt.drawImage(this, 0, 0);
|
1137 |
+
'/><br/>
|
1138 |
+
<canvas id='cnv_out'/>""")
|
1139 |
+
load_all = gr.Checkbox(label="Load all")
|
1140 |
+
render = gr.Button("Render")
|
1141 |
+
input_json.input(show_json, inputs=[input_json], outputs=[processed_video, processed_zip, output_frame, output_mask, output_depth, coords])
|
1142 |
+
|
1143 |
+
def on_submit(uploaded_video,model_type,coordinates):
|
1144 |
+
global locations
|
1145 |
+
locations = []
|
1146 |
+
avg = [0, 0]
|
1147 |
+
|
1148 |
+
locations = json.loads(coordinates)
|
1149 |
+
for k, location in enumerate(locations):
|
1150 |
+
if "tiles" in locations[k]:
|
1151 |
+
locations[k]["heading"] = locations[k]["tiles"]["originHeading"]
|
1152 |
+
locations[k]["pitch"] = locations[k]["tiles"]["originPitch"]
|
1153 |
+
else:
|
1154 |
+
locations[k]["heading"] = 0
|
1155 |
+
locations[k]["pitch"] = 0
|
1156 |
+
|
1157 |
+
if "location" in locations[k]:
|
1158 |
+
locations[k] = locations[k]["location"]["latLng"]
|
1159 |
+
avg[0] = avg[0] + locations[k]["lat"]
|
1160 |
+
avg[1] = avg[1] + locations[k]["lng"]
|
1161 |
+
else:
|
1162 |
+
locations[k]["lat"] = 0
|
1163 |
+
locations[k]["lng"] = 0
|
1164 |
+
|
1165 |
+
if len(locations) > 0:
|
1166 |
+
avg[0] = avg[0] / len(locations)
|
1167 |
+
avg[1] = avg[1] / len(locations)
|
1168 |
+
|
1169 |
+
for k, location in enumerate(locations):
|
1170 |
+
lat = vincenty((location["lat"], 0), (avg[0], 0)) * 1000
|
1171 |
+
lng = vincenty((0, location["lng"]), (0, avg[1])) * 1000
|
1172 |
+
locations[k]["lat"] = float(lat / 2.5 * 95 * np.sign(location["lat"]-avg[0]))
|
1173 |
+
locations[k]["lng"] = float(lng / 2.5 * 95 * np.sign(location["lng"]-avg[1]))
|
1174 |
+
print(locations)
|
1175 |
+
|
1176 |
+
# Process the video and get the path of the output video
|
1177 |
+
output_video_path = make_video(uploaded_video,encoder=model_type)
|
1178 |
+
|
1179 |
+
return output_video_path + (json.dumps(locations),)
|
1180 |
+
|
1181 |
+
submit.click(on_submit, inputs=[input_video, model_type, coords], outputs=[processed_video, processed_zip, output_frame, output_mask, output_depth, coords])
|
1182 |
+
render.click(None, inputs=[coords, mesh_order, bgcolor, output_frame, output_mask, selected, output_depth], outputs=None, js=load_model)
|
1183 |
+
render.click(partial(get_mesh), inputs=[output_frame, output_mask, blur_in, load_all], outputs=[result, result_file, mesh_order])
|
1184 |
+
|
1185 |
+
example_files = [["./examples/streetview.mp4", "vits", example_coords]]
|
1186 |
+
examples = gr.Examples(examples=example_files, fn=on_submit, cache_examples=True, inputs=[input_video, model_type, coords], outputs=[processed_video, processed_zip, output_frame, output_mask, output_depth, coords])
|
1187 |
+
|
1188 |
+
|
1189 |
+
if __name__ == '__main__':
|
1190 |
+
demo.queue().launch()
|