xelu3banh commited on
Commit
6075718
·
verified ·
1 Parent(s): f9a05f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -32
app.py CHANGED
@@ -1,45 +1,92 @@
 
1
  import gradio as gr
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")
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
- img = Image.fromarray(formatted)
31
- return img
32
-
33
- return result
34
-
35
- title = "Demo: zero-shot depth estimation with DPT"
36
- 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."
37
-
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
- enable_queue=True)
45
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from doctest import Example
2
  import gradio as gr
3
+ from transformers import DPTImageProcessor, DPTForDepthEstimation
4
  import torch
5
  import numpy as np
6
+ from PIL import Image, ImageOps
7
+ from pathlib import Path
8
+ import glob
9
+ from autostereogram.converter import StereogramConverter
10
+ from datetime import datetime
11
+ import time
12
+ import tempfile
13
 
14
+ feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
 
 
15
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
16
 
17
+ stereo_converter = StereogramConverter()
18
+
19
+
20
+ def process_image(image_path):
21
+ print("\n\n\n")
22
+ print("Processing image:", image_path)
23
+ last_time = time.time()
24
+ image_raw = Image.open(Path(image_path))
25
+
26
+ image = image_raw
27
+
28
  # prepare image for the model
29
  encoding = feature_extractor(image, return_tensors="pt")
30
+
31
  # forward pass
32
  with torch.no_grad():
33
+ outputs = model(**encoding)
34
+ predicted_depth = outputs.predicted_depth
35
+
36
  # interpolate to original size
37
  prediction = torch.nn.functional.interpolate(
38
+ predicted_depth.unsqueeze(1),
39
+ size=image.size[::-1],
40
+ mode="bicubic",
41
+ align_corners=False,
42
+ ).squeeze()
43
  output = prediction.cpu().numpy()
44
+ depth_image = (output * 255 / np.max(output)).astype("uint8")
45
+ depth_image_padded = np.array(
46
+ Image.fromarray(depth_image)
47
+ )
48
+
49
+
50
+ # Return as downloadable file
51
+ return depth_image_padded
52
+
53
+
54
+
55
+ examples_images = [[f] for f in sorted(glob.glob("examples/*.jpg"))]
56
+
57
+
58
+ with gr.Blocks() as blocks:
59
+ gr.Markdown(
60
+ """
61
+ ## Depth Image to Autostereogram (Magic Eye)
62
+ This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
63
+ Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
64
+ to generate the autostereogram (Magic Eye)
65
+ <base target="_blank">
66
+ """
67
+ )
68
+ with gr.Row():
69
+ with gr.Column():
70
+ input_image = gr.Image(type="filepath", label="Input Image")
71
+ button = gr.Button("Predict")
72
+ with gr.Column():
73
+ predicted_depth = gr.Image(label="Predicted Depth", type="pil")
74
+ with gr.Row():
75
+ autostereogram = gr.Image(label="Autostereogram", type="pil")
76
+ with gr.Row():
77
+ with gr.Column():
78
+ file_download = gr.File(label="Download Image")
79
+ with gr.Row():
80
+ gr.Examples(
81
+ examples=examples_images,
82
+ fn=process_image,
83
+ inputs=[input_image],
84
+ outputs=predicted_depth,
85
+ cache_examples=True,
86
+ )
87
+ button.click(
88
+ fn=process_image,
89
+ inputs=[input_image],
90
+ outputs=predicted_depth,
91
+ )
92
+ blocks.launch(debug=True)