Update app.py
Browse files
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
|
| 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 |
-
|
| 40 |
-
return
|
| 41 |
|
| 42 |
-
title = "
|
| 43 |
-
description = "Improved 3D interactive depth viewer using Three.js embedded in a Gradio app.
|
| 44 |
-
examples = [["examples/owl1.jpg"],['examples/marsattacks.jpg'],['examples/kitten.jpg']]
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
| 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")
|