johnrobinsn commited on
Commit
ff24905
·
verified ·
1 Parent(s): b676e08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -5,16 +5,18 @@ import numpy as np
5
  from PIL import Image
6
  from pathlib import Path
7
  from depth_viewer import depthviewer2html
8
- import cv2
9
 
10
  feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
11
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
12
 
13
  def process_image(image_path):
 
 
 
14
  image_path = Path(image_path)
15
  image = Image.open(image_path)
16
 
17
- # if wider than 512 pixels let's resample to keep it performant on phones etc
18
  if image.size[0] > 512:
19
  image = image.resize((512, int(512 * image.size[1] / image.size[0])), Image.Resampling.LANCZOS)
20
 
@@ -36,21 +38,26 @@ def process_image(image_path):
36
 
37
  output = prediction.cpu().numpy()
38
  depth = (output * 255 / np.max(output)).astype('uint8')
39
- h = depthviewer2html(image, depth)
40
- return h # No need for list wrapper with single output
41
 
42
- title = "3d Visualization of Depth Maps Generated using MiDaS"
43
- description = "Improved 3D interactive depth viewer using Three.js embedded in a Gradio app. For more details see the <a href='https://colab.research.google.com/drive/1l2l8U7Vhq9RnvV2tHyfhrXKNuHfmb4IP?usp=sharing'>Colab Notebook.</a>"
44
- examples = [["examples/owl1.jpg"],['examples/marsattacks.jpg'],['examples/kitten.jpg']]
45
 
46
- iface = gr.Interface(fn=process_image,
47
- inputs=[gr.Image(type="filepath",label="Input Image")],
48
- outputs=[gr.HTML(label='Depth Viewer',elem_id='depth-viewer')],
49
- title=title,
50
- description=description,
51
- examples=examples,
52
- allow_flagging="never",
53
- cache_examples=False,
54
- css='#depth-viewer: {height:300px;}')
 
 
 
 
 
 
55
 
56
- iface.launch(server_name="0.0.0.0", debug=True)
 
5
  from PIL import Image
6
  from pathlib import Path
7
  from depth_viewer import depthviewer2html
 
8
 
9
  feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
10
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
 
12
  def process_image(image_path):
13
+ if image_path is None:
14
+ return ""
15
+
16
  image_path = Path(image_path)
17
  image = Image.open(image_path)
18
 
19
+ # if wider than 512 pixels let's resample to keep it performant
20
  if image.size[0] > 512:
21
  image = image.resize((512, int(512 * image.size[1] / image.size[0])), Image.Resampling.LANCZOS)
22
 
 
38
 
39
  output = prediction.cpu().numpy()
40
  depth = (output * 255 / np.max(output)).astype('uint8')
41
+
42
+ return depthviewer2html(image, depth)
43
 
44
+ title = "3D Visualization of Depth Maps Generated using MiDaS"
45
+ description = "Improved 3D interactive depth viewer using Three.js embedded in a Gradio app."
 
46
 
47
+ with gr.Blocks(css="#depth-viewer { height: 600px; }") as demo:
48
+ gr.Markdown(f"# {title}")
49
+ gr.Markdown(description)
50
+
51
+ with gr.Row():
52
+ input_image = gr.Image(type="filepath", label="Input Image")
53
+
54
+ output_html = gr.HTML(label="Depth Viewer", elem_id="depth-viewer")
55
+
56
+ input_image.change(fn=process_image, inputs=input_image, outputs=output_html)
57
+
58
+ gr.Examples(
59
+ examples=[["examples/owl1.jpg"], ["examples/marsattacks.jpg"], ["examples/kitten.jpg"]],
60
+ inputs=input_image
61
+ )
62
 
63
+ demo.launch(server_name="0.0.0.0")