freealise commited on
Commit
4707413
·
verified ·
1 Parent(s): 2270054

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -1431
app.py DELETED
@@ -1,1431 +0,0 @@
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
- import ffmpy
19
- from apng import APNG
20
-
21
- #from depth_anything.dpt import DepthAnything
22
- #from depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
23
- from huggingface_hub import hf_hub_download
24
- from depth_anything_v2.dpt import DepthAnythingV2
25
-
26
- DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
27
- model_configs = {
28
- 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
29
- 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
30
- 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
31
- 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
32
- }
33
- encoder2name = {
34
- 'vits': 'Small',
35
- 'vitb': 'Base',
36
- 'vitl': 'Large',
37
- 'vitg': 'Giant', # we are undergoing company review procedures to release our giant model checkpoint
38
- }
39
-
40
- blurin = "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"
41
- edge = []
42
- gradient = None
43
- params = { "fnum":0 }
44
- pcolors = []
45
- frame_selected = 0
46
- frames = []
47
- backups = []
48
- depths = []
49
- masks = []
50
- locations = []
51
- mesh = []
52
- mesh_n = []
53
- scene = None
54
-
55
- def zip_files(files_in, files_out):
56
- with ZipFile("depth_result.zip", "w") as zipObj:
57
- for idx, file in enumerate(files_in):
58
- zipObj.write(file, file.split("/")[-1])
59
- for idx, file in enumerate(files_out):
60
- zipObj.write(file, file.split("/")[-1])
61
- return "depth_result.zip"
62
-
63
- def create_alpha_video(frames, fps, type):
64
- print("building composite video result")
65
- imgs = []
66
- for j, img in enumerate(frames):
67
- imgs.append(cv2.imread(img).astype(np.uint8))
68
-
69
- APNG.from_files(frames, delay=1000).save(type + "_result.png")
70
- mediapy.write_video(type + "_result.webm", imgs, fps=fps, codec='vp9')
71
- return type + "_result.webm"
72
-
73
- def create_video(frames, fps, type):
74
- print("building video result")
75
- imgs = []
76
- for j, img in enumerate(frames):
77
- imgs.append(cv2.cvtColor(cv2.imread(img).astype(np.uint8), cv2.COLOR_BGR2RGB))
78
-
79
- mediapy.write_video(type + "_result.mp4", imgs, fps=fps)
80
- return type + "_result.mp4"
81
-
82
- @torch.no_grad()
83
- #@spaces.GPU
84
- def predict_depth(image, model):
85
- return model.infer_image(image)
86
-
87
- #def predict_depth(model, image):
88
- # return model(image)["depth"]
89
-
90
- def make_video(video_path, outdir='./vis_video_depth', encoder='vits', blur_data=blurin, o=1, b=32):
91
- if encoder not in ["vitl","vitb","vits","vitg"]:
92
- encoder = "vits"
93
-
94
- model_name = encoder2name[encoder]
95
- model = DepthAnythingV2(**model_configs[encoder])
96
- filepath = hf_hub_download(repo_id=f"depth-anything/Depth-Anything-V2-{model_name}", filename=f"depth_anything_v2_{encoder}.pth", repo_type="model")
97
- state_dict = torch.load(filepath, map_location="cpu")
98
- model.load_state_dict(state_dict)
99
- model = model.to(DEVICE).eval()
100
-
101
- #mapper = {"vits":"small","vitb":"base","vitl":"large"}
102
- # DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
103
- # model = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(DEVICE).eval()
104
- # Define path for temporary processed frames
105
- #temp_frame_dir = tempfile.mkdtemp()
106
-
107
- #margin_width = 50
108
- #to_tensor_transform = transforms.ToTensor()
109
-
110
- #DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
111
- # depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_{}14'.format(encoder)).to(DEVICE).eval()
112
- #depth_anything = pipeline(task = "depth-estimation", model=f"nielsr/depth-anything-{mapper[encoder]}")
113
-
114
- # total_params = sum(param.numel() for param in depth_anything.parameters())
115
- # print('Total parameters: {:.2f}M'.format(total_params / 1e6))
116
-
117
- #transform = Compose([
118
- # Resize(
119
- # width=518,
120
- # height=518,
121
- # resize_target=False,
122
- # keep_aspect_ratio=True,
123
- # ensure_multiple_of=14,
124
- # resize_method='lower_bound',
125
- # image_interpolation_method=cv2.INTER_CUBIC,
126
- # ),
127
- # NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
128
- # PrepareForNet(),
129
- #])
130
-
131
- if os.path.isfile(video_path):
132
- if video_path.endswith('txt'):
133
- with open(video_path, 'r') as f:
134
- lines = f.read().splitlines()
135
- else:
136
- filenames = [video_path]
137
- else:
138
- filenames = os.listdir(video_path)
139
- filenames = [os.path.join(video_path, filename) for filename in filenames if not filename.startswith('.')]
140
- filenames.sort()
141
-
142
- # os.makedirs(outdir, exist_ok=True)
143
- global masks
144
-
145
- for k, filename in enumerate(filenames):
146
- file_size = os.path.getsize(filename)/1024/1024
147
- if file_size > 128.0:
148
- print(f'File size of {filename} larger than 128Mb, sorry!')
149
- return filename
150
- print('Progress {:}/{:},'.format(k+1, len(filenames)), 'Processing', filename)
151
-
152
- raw_video = cv2.VideoCapture(filename)
153
- frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
154
- frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
155
- if frame_rate < 1:
156
- frame_rate = 1
157
- cframes = int(raw_video.get(cv2.CAP_PROP_FRAME_COUNT))
158
- print(f'frames: {cframes}, fps: {frame_rate}')
159
- # output_width = frame_width * 2 + margin_width
160
-
161
- #filename = os.path.basename(filename)
162
- # output_path = os.path.join(outdir, filename[:filename.rfind('.')] + '_video_depth.mp4')
163
- #with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
164
- # output_path = tmpfile.name
165
- #out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"avc1"), frame_rate, (output_width, frame_height))
166
- #fourcc = cv2.VideoWriter_fourcc(*'mp4v')
167
- #out = cv2.VideoWriter(output_path, fourcc, frame_rate, (output_width, frame_height))
168
-
169
- count = 0
170
- n = 0
171
- depth_frames = []
172
- orig_frames = []
173
- backup_frames = []
174
- rgba_frames = []
175
- thumbnail_old = []
176
-
177
- while raw_video.isOpened():
178
- ret, raw_frame = raw_video.read()
179
- if not ret:
180
- break
181
- else:
182
- print(count)
183
-
184
- frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB) / 255.0
185
- frame_pil = Image.fromarray((frame * 255).astype(np.uint8))
186
- #frame = transform({'image': frame})['image']
187
- #frame = torch.from_numpy(frame).unsqueeze(0).to(DEVICE)
188
- #raw_frame_bg = cv2.medianBlur(raw_frame, 255)
189
-
190
- #
191
- depth = predict_depth(raw_frame[:, :, ::-1], model)
192
- depth_gray = ((depth - depth.min()) / (depth.max() - depth.min()) * 255.0).astype(np.uint8)
193
- #
194
-
195
- #depth = to_tensor_transform(predict_depth(depth_anything, frame_pil))
196
- #depth = F.interpolate(depth[None], (frame_height, frame_width), mode='bilinear', align_corners=False)[0, 0]
197
- #depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
198
- #depth = depth.cpu().numpy().astype(np.uint8)
199
- #depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_BONE)
200
- #depth_gray = cv2.cvtColor(depth_color, cv2.COLOR_RGBA2GRAY)
201
-
202
- # Remove white border around map:
203
- # define lower and upper limits of white
204
- #white_lo = np.array([250,250,250])
205
- #white_hi = np.array([255,255,255])
206
- # mask image to only select white
207
- mask = cv2.inRange(depth_gray[0:int(depth_gray.shape[0]/8*7)-1, 0:depth_gray.shape[1]], 250, 255)
208
- # change image to black where we found white
209
- depth_gray[0:int(depth_gray.shape[0]/8*7)-1, 0:depth_gray.shape[1]][mask>0] = 0
210
-
211
- mask = cv2.inRange(depth_gray[int(depth_gray.shape[0]/8*7):depth_gray.shape[0], 0:depth_gray.shape[1]], 192, 255)
212
- depth_gray[int(depth_gray.shape[0]/8*7):depth_gray.shape[0], 0:depth_gray.shape[1]][mask>0] = 192
213
-
214
- depth_color = cv2.cvtColor(depth_gray, cv2.COLOR_GRAY2BGR)
215
- # split_region = np.ones((frame_height, margin_width, 3), dtype=np.uint8) * 255
216
- # combined_frame = cv2.hconcat([raw_frame, split_region, depth_color])
217
-
218
- # out.write(combined_frame)
219
- # frame_path = os.path.join(temp_frame_dir, f"frame_{count:05d}.png")
220
- # cv2.imwrite(frame_path, combined_frame)
221
-
222
- #raw_frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2BGRA)
223
- #raw_frame[:, :, 3] = 255
224
-
225
- if cframes < 16:
226
- thumbnail = cv2.cvtColor(cv2.resize(raw_frame, (16,32)), cv2.COLOR_BGR2GRAY).flatten()
227
- if len(thumbnail_old) > 0:
228
- diff = thumbnail - thumbnail_old
229
- #print(diff)
230
- c = Counter(diff)
231
- value, cc = c.most_common()[0]
232
- if value == 0 and cc > int(16*32*0.8):
233
- count += 1
234
- continue
235
- thumbnail_old = thumbnail
236
-
237
- blur_frame = blur_image(raw_frame, depth_color, blur_data)
238
- rgba_frame = cv2.cvtColor(cv2.cvtColor(blur_frame, cv2.COLOR_BGR2RGB), cv2.COLOR_RGB2RGBA)
239
- rgba_frame[:,:,3] = depth_gray
240
-
241
- cv2.imwrite(f"f{count}.png", rgba_frame)
242
- rgba_frames.append(f"f{count}.png")
243
-
244
- cv2.imwrite(f"f{count}.jpg", blur_frame)
245
- orig_frames.append(f"f{count}.jpg")
246
-
247
- cv2.imwrite(f"f{count}_.jpg", blur_frame)
248
- backup_frames.append(f"f{count}_.jpg")
249
-
250
- cv2.imwrite(f"f{count}_dmap.jpg", depth_color)
251
- depth_frames.append(f"f{count}_dmap.jpg")
252
-
253
- depth_gray = seg_frame(depth_gray, o, b) + 128
254
- #print(depth_gray[depth_gray>128]-128)
255
-
256
- cv2.imwrite(f"f{count}_mask.jpg", depth_gray)
257
- masks.append(f"f{count}_mask.jpg")
258
- count += 1
259
-
260
- final_vid = create_video(orig_frames, frame_rate, "orig")
261
- depth_vid = create_video(depth_frames, frame_rate, "depth")
262
- alpha_vid = create_alpha_video(rgba_frames, frame_rate, "rgba")
263
-
264
- final_zip = zip_files(orig_frames, depth_frames)
265
- raw_video.release()
266
- # out.release()
267
- cv2.destroyAllWindows()
268
-
269
- global gradient
270
- global frame_selected
271
- global depths
272
- global frames
273
- global backups
274
- frames = orig_frames
275
- backups = backup_frames
276
- depths = depth_frames
277
-
278
- if depth_color.shape[0] == 2048: #height
279
- gradient = cv2.imread('./gradient_large.png').astype(np.uint8)
280
- elif depth_color.shape[0] == 1024:
281
- gradient = cv2.imread('./gradient.png').astype(np.uint8)
282
- else:
283
- gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
284
-
285
- return alpha_vid, final_zip, frames, masks[frame_selected], depths, depth_vid #output_path
286
-
287
- def depth_edges_mask(depth):
288
- """Returns a mask of edges in the depth map.
289
- Args:
290
- depth: 2D numpy array of shape (H, W) with dtype float32.
291
- Returns:
292
- mask: 2D numpy array of shape (H, W) with dtype bool.
293
- """
294
- # Compute the x and y gradients of the depth map.
295
- depth_dx, depth_dy = np.gradient(depth)
296
- # Compute the gradient magnitude.
297
- depth_grad = np.sqrt(depth_dx ** 2 + depth_dy ** 2)
298
- # Compute the edge mask.
299
- mask = depth_grad > 0.05
300
- return mask
301
-
302
- def pano_depth_to_world_points(depth):
303
- """
304
- 360 depth to world points
305
- given 2D depth is an equirectangular projection of a spherical image
306
- Treat depth as radius
307
- longitude : -pi to pi
308
- latitude : -pi/2 to pi/2
309
- """
310
-
311
- # Convert depth to radius
312
- radius = (255 - depth.flatten())
313
-
314
- lon = np.linspace(0, np.pi*2, depth.shape[1])
315
- lat = np.linspace(0, np.pi, depth.shape[0])
316
- lon, lat = np.meshgrid(lon, lat)
317
- lon = lon.flatten()
318
- lat = lat.flatten()
319
-
320
- pts3d = [[0,0,0]]
321
- uv = [[0,0]]
322
- nl = [[0,0,0]]
323
- for i in range(0, 1): #(0,2)
324
- for j in range(0, 1): #(0,2)
325
- #rnd_lon = (np.random.rand(depth.shape[0]*depth.shape[1]) - 0.5) / 8
326
- #rnd_lat = (np.random.rand(depth.shape[0]*depth.shape[1]) - 0.5) / 8
327
- d_lon = lon + i/2 * np.pi*2 / depth.shape[1]
328
- d_lat = lat + j/2 * np.pi / depth.shape[0]
329
-
330
- nx = np.cos(d_lon) * np.sin(d_lat)
331
- ny = np.cos(d_lat)
332
- nz = np.sin(d_lon) * np.sin(d_lat)
333
-
334
- # Convert to cartesian coordinates
335
- x = radius * nx
336
- y = radius * ny
337
- z = radius * nz
338
-
339
- pts = np.stack([x, y, z], axis=1)
340
- uvs = np.stack([lon/np.pi/2, lat/np.pi], axis=1)
341
- nls = np.stack([-nx, -ny, -nz], axis=1)
342
-
343
- pts3d = np.concatenate((pts3d, pts), axis=0)
344
- uv = np.concatenate((uv, uvs), axis=0)
345
- nl = np.concatenate((nl, nls), axis=0)
346
- #print(f'i: {i}, j: {j}')
347
- j = j+1
348
- i = i+1
349
-
350
- return [pts3d, uv, nl]
351
-
352
- def rgb2gray(rgb):
353
- return np.dot(rgb[...,:3], [0.333, 0.333, 0.333])
354
-
355
- def get_mesh(image, depth, blur_data, loadall):
356
- global depths
357
- global pcolors
358
- global frame_selected
359
- global mesh
360
- global mesh_n
361
- global scene
362
- if loadall == False:
363
- mesh = []
364
- mesh_n = []
365
- fnum = frame_selected
366
-
367
- #print(image[fnum][0])
368
- #print(depth["composite"])
369
-
370
- depthc = cv2.imread(depths[frame_selected], cv2.IMREAD_UNCHANGED).astype(np.uint8)
371
- blur_img = blur_image(cv2.imread(image[fnum][0], cv2.IMREAD_UNCHANGED).astype(np.uint8), depthc, blur_data)
372
- gdepth = cv2.cvtColor(depthc, cv2.COLOR_RGB2GRAY) #rgb2gray(depthc)
373
-
374
- print('depth to gray - ok')
375
- points = pano_depth_to_world_points(gdepth)
376
- pts3d = points[0]
377
- uv = points[1]
378
- nl = points[2]
379
- print('radius from depth - ok')
380
-
381
- # Create a trimesh mesh from the points
382
- # Each pixel is connected to its 4 neighbors
383
- # colors are the RGB values of the image
384
- uvs = uv.reshape(-1, 2)
385
- #print(uvs)
386
- #verts = pts3d.reshape(-1, 3)
387
- verts = [[0,0,0]]
388
- normals = nl.reshape(-1, 3)
389
- rgba = cv2.cvtColor(blur_img, cv2.COLOR_RGB2RGBA)
390
- colors = rgba.reshape(-1, 4)
391
- clrs = [[128,128,128,0]]
392
-
393
- #for i in range(0,1): #(0,4)
394
- #clrs = np.concatenate((clrs, colors), axis=0)
395
- #i = i+1
396
- #verts, clrs
397
-
398
- #pcd = o3d.geometry.TriangleMesh.create_tetrahedron()
399
- #pcd.compute_vertex_normals()
400
- #pcd.paint_uniform_color((1.0, 1.0, 1.0))
401
- #mesh.append(pcd)
402
- #print(mesh[len(mesh)-1])
403
- if not str(fnum) in mesh_n:
404
- mesh_n.append(str(fnum))
405
- print('mesh - ok')
406
-
407
- # Save as glb
408
- #glb_file = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
409
- #o3d.io.write_triangle_mesh(glb_file.name, pcd)
410
- #print('file - ok')
411
- return "./TriangleWithoutIndices.gltf", ",".join(mesh_n)
412
-
413
-
414
- def blur_image(image, depth, blur_data):
415
- blur_a = blur_data.split()
416
- #print(f'blur data {blur_data}')
417
-
418
- blur_frame = image.copy()
419
- j = 0
420
- while j < 256:
421
- i = 255 - j
422
- blur_lo = np.array([i,i,i])
423
- blur_hi = np.array([i+1,i+1,i+1])
424
- blur_mask = cv2.inRange(depth, blur_lo, blur_hi)
425
-
426
- #print(f'kernel size {int(blur_a[j])}')
427
- blur = cv2.GaussianBlur(image, (int(blur_a[j]), int(blur_a[j])), 0)
428
-
429
- blur_frame[blur_mask>0] = blur[blur_mask>0]
430
- j = j + 1
431
-
432
- white = cv2.inRange(blur_frame, np.array([255,255,255]), np.array([255,255,255]))
433
- blur_frame[white>0] = (254,254,254)
434
-
435
- return blur_frame
436
-
437
- def loadfile(f):
438
- return f
439
-
440
- def show_json(txt):
441
- data = json.loads(txt)
442
- print(txt)
443
- i=0
444
- while i < len(data[2]):
445
- data[2][i] = data[2][i]["image"]["path"]
446
- data[4][i] = data[4][i]["path"]
447
- i=i+1
448
- return data[0]["video"]["path"], data[1]["path"], data[2], data[3]["background"]["path"], data[4], data[5]
449
-
450
-
451
- def seg_frame(newmask, b, d):
452
-
453
- if newmask.shape[0] == 2048: #height
454
- gd = cv2.imread('./gradient_large.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
455
- elif newmask.shape[0] == 1024:
456
- gd = cv2.imread('./gradient.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
457
- else:
458
- gd = cv2.imread('./gradient_small.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
459
-
460
- newmask[np.absolute(newmask.astype(np.int16)-gd.astype(np.int16))<16] = 0
461
- ret,newmask = cv2.threshold(newmask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
462
-
463
- #b = 1
464
- #d = 32
465
- element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
466
- bd = cv2.erode(newmask, element)
467
- element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * d + 1, 2 * d + 1), (d, d))
468
- bg = cv2.dilate(newmask, element)
469
- bg[bg.shape[0]-64:bg.shape[0],0:bg.shape[1]] = 0
470
-
471
- mask = np.zeros(newmask.shape[:2],np.uint8)
472
- # https://docs.opencv.org/4.x/d8/d83/tutorial_py_grabcut.html
473
- # wherever it is marked white (sure foreground), change mask=1
474
- # wherever it is marked black (sure background), change mask=0
475
- mask[bg == 255] = 3
476
- mask[bd == 255] = 1 #2: probable bg, 3: probable fg
477
-
478
- return mask
479
-
480
-
481
- def select_frame(d, evt: gr.SelectData):
482
- global frame_selected
483
- global depths
484
- global masks
485
- global edge
486
-
487
- if evt.index != frame_selected:
488
- edge = []
489
- frame_selected = evt.index
490
-
491
- return depths[frame_selected], frame_selected
492
-
493
- def switch_rows(v):
494
- global frames
495
- global depths
496
- if v == True:
497
- print(depths[0])
498
- return depths
499
- else:
500
- print(frames[0])
501
- return frames
502
-
503
-
504
- def bincount(a):
505
- a2D = a.reshape(-1,a.shape[-1])
506
- col_range = (256, 256, 256) # generically : a2D.max(0)+1
507
- a1D = np.ravel_multi_index(a2D.T, col_range)
508
- return list(reversed(np.unravel_index(np.bincount(a1D).argmax(), col_range)))
509
-
510
- def reset_mask(d):
511
- global frame_selected
512
- global frames
513
- global backups
514
- global masks
515
- global depths
516
- global edge
517
-
518
- edge = []
519
- backup = cv2.imread(backups[frame_selected]).astype(np.uint8)
520
- cv2.imwrite(frames[frame_selected], backup)
521
-
522
- d["layers"][0][0:d["layers"][0].shape[0], 0:d["layers"][0].shape[1]] = (0,0,0,0)
523
-
524
- return gr.ImageEditor(value=d)
525
-
526
-
527
- def draw_mask(o, b, v, d, evt: gr.EventData):
528
- global frames
529
- global depths
530
- global params
531
- global frame_selected
532
- global masks
533
- global gradient
534
- global edge
535
-
536
- points = json.loads(v)
537
- pts = np.array(points, np.int32)
538
- pts = pts.reshape((-1,1,2))
539
-
540
- if len(edge) == 0 or params["fnum"] != frame_selected:
541
- if params["fnum"] != frame_selected:
542
- d["background"] = cv2.imread(depths[frame_selected]).astype(np.uint8)
543
- params["fnum"] = frame_selected
544
-
545
- bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY)
546
- bg[bg==255] = 0
547
-
548
- edge = bg.copy()
549
- else:
550
- bg = edge.copy()
551
-
552
- x = points[len(points)-1][0]
553
- y = points[len(points)-1][1]
554
-
555
- mask = cv2.imread(masks[frame_selected], cv2.IMREAD_GRAYSCALE).astype(np.uint8)
556
- mask[mask==128] = 0
557
- print(mask[mask>0]-128)
558
- d["layers"][0] = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGBA)
559
-
560
- sel = cv2.floodFill(mask, None, (x, y), 1, 2, 2, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
561
- # 255 << 8 tells to fill with the value 255)
562
- sel = sel[1:sel.shape[0]-1, 1:sel.shape[1]-1]
563
-
564
- d["layers"][0][sel==0] = (0,0,0,0)
565
-
566
-
567
- mask = cv2.cvtColor(d["layers"][0], cv2.COLOR_RGBA2GRAY)
568
- mask[mask==0] = 128
569
- print(mask[mask>128]-128)
570
- mask, bgdModel, fgdModel = cv2.grabCut(cv2.cvtColor(d["background"], cv2.COLOR_RGBA2RGB), mask-128, None,None,None,15, cv2.GC_INIT_WITH_MASK)
571
- mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
572
-
573
- frame = cv2.imread(frames[frame_selected], cv2.IMREAD_UNCHANGED).astype(np.uint8)
574
- frame[mask>0] = (255,255,255)
575
- cv2.imwrite(frames[frame_selected], frame)
576
-
577
- switch_rows(False)
578
- return gr.ImageEditor(value=d)
579
-
580
-
581
- load_model="""
582
- async(c, o, p, d, n, m, s)=>{
583
- var intv = setInterval(function(){
584
- if (document.getElementById("model3D").getElementsByTagName("canvas")[0]) {
585
- try {
586
- if (typeof BABYLON !== "undefined" && BABYLON.Engine && BABYLON.Engine.LastCreatedScene) {
587
- BABYLON.Engine.LastCreatedScene.onAfterRenderObservable.add(function() { //onDataLoadedObservable
588
-
589
- var then = new Date().getTime();
590
- var now, delta;
591
- const interval = 1000 / 25;
592
- const tolerance = 0.1;
593
-
594
- BABYLON.Engine.LastCreatedScene.getEngine().stopRenderLoop();
595
- BABYLON.Engine.LastCreatedScene.getEngine().runRenderLoop(function () {
596
- now = new Date().getTime();
597
- delta = now - then;
598
- then = now - (delta % interval);
599
- if (delta >= interval - tolerance) {
600
- BABYLON.Engine.LastCreatedScene.render();
601
- }
602
- });
603
-
604
- BABYLON.Engine.LastCreatedScene.getEngine().setHardwareScalingLevel(1.0);
605
- BABYLON.Engine.LastCreatedScene.clearColor = new BABYLON.Color4(255,255,255,255);
606
- BABYLON.Engine.LastCreatedScene.ambientColor = new BABYLON.Color4(255,255,255,255);
607
- //BABYLON.Engine.LastCreatedScene.autoClear = false;
608
- //BABYLON.Engine.LastCreatedScene.autoClearDepthAndStencil = false;
609
- /*for (var i=0; i<BABYLON.Engine.LastCreatedScene.getNodes().length; i++) {
610
- if (BABYLON.Engine.LastCreatedScene.getNodes()[i].material) {
611
- BABYLON.Engine.LastCreatedScene.getNodes()[i].material.pointSize = Math.ceil(Math.log2(Math.PI/document.getElementById("zoom").value));
612
- }
613
- }*/
614
- BABYLON.Engine.LastCreatedScene.getAnimationRatio();
615
- });
616
-
617
- if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
618
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata = {
619
- pipeline: new BABYLON.DefaultRenderingPipeline("default", true, BABYLON.Engine.LastCreatedScene, [BABYLON.Engine.LastCreatedScene.activeCamera])
620
- }
621
- }
622
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.samples = 4;
623
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast = 1.0;
624
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure = 1.0;
625
-
626
- //BABYLON.Engine.LastCreatedScene.activeCamera.detachControl(document.getElementById("model3D").getElementsByTagName("canvas")[0]);
627
- BABYLON.Engine.LastCreatedScene.activeCamera.inertia = 0.0;
628
- //pan
629
- BABYLON.Engine.LastCreatedScene.activeCamera.panningInertia = 0.0;
630
- BABYLON.Engine.LastCreatedScene.activeCamera.panningDistanceLimit = 16;
631
- BABYLON.Engine.LastCreatedScene.activeCamera.panningSensibility = 16;
632
- //zoom
633
- BABYLON.Engine.LastCreatedScene.activeCamera.pinchDeltaPercentage = 1/256;
634
- BABYLON.Engine.LastCreatedScene.activeCamera.wheelDeltaPercentage = 1/256;
635
- BABYLON.Engine.LastCreatedScene.activeCamera.upperRadiusLimit = (1.57-0.157)*16;
636
- BABYLON.Engine.LastCreatedScene.activeCamera.lowerRadiusLimit = 0.0;
637
- //BABYLON.Engine.LastCreatedScene.activeCamera.attachControl(document.getElementById("model3D").getElementsByTagName("canvas")[0], false);
638
-
639
- BABYLON.Engine.LastCreatedScene.activeCamera.fov = document.getElementById("zoom").value;
640
-
641
- 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)";
642
- document.getElementById("model3D").getElementsByTagName("canvas")[0].oncontextmenu = function(e){e.preventDefault();}
643
- document.getElementById("model3D").getElementsByTagName("canvas")[0].ondrag = function(e){e.preventDefault();}
644
-
645
- document.getElementById("model3D").appendChild(document.getElementById("compass_box"));
646
- window.coords = JSON.parse(document.getElementById("coords").getElementsByTagName("textarea")[0].value);
647
- window.counter = 0;
648
-
649
- if (o.indexOf(""+n) < 0) {
650
- if (o != "") { o += ","; }
651
- o += n;
652
- }
653
- //alert(o);
654
- var o_ = o.split(",");
655
- var q = BABYLON.Engine.LastCreatedScene.meshes;
656
- for(i = 0; i < q.length; i++) {
657
- let mesh = q[i];
658
- mesh.dispose(false, true);
659
- }
660
- var dome = [];
661
- /*for (var j=0; j<o_.length; j++) {
662
- o_[j] = parseInt(o_[j]);
663
- dome[j] = new BABYLON.PhotoDome("dome"+j, p[o_[j]].image.url,
664
- {
665
- resolution: 16,
666
- size: 512
667
- }, BABYLON.Engine.LastCreatedScene);
668
- var q = BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-2]._children;
669
- for(i = 0; i < q.length; i++) {
670
- let mesh = q[i];
671
- mesh.dispose(false, true);
672
- }
673
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].name = "dome"+j;
674
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].scaling.z = -1;
675
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].alphaIndex = o_.length-j;
676
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].visibility = 0.9999;
677
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.diffuseTexture.hasAlpha = true;
678
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.useAlphaFromDiffuseTexture = true;
679
- 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);});
680
-
681
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].rotationQuaternion = null;
682
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].position.z = coords[o_[j]].lat;
683
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].position.x = coords[o_[j]].lng;
684
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].rotation.y = coords[o_[j]].heading / 180 * Math.PI;
685
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].rotation.z = -coords[o_[j]].pitch / 180 * Math.PI;
686
- }*/
687
-
688
- if (s == false) {
689
- v_url = document.getElementById("output_video").getElementsByTagName("video")[0].src;
690
- } else {
691
- v_url = document.getElementById("depth_video").getElementsByTagName("video")[0].src;
692
- }
693
- window.videoDome = new BABYLON.VideoDome(
694
- "videoDome", [v_url],
695
- {
696
- resolution: 16,
697
- size: 512,
698
- clickToPlay: false,
699
- }, BABYLON.Engine.LastCreatedScene
700
- );
701
- var q = BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-2]._children;
702
- for (i = 0; i < q.length; i++) {
703
- let mesh = q[i];
704
- mesh.dispose(false, true);
705
- }
706
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].rotationQuaternion = null;
707
- //BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].position.z = coords[counter].lat;
708
- //BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].position.x = coords[counter].lng;
709
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].rotation.y = coords[counter].heading / 180 * Math.PI;
710
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].rotation.z = -coords[counter].pitch / 180 * Math.PI;
711
-
712
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].scaling.z = -1;
713
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.diffuseTexture.hasAlpha = true;
714
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.useAlphaFromDiffuseTexture = true;
715
- //BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.emissiveTexture = videoDome.videoTexture;
716
- //BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.emissiveTexture.hasAlpha = true;
717
- //BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].material.useAlphaFromEmissiveTexture = true;
718
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].alphaIndex = 1;
719
- BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1].visibility = 0.9999;
720
-
721
- window.md = false;
722
- window.rd = false;
723
- window.compass = document.getElementById("compass");
724
- window.x = 0;
725
- window.y = 0;
726
- window.xold = 0;
727
- window.yold = 0;
728
- window.buffer = null;
729
- window.bufferCanvas = document.createElement("canvas");
730
- window.ctx = bufferCanvas.getContext("2d", { willReadFrequently: true });
731
- window.video = document.getElementById("depth_video").getElementsByTagName("video")[0];
732
- window.parallax = 0;
733
- window.xdir = new BABYLON.Vector3(1, 0, 0);
734
- window.rdir = new BABYLON.Vector3(0, 0, 0);
735
- window.videoDomeMesh = BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1];
736
-
737
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointermove', function(evt) {
738
- if (md === true) {
739
- rdir = BABYLON.Engine.LastCreatedScene.activeCamera.getDirection(xdir);
740
- videoDomeMesh.position.x = parallax * rdir.x;
741
- videoDomeMesh.position.z = parallax * rdir.z;
742
-
743
- try {
744
- compass.style.transform = "rotateX(" + (BABYLON.Engine.LastCreatedScene.activeCamera.beta-Math.PI/2) + "rad) rotateZ(" + BABYLON.Engine.LastCreatedScene.activeCamera.alpha + "rad)";
745
- } catch(e) {alert(e);}
746
- }
747
- if (rd === true) {
748
- x = parseInt(evt.clientX - evt.target.getBoundingClientRect().x);
749
- y = parseInt(evt.clientY - evt.target.getBoundingClientRect().y);
750
-
751
- if (Math.abs(BABYLON.Engine.LastCreatedScene.activeCamera.radius) > (1.57-0.157)*16) {
752
- BABYLON.Engine.LastCreatedScene.activeCamera.radius = (1.57-0.157)*16;
753
- } else {
754
- BABYLON.Engine.LastCreatedScene.activeCamera.fov = BABYLON.Engine.LastCreatedScene.activeCamera.radius/16 + 0.157;
755
- }
756
- document.getElementById('zoom').value = BABYLON.Engine.LastCreatedScene.activeCamera.fov;
757
- document.getElementById('zoom').parentNode.childNodes[2].innerText = document.getElementById('zoom').value;
758
-
759
- xold=x;
760
- yold=y;
761
- }
762
- });
763
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointerdown', function() {
764
- md = true;
765
- });
766
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointerup', function() {
767
- md = false;
768
- rd = false;
769
- });
770
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointercancel', function() {
771
- md = false;
772
- rd = false;
773
- });
774
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointerleave', function() {
775
- md = false;
776
- rd = false;
777
- });
778
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointerout', function() {
779
- md = false;
780
- rd = false;
781
- });
782
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('contextmenu', function() {
783
- rd = true;
784
- });
785
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('gesturestart', function() {
786
- rd = true;
787
- });
788
- document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('gestureend', function() {
789
- rd = false;
790
- });
791
-
792
-
793
- function requestMap() {
794
- try {
795
- ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
796
- videoDome.videoTexture.video.pause();
797
- video.pause();
798
- if (buffer) {
799
- counter = parseInt(video.currentTime);
800
- if (!coords[counter]) {counter = coords.length-1;}
801
- applyDisplacementMapFromBuffer(videoDomeMesh, buffer, video.videoWidth, video.videoHeight, 0, -1, null, null, true);
802
- }
803
- buffer = ctx.getImageData(0, 0, video.videoWidth, video.videoHeight).data;
804
- applyDisplacementMapFromBuffer(videoDomeMesh, buffer, video.videoWidth, video.videoHeight, 0, 1, null, null, true);
805
- } catch(e) {alert(e)}
806
- }
807
- window.requestMap = requestMap;
808
-
809
- videoDome.videoTexture.video.oncanplaythrough = function () {
810
- document.getElementById('seek').innerHTML = '';
811
- for (var i=0; i<videoDome.videoTexture.video.duration; i++) {
812
- document.getElementById('seek').innerHTML += '<a href="#" style="position:absolute;left:'+(56+coords[i].lng/2)+'px;top:'+(56-coords[i].lat/2)+'px;" onclick="seek('+i+');">-'+i+'-</a> ';
813
- }
814
- bufferCanvas.width = video.videoWidth;
815
- bufferCanvas.height = video.videoHeight;
816
-
817
- videoPlay();
818
- };
819
-
820
- //var debugLayer = BABYLON.Engine.LastCreatedScene.debugLayer.show();
821
-
822
- if (document.getElementById("model")) {
823
- document.getElementById("model").appendChild(document.getElementById("model3D"));
824
- toggleDisplay("model");
825
- }
826
-
827
- clearInterval(intv);
828
- }
829
- } catch(e) {alert(e);}
830
- }
831
- }, 40);
832
- }
833
- """
834
-
835
- js = """
836
- async()=>{
837
- console.log('Hi');
838
-
839
- const chart = document.getElementById('chart');
840
- const blur_in = document.getElementById('blur_in').getElementsByTagName('textarea')[0];
841
- var md = false;
842
- var xold = 128;
843
- var yold = 32;
844
- var a = new Array(256);
845
- var l;
846
-
847
- for (var i=0; i<256; i++) {
848
- const hr = document.createElement('hr');
849
- hr.style.backgroundColor = 'hsl(0,0%,' + (100-i/256*100) + '%)';
850
- chart.appendChild(hr);
851
- }
852
-
853
- function resetLine() {
854
- a.fill(1);
855
- for (var i=0; i<256; i++) {
856
- chart.childNodes[i].style.height = a[i] + 'px';
857
- chart.childNodes[i].style.marginTop = '32px';
858
- }
859
- }
860
- resetLine();
861
- window.resetLine = resetLine;
862
-
863
- function pointerDown(x, y) {
864
- md = true;
865
- xold = parseInt(x - chart.getBoundingClientRect().x);
866
- yold = parseInt(y - chart.getBoundingClientRect().y);
867
- chart.title = xold + ',' + yold;
868
- }
869
- window.pointerDown = pointerDown;
870
-
871
- function pointerUp() {
872
- md = false;
873
- var evt = document.createEvent('Event');
874
- evt.initEvent('input', true, false);
875
- blur_in.dispatchEvent(evt);
876
- chart.title = '';
877
- }
878
- window.pointerUp = pointerUp;
879
-
880
- function lerp(y1, y2, mu) { return y1*(1-mu)+y2*mu; }
881
-
882
- function drawLine(x, y) {
883
- x = parseInt(x - chart.getBoundingClientRect().x);
884
- y = parseInt(y - chart.getBoundingClientRect().y);
885
- if (md === true && y >= 0 && y < 64 && x >= 0 && x < 256) {
886
- if (y < 32) {
887
- a[x] = Math.abs(32-y)*2 + 1;
888
- chart.childNodes[x].style.height = a[x] + 'px';
889
- chart.childNodes[x].style.marginTop = y + 'px';
890
-
891
- for (var i=Math.min(xold, x)+1; i<Math.max(xold, x); i++) {
892
- l = parseInt(lerp( yold, y, (i-xold)/(x-xold) ));
893
-
894
- if (l < 32) {
895
- a[i] = Math.abs(32-l)*2 + 1;
896
- chart.childNodes[i].style.height = a[i] + 'px';
897
- chart.childNodes[i].style.marginTop = l + 'px';
898
- } else if (l < 64) {
899
- a[i] = Math.abs(l-32)*2 + 1;
900
- chart.childNodes[i].style.height = a[i] + 'px';
901
- chart.childNodes[i].style.marginTop = (64-l) + 'px';
902
- }
903
- }
904
- } else if (y < 64) {
905
- a[x] = Math.abs(y-32)*2 + 1;
906
- chart.childNodes[x].style.height = a[x] + 'px';
907
- chart.childNodes[x].style.marginTop = (64-y) + 'px';
908
-
909
- for (var i=Math.min(xold, x)+1; i<Math.max(xold, x); i++) {
910
- l = parseInt(lerp( yold, y, (i-xold)/(x-xold) ));
911
-
912
- if (l < 32) {
913
- a[i] = Math.abs(32-l)*2 + 1;
914
- chart.childNodes[i].style.height = a[i] + 'px';
915
- chart.childNodes[i].style.marginTop = l + 'px';
916
- } else if (l < 64) {
917
- a[i] = Math.abs(l-32)*2 + 1;
918
- chart.childNodes[i].style.height = a[i] + 'px';
919
- chart.childNodes[i].style.marginTop = (64-l) + 'px';
920
- }
921
- }
922
- }
923
- blur_in.value = a.join(' ');
924
- xold = x;
925
- yold = y;
926
- chart.title = xold + ',' + yold;
927
- }
928
- }
929
- window.drawLine = drawLine;
930
-
931
-
932
- window.screenshot = false;
933
-
934
- function snapshot() {
935
- if (BABYLON) {
936
- screenshot = true;
937
- BABYLON.Engine.LastCreatedScene.getEngine().onEndFrameObservable.add(function() {
938
- if (screenshot === true) {
939
- screenshot = false;
940
- try {
941
- BABYLON.Tools.CreateScreenshotUsingRenderTarget(BABYLON.Engine.LastCreatedScene.getEngine(), BABYLON.Engine.LastCreatedScene.activeCamera,
942
- { precision: 1.0 }, (durl) => {
943
- var cnvs = document.getElementById("model3D").getElementsByTagName("canvas")[0]; //.getContext("webgl2");
944
- 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">
945
- <defs>
946
- <filter id="blur" x="0" y="0" xmlns="http://www.w3.org/2000/svg">
947
- <feGaussianBlur in="SourceGraphic" stdDeviation="1" />
948
- </filter>
949
- </defs>
950
- <image filter="url(#blur)" id="svg_img" x="0" y="0" width="` + cnvs.width + `" height="` + cnvs.height + `" xlink:href=\"` + durl + `\"/>
951
- </svg>`;
952
- document.getElementById("cnv_out").width = cnvs.width;
953
- document.getElementById("cnv_out").height = cnvs.height;
954
- document.getElementById("img_out").src = "data:image/svg+xml;base64," + btoa(svgd);
955
- }
956
- );
957
- } catch(e) { alert(e); }
958
- // https://forum.babylonjs.com/t/best-way-to-save-to-jpeg-snapshots-of-scene/17663/11
959
- }
960
- });
961
- }
962
- }
963
- window.snapshot = snapshot;
964
-
965
-
966
- window.recorder = null;
967
-
968
- function record_video() {
969
- try {
970
- if (BABYLON.VideoRecorder.IsSupported(BABYLON.Engine.LastCreatedScene.getEngine()) && (recorder == null || !recorder.isRecording) ) {
971
- if (recorder == null) {
972
- recorder = new BABYLON.VideoRecorder(BABYLON.Engine.LastCreatedScene.getEngine(), { mimeType:'video/mp4', fps:25, /*audioTracks: mediaStreamDestination.stream.getAudioTracks()*/ });
973
- }
974
- recorder.startRecording('video.mp4', 60*60);
975
- }
976
- } catch(e) {alert(e);}
977
- }
978
- window.record_video = record_video;
979
-
980
- function stop_recording() {
981
- if (recorder.isRecording) {
982
- recorder.stopRecording();
983
- }
984
- }
985
- window.stop_recording = stop_recording;
986
-
987
- function seek(t) {
988
- videoDome.videoTexture.video.currentTime = t;
989
- if (videoDome.videoTexture.video.currentTime > videoDome.videoTexture.video.duration) {
990
- videoDome.videoTexture.video.currentTime = videoDome.videoTexture.video.duration;
991
- } else if (videoDome.videoTexture.video.currentTime < 0) {
992
- videoDome.videoTexture.video.currentTime = 0;
993
- }
994
- video.currentTime = t;
995
- if (video.currentTime > video.duration) {
996
- video.currentTime = video.duration;
997
- } else if (video.currentTime < 0) {
998
- video.currentTime = 0;
999
- }
1000
- requestMap();
1001
- }
1002
- window.seek = seek;
1003
-
1004
- function videoPlay() {
1005
- videoDome.videoTexture.video.oncanplaythrough = null;
1006
- video.oncanplaythrough = null;
1007
-
1008
- videoDome.videoTexture.video.loop = true;
1009
- video.loop = true;
1010
- videoDome.videoTexture.video.play();
1011
- video.play();
1012
- }
1013
- window.videoPlay = videoPlay;
1014
-
1015
-
1016
- function applyDisplacementMapFromBuffer(
1017
- mesh,
1018
- buffer,
1019
- heightMapWidth,
1020
- heightMapHeight,
1021
- minHeight,
1022
- maxHeight,
1023
- uvOffset,
1024
- uvScale,
1025
- forceUpdate
1026
- ) {
1027
- try {
1028
- if (!mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
1029
- let positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
1030
- let normals = [];
1031
- BABYLON.VertexData.ComputeNormals(positions, mesh.getIndices(), normals, {useRightHandedSystem: true});
1032
- mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
1033
- }
1034
- const positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind, true, true);
1035
- const normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
1036
- const uvs = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
1037
-
1038
- let position = BABYLON.Vector3.Zero();
1039
- const normal = BABYLON.Vector3.Zero();
1040
- const uv = BABYLON.Vector2.Zero();
1041
-
1042
- uvOffset = uvOffset || BABYLON.Vector2.Zero();
1043
- uvScale = uvScale || new BABYLON.Vector2(1, 1);
1044
-
1045
- for (let index = 0; index < positions.length; index += 3) {
1046
- BABYLON.Vector3.FromArrayToRef(positions, index, position);
1047
- BABYLON.Vector3.FromArrayToRef(normals, index, normal);
1048
- BABYLON.Vector2.FromArrayToRef(uvs, (index / 3) * 2, uv);
1049
-
1050
- // Compute height
1051
- const u = (Math.abs(uv.x * uvScale.x + (uvOffset.x % 1)) * (heightMapWidth - 1)) % heightMapWidth | 0;
1052
- const v = (Math.abs(uv.y * uvScale.y + (uvOffset.y % 1)) * (heightMapHeight - 1)) % heightMapHeight | 0;
1053
-
1054
- const pos = (u + v * heightMapWidth) * 4;
1055
- const r = buffer[pos] / 255.0;
1056
- const g = buffer[pos + 1] / 255.0;
1057
- const b = buffer[pos + 2] / 255.0;
1058
- const a = buffer[pos + 3] / 255.0;
1059
-
1060
- const gradient = r * 0.33 + g * 0.33 + b * 0.33;
1061
- //const gradient = a;
1062
-
1063
- normal.normalize();
1064
- normal.scaleInPlace(minHeight + (maxHeight - minHeight) * gradient);
1065
- position = position.add(normal);
1066
-
1067
- position.toArray(positions, index);
1068
- }
1069
- mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
1070
-
1071
- return mesh;
1072
- } catch(e) {alert(e)}
1073
- }
1074
- window.applyDisplacementMapFromBuffer = applyDisplacementMapFromBuffer;
1075
-
1076
-
1077
- var intv_ = setInterval(function(){
1078
- if (document.getElementById("image_edit") && document.getElementById("image_edit").getElementsByTagName("canvas")) {
1079
- document.getElementById("image_edit").getElementsByTagName("canvas")[0].oncontextmenu = function(e){e.preventDefault();}
1080
- document.getElementById("image_edit").getElementsByTagName("canvas")[0].ondrag = function(e){e.preventDefault();}
1081
-
1082
- document.getElementById("image_edit").getElementsByTagName("canvas")[0].onclick = function(e) {
1083
- var x = parseInt((e.clientX-e.target.getBoundingClientRect().x)*e.target.width/e.target.getBoundingClientRect().width);
1084
- var y = parseInt((e.clientY-e.target.getBoundingClientRect().y)*e.target.height/e.target.getBoundingClientRect().height);
1085
-
1086
- var p = document.getElementById("mouse").getElementsByTagName("textarea")[0].value.slice(1, -1);
1087
- if (p != "") { p += ", "; }
1088
- p += "[" + x + ", " + y + "]";
1089
- document.getElementById("mouse").getElementsByTagName("textarea")[0].value = "[" + p + "]";
1090
-
1091
- var evt = document.createEvent("Event");
1092
- evt.initEvent("input", true, false);
1093
- document.getElementById("mouse").getElementsByTagName("textarea")[0].dispatchEvent(evt);
1094
- }
1095
- document.getElementById("image_edit").getElementsByTagName("canvas")[0].onfocus = function(e) {
1096
- document.getElementById("mouse").getElementsByTagName("textarea")[0].value = "[]";
1097
- }
1098
- document.getElementById("image_edit").getElementsByTagName("canvas")[0].onblur = function(e) {
1099
- document.getElementById("mouse").getElementsByTagName("textarea")[0].value = "[]";
1100
- }
1101
- clearInterval(intv_);
1102
- }
1103
- }, 40);
1104
-
1105
- }
1106
- """
1107
-
1108
- css = """
1109
- #img-display-container {
1110
- max-height: 100vh;
1111
- }
1112
- #img-display-input {
1113
- max-height: 80vh;
1114
- }
1115
- #img-display-output {
1116
- max-height: 80vh;
1117
- }
1118
- """
1119
-
1120
- head = """
1121
- """
1122
-
1123
- title = "# Depth Anything V2 Video"
1124
- description = """**Depth Anything V2** on full video files, intended for Google Street View panorama slideshows.
1125
- Please refer to the [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."""
1126
-
1127
-
1128
- #transform = Compose([
1129
- # Resize(
1130
- # width=518,
1131
- # height=518,
1132
- # resize_target=False,
1133
- # keep_aspect_ratio=True,
1134
- # ensure_multiple_of=14,
1135
- # resize_method='lower_bound',
1136
- # image_interpolation_method=cv2.INTER_CUBIC,
1137
- # ),
1138
- # NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
1139
- # PrepareForNet(),
1140
- #])
1141
-
1142
- # @torch.no_grad()
1143
- # def predict_depth(model, image):
1144
- # return model(image)
1145
-
1146
- with gr.Blocks(css=css, js=js, head=head) as demo:
1147
- gr.Markdown(title)
1148
- gr.Markdown(description)
1149
- gr.Markdown("### Video Depth Prediction demo")
1150
-
1151
- with gr.Row():
1152
- with gr.Column():
1153
- with gr.Group():
1154
- input_json = gr.Textbox(elem_id="json_in", value="{}", label="JSON", interactive=False)
1155
- input_url = gr.Textbox(elem_id="url_in", value="./examples/streetview.mp4", label="URL")
1156
- input_video = gr.Video(label="Input Video", format="mp4")
1157
- input_url.input(fn=loadfile, inputs=[input_url], outputs=[input_video])
1158
- submit = gr.Button("Submit")
1159
- with gr.Group():
1160
- output_frame = gr.Gallery(label="Frames", preview=True, columns=8192, interactive=False)
1161
- output_switch = gr.Checkbox(label="Show depths")
1162
- output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
1163
- selected = gr.Number(label="Selected frame", visible=False, elem_id="fnum", value=0, minimum=0, maximum=256, interactive=False)
1164
- with gr.Accordion(label="Depths", open=False):
1165
- output_depth = gr.Files(label="Depth files", interactive=False)
1166
- with gr.Group():
1167
- output_mask = gr.ImageEditor(layers=False, sources=('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")
1168
- with gr.Accordion(label="Border", open=False):
1169
- boffset = gr.Slider(label="Inner", value=1, maximum=256, minimum=0, step=1)
1170
- bsize = gr.Slider(label="Outer", value=32, maximum=256, minimum=0, step=1)
1171
- mouse = gr.Textbox(label="Mouse x,y", elem_id="mouse", value="""[]""", interactive=False)
1172
- reset = gr.Button("Reset", size='sm')
1173
- mouse.input(fn=draw_mask, show_progress="minimal", inputs=[boffset, bsize, mouse, output_mask], outputs=[output_mask])
1174
- reset.click(fn=reset_mask, inputs=[output_mask], outputs=[output_mask])
1175
-
1176
- with gr.Column():
1177
- model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl"), ("giant", "vitg")], type="value", value="vits", label='Model Type')
1178
- processed_video = gr.Video(label="Output Video", format="mp4", elem_id="output_video", interactive=False)
1179
- processed_zip = gr.File(label="Output Archive", interactive=False)
1180
- depth_video = gr.Video(label="Depth Video", format="mp4", elem_id="depth_video", interactive=False, visible=True)
1181
- result = gr.Model3D(label="3D Mesh", clear_color=[0.5, 0.5, 0.5, 0.0], camera_position=[0, 90, 512], zoom_speed=2.0, pan_speed=2.0, interactive=True, elem_id="model3D")
1182
- with gr.Accordion(label="Embed in website", open=False):
1183
- embed_model = gr.Textbox(elem_id="embed_model", label="Include this wherever the model is to appear on the page", interactive=False, value="""
1184
-
1185
- """)
1186
-
1187
- with gr.Tab("Blur"):
1188
- 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>
1189
- <style>
1190
- * {
1191
- user-select: none;
1192
- }
1193
- html, body {
1194
- user-select: none;
1195
- }
1196
- #model3D canvas {
1197
- user-select: none;
1198
- }
1199
- #chart hr {
1200
- width: 1px;
1201
- height: 1px;
1202
- clear: none;
1203
- border: 0;
1204
- padding:0;
1205
- display: inline-block;
1206
- position: relative;
1207
- vertical-align: top;
1208
- margin-top:32px;
1209
- }
1210
- #chart {
1211
- padding:0;
1212
- margin:0;
1213
- width:256px;
1214
- height:64px;
1215
- background-color:#808080;
1216
- touch-action: none;
1217
- }
1218
- #compass_box {
1219
- position:absolute;
1220
- top:2em;
1221
- right:3px;
1222
- border:1px dashed gray;
1223
- border-radius: 50%;
1224
- width:1.5em;
1225
- height:1.5em;
1226
- padding:0;
1227
- margin:0;
1228
- }
1229
- #compass {
1230
- position:absolute;
1231
- transform:rotate(0deg);
1232
- border:1px solid black;
1233
- border-radius: 50%;
1234
- width:100%;
1235
- height:100%;
1236
- padding:0;
1237
- margin:0;
1238
- line-height:1em;
1239
- letter-spacing:0;
1240
- }
1241
- #compass b {
1242
- margin-top:-1px;
1243
- }
1244
- </style>
1245
- """)
1246
- 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='
1247
- var pts_a = document.getElementById(\"blur_in\").getElementsByTagName(\"textarea\")[0].value.split(\" \");
1248
- for (var i=0; i<256; i++) {
1249
- var avg = 0;
1250
- var div = this.value;
1251
- for (var j = i-parseInt(this.value/2); j <= i+parseInt(this.value/2); j++) {
1252
- if (pts_a[j]) {
1253
- avg += parseInt(pts_a[j]);
1254
- } else if (div > 1) {
1255
- div--;
1256
- }
1257
- }
1258
- pts_a[i] = Math.round((avg / div - 1) / 2) * 2 + 1;
1259
-
1260
- document.getElementById(\"chart\").childNodes[i].style.height = pts_a[i] + \"px\";
1261
- document.getElementById(\"chart\").childNodes[i].style.marginTop = (64-pts_a[i])/2 + \"px\";
1262
- }
1263
- document.getElementById(\"blur_in\").getElementsByTagName(\"textarea\")[0].value = pts_a.join(\" \");
1264
-
1265
- var evt = document.createEvent(\"Event\");
1266
- evt.initEvent(\"input\", true, false);
1267
- document.getElementById(\"blur_in\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
1268
- ' oninput='
1269
- this.parentNode.childNodes[2].innerText = this.value;
1270
- ' onchange='this.click();'/><span>1</span>""")
1271
- with gr.Accordion(label="Levels", open=False):
1272
- blur_in = gr.Textbox(elem_id="blur_in", label="Kernel size", show_label=False, interactive=False, value=blurin)
1273
- with gr.Group():
1274
- with gr.Accordion(label="Locations", open=False):
1275
- output_frame.select(fn=select_frame, inputs=[output_mask], outputs=[output_mask, selected])
1276
- example_coords = """[
1277
- {"lat": 50.07379596793083, "lng": 14.437146122950555, "heading": 152.70303, "pitch": 2.607833999999997},
1278
- {"lat": 50.073799567020004, "lng": 14.437146774240507, "heading": 151.12973, "pitch": 2.8672300000000064},
1279
- {"lat": 50.07377647505558, "lng": 14.437161000659017, "heading": 151.41025, "pitch": 3.4802200000000028},
1280
- {"lat": 50.07379496839027, "lng": 14.437148958238538, "heading": 151.93391, "pitch": 2.843050000000005},
1281
- {"lat": 50.073823157821664, "lng": 14.437124189538856, "heading": 152.95769, "pitch": 4.233024999999998}
1282
- ]"""
1283
- coords = gr.Textbox(elem_id="coords", value=example_coords, label="Coordinates", interactive=False)
1284
- mesh_order = gr.Textbox(elem_id="order", value="", label="Order", interactive=False)
1285
- load_all = gr.Checkbox(label="Load all")
1286
-
1287
- with gr.Group():
1288
- camera = gr.HTML(value="""<div style='width:128px;height:128px;border:1px dotted gray;padding:0;margin:0;float:left;clear:none;' id='seek'></div>
1289
- <span style='max-width:50%;float:right;clear:none;text-align:right;'>
1290
- <a href='#' id='reset_cam' style='float:right;clear:none;color:white' onclick='
1291
- if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
1292
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata = {
1293
- screenshot: true,
1294
- pipeline: new BABYLON.DefaultRenderingPipeline(\"default\", true, BABYLON.Engine.LastCreatedScene, [BABYLON.Engine.LastCreatedScene.activeCamera])
1295
- }
1296
- }
1297
- BABYLON.Engine.LastCreatedScene.activeCamera.radius = 0;
1298
- BABYLON.Engine.LastCreatedScene.activeCamera.alpha = 0;
1299
- BABYLON.Engine.LastCreatedScene.activeCamera.beta = Math.PI / 2;
1300
-
1301
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.samples = 4;
1302
- BABYLON.Engine.LastCreatedScene.activeCamera.fov = document.getElementById(\"zoom\").value;
1303
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast = document.getElementById(\"contrast\").value;
1304
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure = document.getElementById(\"exposure\").value;
1305
-
1306
- 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)\";
1307
- document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
1308
- document.getElementById(\"model3D\").getElementsByTagName(\"canvas\")[0].ondrag = function(e){e.preventDefault();}
1309
- '>Reset camera</a><br/>
1310
- <span><label for='zoom' style='width:8em'>Zoom</label><input id='zoom' type='range' style='width:128px;height:1em;' value='0.8' min='0.157' max='1.57' step='0.001' oninput='
1311
- if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
1312
- var evt = document.createEvent(\"Event\");
1313
- evt.initEvent(\"click\", true, false);
1314
- document.getElementById(\"reset_cam\").dispatchEvent(evt);
1315
- }
1316
- BABYLON.Engine.LastCreatedScene.activeCamera.fov = this.value;
1317
- this.parentNode.childNodes[2].innerText = BABYLON.Engine.LastCreatedScene.activeCamera.fov;
1318
-
1319
- 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)\";
1320
- '/><span>0.8</span></span><br/>
1321
- <span><label for='pan' style='width:8em'>Pan</label><input id='pan' type='range' style='width:128px;height:1em;' value='0' min='-16' max='16' step='0.001' oninput='
1322
- if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
1323
- var evt = document.createEvent(\"Event\");
1324
- evt.initEvent(\"click\", true, false);
1325
- document.getElementById(\"reset_cam\").dispatchEvent(evt);
1326
- }
1327
- parallax = this.value;
1328
- rdir = BABYLON.Engine.LastCreatedScene.activeCamera.getDirection(xdir);
1329
- videoDomeMesh.position.x = parallax * rdir.x;
1330
- videoDomeMesh.position.z = parallax * rdir.z;
1331
- this.parentNode.childNodes[2].innerText = parallax;
1332
- '/><span>0.0</span></span><br/>
1333
- <span><label for='contrast' style='width:8em'>Contrast</label><input id='contrast' type='range' style='width:128px;height:1em;' value='1.0' min='0' max='2' step='0.001' oninput='
1334
- if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
1335
- var evt = document.createEvent(\"Event\");
1336
- evt.initEvent(\"click\", true, false);
1337
- document.getElementById(\"reset_cam\").dispatchEvent(evt);
1338
- }
1339
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast = this.value;
1340
- this.parentNode.childNodes[2].innerText = BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.contrast;
1341
- '/><span>1.0</span></span><br/>
1342
- <span><label for='exposure' style='width:8em'>Exposure</label><input id='exposure' type='range' style='width:128px;height:1em;' value='1.0' min='0' max='2' step='0.001' oninput='
1343
- if (!BABYLON.Engine.LastCreatedScene.activeCamera.metadata) {
1344
- var evt = document.createEvent(\"Event\");
1345
- evt.initEvent(\"click\", true, false);
1346
- document.getElementById(\"reset_cam\").dispatchEvent(evt);
1347
- }
1348
- BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure = this.value;
1349
- this.parentNode.childNodes[2].innerText = BABYLON.Engine.LastCreatedScene.activeCamera.metadata.pipeline.imageProcessing.exposure;
1350
- '/><span>1.0</span></span><br/>
1351
- <a href='#' onclick='snapshot();'>Screenshot</a>
1352
- <a href='#' onclick='record_video();'>Record</a>
1353
- <a href='#' onclick='stop_recording();'>Stop rec.</a>
1354
- <a href='#' onclick='videoPlay();'>Play</a></span>""")
1355
- snapshot = gr.HTML(value="""<img src='' id='img_out' onload='var ctxt = document.getElementById(\"cnv_out\").getContext(\"2d\");ctxt.drawImage(this, 0, 0);'/><br/>
1356
- <canvas id='cnv_out'></canvas>
1357
- <div id='compass_box'><div id='compass'><a id='fullscreen' onclick='
1358
- const model3D = document.getElementById(\"model3D\");
1359
- if (model3D.parentNode.tagName != \"BODY\") {
1360
- window.modelContainer = model3D.parentNode.id;
1361
- document.body.appendChild(model3D);
1362
- model3D.style.position = \"fixed\";
1363
- model3D.style.left = \"0\";
1364
- model3D.style.top = \"0\";
1365
- model3D.style.zIndex = \"100\";
1366
- document.getElementById(\"compass_box\").style.zIndex = \"101\";
1367
- } else {
1368
- document.getElementById(window.modelContainer).appendChild(model3D);
1369
- model3D.style.position = \"relative\";
1370
- model3D.style.left = \"0\";
1371
- model3D.style.top = \"0\";
1372
- model3D.style.zIndex = \"initial\";
1373
- document.getElementById(\"compass_box\").style.zIndex = \"initial\";
1374
- }'><b style='color:blue;'>◅</b>𝍠<b style='color:red;'>▻</b></a></div>
1375
- </div>
1376
- """)
1377
- render = gr.Button("Render")
1378
- input_json.input(show_json, inputs=[input_json], outputs=[processed_video, processed_zip, output_frame, output_mask, output_depth, coords])
1379
-
1380
-
1381
- def on_submit(uploaded_video,model_type,blur_in,boffset,bsize,coordinates):
1382
- global locations
1383
- locations = []
1384
- avg = [0, 0]
1385
-
1386
- locations = json.loads(coordinates)
1387
- for k, location in enumerate(locations):
1388
- if "tiles" in locations[k]:
1389
- locations[k]["heading"] = locations[k]["tiles"]["originHeading"]
1390
- locations[k]["pitch"] = locations[k]["tiles"]["originPitch"]
1391
- elif not "heading" in locations[k] or not "pitch" in locations[k]:
1392
- locations[k]["heading"] = 0.0
1393
- locations[k]["pitch"] = 0.0
1394
-
1395
- if "location" in locations[k]:
1396
- locations[k] = locations[k]["location"]["latLng"]
1397
- elif not "lat" in locations[k] or not "lng" in locations[k]:
1398
- locations[k]["lat"] = 0.0
1399
- locations[k]["lng"] = 0.0
1400
-
1401
- avg[0] = avg[0] + locations[k]["lat"]
1402
- avg[1] = avg[1] + locations[k]["lng"]
1403
-
1404
- if len(locations) > 0:
1405
- avg[0] = avg[0] / len(locations)
1406
- avg[1] = avg[1] / len(locations)
1407
-
1408
- for k, location in enumerate(locations):
1409
- lat = vincenty((location["lat"], 0), (avg[0], 0)) * 1000
1410
- lng = vincenty((0, location["lng"]), (0, avg[1])) * 1000
1411
- locations[k]["lat"] = float(lat / 2.5 * 111 * np.sign(location["lat"]-avg[0]))
1412
- locations[k]["lng"] = float(lng / 2.5 * 111 * np.sign(location["lng"]-avg[1]))
1413
- print(locations)
1414
- # 2.5m is height of camera on google street view car,
1415
- # distance from center of sphere to pavement roughly 255 - 144 = 111 units
1416
-
1417
- # Process the video and get the path of the output video
1418
- output_video_path = make_video(uploaded_video,encoder=model_type,blur_data=blurin,o=boffset,b=bsize)
1419
-
1420
- return output_video_path + (json.dumps(locations),)
1421
-
1422
- submit.click(on_submit, inputs=[input_video, model_type, blur_in, boffset, bsize, coords], outputs=[processed_video, processed_zip, output_frame, output_mask, output_depth, depth_video, coords])
1423
- render.click(None, inputs=[coords, mesh_order, output_frame, output_mask, selected, output_depth, output_switch], outputs=None, js=load_model)
1424
- render.click(partial(get_mesh), inputs=[output_frame, output_mask, blur_in, load_all], outputs=[result, mesh_order])
1425
-
1426
- example_files = [["./examples/streetview.mp4", "vits", blurin, 1, 32, example_coords]]
1427
- examples = gr.Examples(examples=example_files, fn=on_submit, cache_examples=True, inputs=[input_video, model_type, blur_in, boffset, bsize, coords], outputs=[processed_video, processed_zip, output_frame, output_mask, output_depth, depth_video, coords])
1428
-
1429
-
1430
- if __name__ == '__main__':
1431
- demo.queue().launch()