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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -13,20 +13,19 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
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
-
21
  # prepare image for the model
22
- # encoding = feature_extractor(image, return_tensors="pt")
23
- inputs = processor(images=image, return_tensors="pt")
24
-
25
  # forward pass
26
  with torch.no_grad():
27
- outputs = model(**encoding)
28
  predicted_depth = outputs.predicted_depth
29
-
30
  # interpolate to original size
31
  prediction = torch.nn.functional.interpolate(
32
  predicted_depth.unsqueeze(1),
@@ -34,11 +33,11 @@ def process_image(image_path):
34
  mode="bicubic",
35
  align_corners=False,
36
  ).squeeze()
 
37
  output = prediction.cpu().numpy()
38
  depth = (output * 255 / np.max(output)).astype('uint8')
39
-
40
- h = depthviewer2html(image,depth)
41
- return [h]
42
 
43
  title = "3d Visualization of Depth Maps Generated using MiDaS"
44
  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>"
@@ -54,4 +53,4 @@ iface = gr.Interface(fn=process_image,
54
  cache_examples=False,
55
  css='#depth-viewer: {height:300px;}')
56
 
57
- iface.launch(debug=True)
 
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
+
21
  # prepare image for the model
22
+ inputs = feature_extractor(images=image, return_tensors="pt")
23
+
 
24
  # forward pass
25
  with torch.no_grad():
26
+ outputs = model(**inputs)
27
  predicted_depth = outputs.predicted_depth
28
+
29
  # interpolate to original size
30
  prediction = torch.nn.functional.interpolate(
31
  predicted_depth.unsqueeze(1),
 
33
  mode="bicubic",
34
  align_corners=False,
35
  ).squeeze()
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>"
 
53
  cache_examples=False,
54
  css='#depth-viewer: {height:300px;}')
55
 
56
+ iface.launch(server_name="0.0.0.0", debug=True)