adpro commited on
Commit
3295ec4
·
verified ·
1 Parent(s): 7f500f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -4,41 +4,46 @@ 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")
15
 
16
- # forward pass
17
  with torch.no_grad():
18
- outputs = model(**encoding)
19
- predicted_depth = outputs.predicted_depth
20
 
21
- # interpolate to original size
22
  prediction = torch.nn.functional.interpolate(
23
- predicted_depth.unsqueeze(1),
24
- size=image.size[::-1],
25
- mode="bicubic",
26
- align_corners=False,
27
- ).squeeze()
 
 
28
  output = prediction.cpu().numpy()
29
  formatted = (output * 255 / np.max(output)).astype('uint8')
30
-
31
  img = Image.fromarray(formatted)
32
- return img
33
 
34
- title = "Demo: zero-shot depth estimation with DPT"
35
- description = "Demo for Intel's DPT, a Dense Prediction Transformer for state-of-the-art dense prediction tasks such as semantic segmentation and depth estimation."
 
 
 
36
 
 
 
 
 
 
 
 
 
37
 
38
- iface = gr.Interface(fn=process_image,
39
- inputs=gr.inputs.Image(type="pil"),
40
- outputs=gr.outputs.Image(type="pil", label="predicted depth"),
41
- title=title,
42
- description=description,
43
- enable_queue=True)
44
- iface.launch(debug=True)
 
4
  import numpy as np
5
  from PIL import Image
6
 
7
+ # Load model and feature extractor
 
8
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
9
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
10
+ model.eval()
11
 
12
  def process_image(image):
13
+ # Chuẩn hóa ảnh đầu vào
14
  encoding = feature_extractor(image, return_tensors="pt")
15
 
16
+ # Forward qua model
17
  with torch.no_grad():
18
+ outputs = model(**encoding)
19
+ predicted_depth = outputs.predicted_depth
20
 
21
+ # Resize output về đúng kích thước ảnh gốc
22
  prediction = torch.nn.functional.interpolate(
23
+ predicted_depth.unsqueeze(1),
24
+ size=image.size[::-1], # (H, W)
25
+ mode="bicubic",
26
+ align_corners=False
27
+ ).squeeze()
28
+
29
+ # Chuyển thành ảnh uint8
30
  output = prediction.cpu().numpy()
31
  formatted = (output * 255 / np.max(output)).astype('uint8')
 
32
  img = Image.fromarray(formatted)
 
33
 
34
+ return img
35
+
36
+ # Interface
37
+ title = "Demo: Zero-shot Depth Estimation with DPT"
38
+ description = "Intel's DPT: Dense Prediction Transformer for depth estimation from a single image."
39
 
40
+ iface = gr.Interface(
41
+ fn=process_image,
42
+ inputs=gr.Image(type="pil", label="Input Image"),
43
+ outputs=gr.Image(type="pil", label="Predicted Depth"),
44
+ title=title,
45
+ description=description,
46
+ allow_flagging="never"
47
+ )
48
 
49
+ iface.launch(debug=True)