adpro commited on
Commit
9128101
·
verified ·
1 Parent(s): 5c7a7ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -28
app.py CHANGED
@@ -1,53 +1,94 @@
 
1
  import gradio as gr
2
  from transformers import DPTImageProcessor, DPTForDepthEstimation
3
  import torch
4
  import numpy as np
5
- from PIL import Image
6
- import io
 
 
 
 
 
7
 
8
- # Load model
9
  feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
10
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
- model.eval()
12
 
13
- def process_image(image):
14
- # Đảm bảo ảnh ở định dạng RGB
15
- if image.mode != "RGB":
16
- image = image.convert("RGB")
17
 
18
- # Encode và dự đoán
 
 
 
 
 
 
 
 
 
 
 
 
19
  encoding = feature_extractor(image, return_tensors="pt")
 
 
20
  with torch.no_grad():
21
  outputs = model(**encoding)
22
  predicted_depth = outputs.predicted_depth
23
 
24
- # Resize về ảnh gốc
25
  prediction = torch.nn.functional.interpolate(
26
  predicted_depth.unsqueeze(1),
27
  size=image.size[::-1],
28
  mode="bicubic",
29
- align_corners=False
30
  ).squeeze()
31
-
32
- # Chuyển depth map sang ảnh uint8
33
  output = prediction.cpu().numpy()
34
  depth_image = (output * 255 / np.max(output)).astype("uint8")
35
- depth_pil = Image.fromarray(depth_image)
 
 
 
 
 
 
36
 
37
- # Lưu vào memory buffer
38
- buffer = io.BytesIO()
39
- depth_pil.save(buffer, format="PNG")
40
- buffer.seek(0)
41
 
42
- return gr.File.update(value=buffer, filename="depth_output.png")
43
 
44
- # Giao diện Gradio dạng API (upload ảnh là xử lý luôn)
45
- demo = gr.Interface(
46
- fn=process_image,
47
- inputs=gr.Image(type="pil", label="Input Image"),
48
- outputs=gr.File(label="Depth Image (PNG)"),
49
- title="Depth Estimation API",
50
- description="Upload an image to get its depth map (as PNG file)"
51
- )
52
 
53
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
53
+
54
+ return depth_image_padded
55
 
 
 
 
 
56
 
57
+ examples_images = [[f] for f in sorted(glob.glob("examples/*.jpg"))]
58
 
 
 
 
 
 
 
 
 
59
 
60
+ with gr.Blocks() as blocks:
61
+ gr.Markdown(
62
+ """
63
+ ## Depth Image to Autostereogram (Magic Eye)
64
+ This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
65
+ Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
66
+ to generate the autostereogram (Magic Eye)
67
+ <base target="_blank">
68
+ """
69
+ )
70
+ with gr.Row():
71
+ with gr.Column():
72
+ input_image = gr.Image(type="filepath", label="Input Image")
73
+ button = gr.Button("Predict")
74
+ with gr.Column():
75
+ predicted_depth = gr.Image(label="Predicted Depth", type="pil")
76
+ with gr.Row():
77
+ autostereogram = gr.Image(label="Autostereogram", type="pil")
78
+ with gr.Row():
79
+ with gr.Column():
80
+ file_download = gr.File(label="Download Image")
81
+ with gr.Row():
82
+ gr.Examples(
83
+ examples=examples_images,
84
+ fn=process_image,
85
+ inputs=[input_image],
86
+ outputs=predicted_depth,
87
+ cache_examples=True,
88
+ )
89
+ button.click(
90
+ fn=process_image,
91
+ inputs=[input_image],
92
+ outputs=predicted_depth,
93
+ )
94
+ blocks.launch(debug=True)