nktssk commited on
Commit
4daa7b9
·
verified ·
1 Parent(s): 857662e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -1,40 +1,45 @@
1
  import os
2
  import gradio as gr
3
- from transformers import pipeline
4
-
5
  import torch.nn.functional as F
 
6
  from PIL import Image
 
7
 
8
- depth_estimator = pipeline(task="depth-estimation",
9
- model="Intel/dpt-hybrid-midas")
10
 
11
  def launch(input_image):
12
  out = depth_estimator(input_image)
13
 
14
- # resize the prediction
 
 
 
 
15
  prediction = F.interpolate(
16
- out["predicted_depth"].unsqueeze(1),
17
- size=input_image.size[::-1],
18
  mode="bicubic",
19
  align_corners=False,
20
  )
21
 
22
- # normalize the prediction
23
  output = prediction.squeeze().numpy()
24
  formatted = (output * 255 / np.max(output)).astype("uint8")
25
  depth = Image.fromarray(formatted)
26
  return depth
27
 
28
- iface = gr.Interface(launch,
29
- inputs=gr.Image(type='pil'),
30
- outputs=gr.Image(type='pil'))
 
 
31
 
32
  demo = gr.Blocks()
33
 
34
  with demo:
35
  gr.TabbedInterface(
36
  [iface],
37
- ["iface"],
38
  )
39
 
40
- demo.launch(debug=True)
 
1
  import os
2
  import gradio as gr
3
+ import torch
 
4
  import torch.nn.functional as F
5
+ import numpy as np
6
  from PIL import Image
7
+ from transformers import pipeline
8
 
9
+ depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-hybrid-midas")
 
10
 
11
  def launch(input_image):
12
  out = depth_estimator(input_image)
13
 
14
+ predicted_depth = torch.tensor(out["predicted_depth"])
15
+
16
+ if len(predicted_depth.shape) == 2: # Если двумерен, добавляем оси
17
+ predicted_depth = predicted_depth.unsqueeze(0).unsqueeze(0)
18
+
19
  prediction = F.interpolate(
20
+ predicted_depth,
21
+ size=input_image.size[::-1], # Порядок: (ширина, высота)
22
  mode="bicubic",
23
  align_corners=False,
24
  )
25
 
 
26
  output = prediction.squeeze().numpy()
27
  formatted = (output * 255 / np.max(output)).astype("uint8")
28
  depth = Image.fromarray(formatted)
29
  return depth
30
 
31
+ iface = gr.Interface(
32
+ launch,
33
+ inputs=gr.Image(type="pil"),
34
+ outputs=gr.Image(type="pil"),
35
+ )
36
 
37
  demo = gr.Blocks()
38
 
39
  with demo:
40
  gr.TabbedInterface(
41
  [iface],
42
+ ["Depth Estimation Interface"],
43
  )
44
 
45
+ demo.launch(debug=True)