nielsr HF staff commited on
Commit
4e9eaed
1 Parent(s): b048fe5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -17
app.py CHANGED
@@ -2,25 +2,13 @@ import gradio as gr
2
  from transformers import DPTFeatureExtractor, DPTForDepthEstimation
3
  import torch
4
  import numpy as np
 
5
 
6
  torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
7
 
8
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
9
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
10
 
11
- def compute_depth(depth, bits):
12
- depth_min = depth.min()
13
- depth_max = depth.max()
14
-
15
- max_val = (2 ** (8 * bits)) - 1
16
-
17
- if depth_max - depth_min > np.finfo("float").eps:
18
- out = max_val * (depth - depth_min) / (depth_max - depth_min)
19
- else:
20
- out = np.zeros(depth.shape, dtype=depth.dtype)
21
-
22
- return out/65536
23
-
24
  def process_image(image):
25
  # prepare image for the model
26
  encoding = feature_extractor(image, return_tensors="pt")
@@ -37,9 +25,10 @@ def process_image(image):
37
  mode="bicubic",
38
  align_corners=False,
39
  )
40
- prediction = prediction.squeeze().cpu().numpy()
41
-
42
- result = compute_depth(prediction, bits=2)
 
43
 
44
  return result
45
 
@@ -49,7 +38,7 @@ examples =[['cats.jpg']]
49
 
50
  iface = gr.Interface(fn=process_image,
51
  inputs=gr.inputs.Image(type="pil"),
52
- outputs=gr.outputs.Image(label="predicted depth"),
53
  title=title,
54
  description=description,
55
  examples=examples,
 
2
  from transformers import DPTFeatureExtractor, DPTForDepthEstimation
3
  import torch
4
  import numpy as np
5
+ from PIL import Image
6
 
7
  torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
8
 
9
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
10
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def process_image(image):
13
  # prepare image for the model
14
  encoding = feature_extractor(image, return_tensors="pt")
 
25
  mode="bicubic",
26
  align_corners=False,
27
  )
28
+ output = prediction.cpu().numpy()
29
+ formatted = (output * 255 / np.max(output)).astype('uint8')
30
+ img = Image.fromarray(formatted)
31
+ return img
32
 
33
  return result
34
 
 
38
 
39
  iface = gr.Interface(fn=process_image,
40
  inputs=gr.inputs.Image(type="pil"),
41
+ outputs=gr.outputs.Image(type="pil", label="predicted depth"),
42
  title=title,
43
  description=description,
44
  examples=examples,