adpro commited on
Commit
029b4b5
·
verified ·
1 Parent(s): c496f78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -32
app.py CHANGED
@@ -1,53 +1,101 @@
 
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
- # Load model và 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
- # Đảm bảo ảnh là RGB
14
- if image.mode != "RGB":
15
- image = image.convert("RGB")
16
 
17
- # Encode
 
 
 
 
 
 
 
 
 
 
 
 
18
  encoding = feature_extractor(image, return_tensors="pt")
19
-
20
- # Dự đoán depth
21
  with torch.no_grad():
22
  outputs = model(**encoding)
23
  predicted_depth = outputs.predicted_depth
24
-
25
- # Resize về kích thước ảnh gốc
26
  prediction = torch.nn.functional.interpolate(
27
  predicted_depth.unsqueeze(1),
28
  size=image.size[::-1],
29
  mode="bicubic",
30
- align_corners=False
31
  ).squeeze()
32
-
33
- # Chuẩn hóa và chuyển về ảnh uint8
34
  output = prediction.cpu().numpy()
35
- output = (output - np.min(output)) / (np.max(output) - np.min(output)) # normalize
36
- formatted = (output * 255).astype("uint8")
37
- depth_img = Image.fromarray(formatted)
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- return depth_img
40
- return result
41
 
42
- title = "Demo: zero-shot depth estimation with DPT"
43
- 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."
44
 
45
- iface = gr.Interface(
46
- fn=process_image,
47
- inputs=gr.inputs.Image(type="pil", label="Input Image"),
48
- outputs=gr.outputs.Image(type="pil", label="Predicted Depth"),
49
- title=title,
50
- description=description,
51
- )
52
 
53
- 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.resize(
27
+ (1280, int(1280 * image_raw.size[1] / image_raw.size[0])),
28
+ Image.Resampling.LANCZOS,
29
+ )
30
+
31
+ # prepare image for the model
32
  encoding = feature_extractor(image, return_tensors="pt")
33
+
34
+ # forward pass
35
  with torch.no_grad():
36
  outputs = model(**encoding)
37
  predicted_depth = outputs.predicted_depth
38
+
39
+ # interpolate to original size
40
  prediction = torch.nn.functional.interpolate(
41
  predicted_depth.unsqueeze(1),
42
  size=image.size[::-1],
43
  mode="bicubic",
44
+ align_corners=False,
45
  ).squeeze()
 
 
46
  output = prediction.cpu().numpy()
47
+ depth_image = (output * 255 / np.max(output)).astype("uint8")
48
+ depth_image_padded = np.array(
49
+ ImageOps.pad(Image.fromarray(depth_image), (1280, 720))
50
+ )
51
+
52
+ stereo_image = stereo_converter.convert_depth_to_stereogram_with_thread_pool(
53
+ depth_image_padded, False
54
+ ).astype(np.uint8)
55
+
56
+ stereo_image_pil = Image.fromarray(stereo_image).convert("RGB")
57
+ with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
58
+ image_name = f.name
59
+ stereo_image_pil.save(image_name)
60
+
61
+ return [depth_image_padded, stereo_image, image_name]
62
 
 
 
63
 
64
+ examples_images = [[f] for f in sorted(glob.glob("examples/*.jpg"))]
 
65
 
 
 
 
 
 
 
 
66
 
67
+ with gr.Blocks() as blocks:
68
+ gr.Markdown(
69
+ """
70
+ ## Depth Image to Autostereogram (Magic Eye)
71
+ This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
72
+ Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
73
+ to generate the autostereogram (Magic Eye)
74
+ <base target="_blank">
75
+ """
76
+ )
77
+ with gr.Row():
78
+ with gr.Column():
79
+ input_image = gr.Image(type="filepath", label="Input Image")
80
+ button = gr.Button("Predict")
81
+ with gr.Column():
82
+ predicted_depth = gr.Image(label="Predicted Depth", type="pil")
83
+ with gr.Row():
84
+ autostereogram = gr.Image(label="Autostereogram", type="pil")
85
+ with gr.Row():
86
+ with gr.Column():
87
+ file_download = gr.File(label="Download Image")
88
+ with gr.Row():
89
+ gr.Examples(
90
+ examples=examples_images,
91
+ fn=process_image,
92
+ inputs=[input_image],
93
+ outputs=[predicted_depth, autostereogram, file_download],
94
+ cache_examples=True,
95
+ )
96
+ button.click(
97
+ fn=process_image,
98
+ inputs=[input_image],
99
+ outputs=[predicted_depth, autostereogram, file_download],
100
+ )
101
+ blocks.launch(debug=True)