freealise commited on
Commit
2c7bac7
·
verified ·
1 Parent(s): 25e4e4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -4
app.py CHANGED
@@ -240,7 +240,7 @@ def make_video(video_path, outdir='./vis_video_depth', encoder='vits', blur_data
240
  count += 1
241
 
242
  final_vid = create_video(orig_frames, frame_rate, "orig")
243
- #final_vid = create_video(depth_frames, frame_rate, "depth")
244
 
245
  final_zip = zip_files(orig_frames, depth_frames)
246
  raw_video.release()
@@ -263,7 +263,7 @@ def make_video(video_path, outdir='./vis_video_depth', encoder='vits', blur_data
263
  else:
264
  gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
265
 
266
- return final_vid, final_zip, frames, masks[frame_selected], depths #output_path
267
 
268
  def depth_edges_mask(depth):
269
  """Returns a mask of edges in the depth map.
@@ -674,6 +674,9 @@ async(c, o, p, d, n, m)=>{
674
  window.y = 0;
675
  window.xold = 0;
676
  window.yold = 0;
 
 
 
677
 
678
  document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointermove', function(evt) {
679
  if (md === true) {
@@ -719,6 +722,85 @@ async(c, o, p, d, n, m)=>{
719
  document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointerout', function() {
720
  md = false;
721
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
722
 
723
  if (document.getElementById("model")) {
724
  document.getElementById("model").appendChild(document.getElementById("model3D"));
@@ -966,6 +1048,7 @@ with gr.Blocks(css=css, js=js, head=head) as demo:
966
  model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl"), ("giant", "vitg")], type="value", value="vits", label='Model Type')
967
  processed_video = gr.Video(label="Output Video", format="mp4", interactive=False)
968
  processed_zip = gr.File(label="Output Archive", interactive=False)
 
969
  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")
970
  with gr.Tab("Blur"):
971
  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>
@@ -1184,12 +1267,12 @@ with gr.Blocks(css=css, js=js, head=head) as demo:
1184
 
1185
  return output_video_path + (json.dumps(locations),)
1186
 
1187
- 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, coords])
1188
  render.click(None, inputs=[coords, mesh_order, output_frame, output_mask, selected, output_depth], outputs=None, js=load_model)
1189
  render.click(partial(get_mesh), inputs=[output_frame, output_mask, blur_in, load_all], outputs=[result, mesh_order])
1190
 
1191
  example_files = [["./examples/streetview.mp4", "vits", blurin, 1, 32, example_coords]]
1192
- 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, coords])
1193
 
1194
 
1195
  if __name__ == '__main__':
 
240
  count += 1
241
 
242
  final_vid = create_video(orig_frames, frame_rate, "orig")
243
+ depth_vid = create_video(depth_frames, frame_rate, "depth")
244
 
245
  final_zip = zip_files(orig_frames, depth_frames)
246
  raw_video.release()
 
263
  else:
264
  gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
265
 
266
+ return final_vid, final_zip, frames, masks[frame_selected], depths, depth_vid #output_path
267
 
268
  def depth_edges_mask(depth):
269
  """Returns a mask of edges in the depth map.
 
674
  window.y = 0;
675
  window.xold = 0;
676
  window.yold = 0;
677
+ window.buffer = null;
678
+ window.ctx = null;
679
+ window.video = document.getElementById("depth_video").getElementsByTagName("video")[0];
680
 
681
  document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointermove', function(evt) {
682
  if (md === true) {
 
722
  document.getElementById("model3D").getElementsByTagName("canvas")[0].addEventListener('pointerout', function() {
723
  md = false;
724
  });
725
+
726
+ video.addEventListener("playing", function () {
727
+ const canvas = document.createElement("canvas");
728
+ canvas.width = video.videoWidth;
729
+ canvas.height = video.videoHeight;
730
+ ctx = canvas.getContext("2d", { willReadFrequently: true });
731
+
732
+ updateMap();
733
+ });
734
+
735
+ function requestMap() {
736
+ ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
737
+ if (buffer) {
738
+ applyDisplacementMapFromBuffer(sceneToRender.meshes[sceneToRender.meshes.length-1], buffer, video.videoWidth, video.videoHeight, 0, -1, null, null, true);
739
+ }
740
+ buffer = ctx.getImageData(0, 0, video.videoWidth, video.videoHeight).data;
741
+ applyDisplacementMapFromBuffer(BABYLON.Engine.LastCreatedScene.meshes[BABYLON.Engine.LastCreatedScene.meshes.length-1], buffer, video.videoWidth, video.videoHeight, 0, 1, null, null, true);
742
+ setTimeout(updateMap, 40);
743
+ }
744
+
745
+ function updateMap() {
746
+ requestAnimationFrame(requestMap);
747
+ }
748
+
749
+ function applyDisplacementMapFromBuffer(
750
+ mesh,
751
+ buffer,
752
+ heightMapWidth,
753
+ heightMapHeight,
754
+ minHeight,
755
+ maxHeight,
756
+ uvOffset,
757
+ uvScale,
758
+ forceUpdate
759
+ ) {
760
+ if (!mesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind) || !mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind) || !mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
761
+ alert("Cannot call applyDisplacementMap: Given mesh is not complete. Position, Normal or UV are missing");
762
+ return mesh;
763
+ }
764
+
765
+ const positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind, true, true);
766
+ const normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
767
+ const uvs = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
768
+
769
+ let position = BABYLON.Vector3.Zero();
770
+ const normal = BABYLON.Vector3.Zero();
771
+ const uv = BABYLON.Vector2.Zero();
772
+
773
+ uvOffset = uvOffset || BABYLON.Vector2.Zero();
774
+ uvScale = uvScale || new BABYLON.Vector2(1, 1);
775
+
776
+ for (let index = 0; index < positions.length; index += 3) {
777
+ BABYLON.Vector3.FromArrayToRef(positions, index, position);
778
+ BABYLON.Vector3.FromArrayToRef(normals, index, normal);
779
+ BABYLON.Vector2.FromArrayToRef(uvs, (index / 3) * 2, uv);
780
+
781
+ // Compute height
782
+ const u = (Math.abs(uv.x * uvScale.x + (uvOffset.x % 1)) * (heightMapWidth - 1)) % heightMapWidth | 0;
783
+ const v = (Math.abs(uv.y * uvScale.y + (uvOffset.y % 1)) * (heightMapHeight - 1)) % heightMapHeight | 0;
784
+
785
+ const pos = (u + v * heightMapWidth) * 4;
786
+ const r = buffer[pos] / 255.0;
787
+ const g = buffer[pos + 1] / 255.0;
788
+ const b = buffer[pos + 2] / 255.0;
789
+ const a = buffer[pos + 3] / 255.0;
790
+
791
+ const gradient = r * 0.33 + g * 0.33 + b * 0.33;
792
+ //const gradient = a;
793
+
794
+ normal.normalize();
795
+ normal.scaleInPlace(minHeight + (maxHeight - minHeight) * gradient);
796
+ position = position.add(normal);
797
+
798
+ position.toArray(positions, index);
799
+ }
800
+
801
+ mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
802
+ return mesh;
803
+ }
804
 
805
  if (document.getElementById("model")) {
806
  document.getElementById("model").appendChild(document.getElementById("model3D"));
 
1048
  model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl"), ("giant", "vitg")], type="value", value="vits", label='Model Type')
1049
  processed_video = gr.Video(label="Output Video", format="mp4", interactive=False)
1050
  processed_zip = gr.File(label="Output Archive", interactive=False)
1051
+ depth_video = gr.Video(label="Depth Video", format="mp4", elem_id="depth_video", interactive=False, visible=False)
1052
  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")
1053
  with gr.Tab("Blur"):
1054
  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>
 
1267
 
1268
  return output_video_path + (json.dumps(locations),)
1269
 
1270
+ 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])
1271
  render.click(None, inputs=[coords, mesh_order, output_frame, output_mask, selected, output_depth], outputs=None, js=load_model)
1272
  render.click(partial(get_mesh), inputs=[output_frame, output_mask, blur_in, load_all], outputs=[result, mesh_order])
1273
 
1274
  example_files = [["./examples/streetview.mp4", "vits", blurin, 1, 32, example_coords]]
1275
+ 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])
1276
 
1277
 
1278
  if __name__ == '__main__':