radames commited on
Commit
56699da
β€’
1 Parent(s): bbbe5c5
Files changed (3) hide show
  1. README.md +3 -3
  2. app.py +41 -64
  3. requirements.txt +2 -3
README.md CHANGED
@@ -1,14 +1,14 @@
1
  ---
2
  title: Depth Image to Autostereogram (Magic Eye)
3
- emoji: πŸ‘€Β πŸ˜΅β€πŸ’«Β πŸ‘€
4
  colorFrom: green
5
  colorTo: black
6
  sdk: gradio
7
- sdk_version: 3.41.2
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
13
 
14
- Depth Image to Autostereogram (Magic Eye)
 
1
  ---
2
  title: Depth Image to Autostereogram (Magic Eye)
3
+ emoji: πŸ‘€Β πŸ˜΅β€πŸ’«Β πŸ‘€
4
  colorFrom: green
5
  colorTo: black
6
  sdk: gradio
7
+ sdk_version: 3.43.2
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
13
 
14
+ Depth Image to Autostereogram (Magic Eye)
app.py CHANGED
@@ -1,6 +1,6 @@
1
  from doctest import Example
2
  import gradio as gr
3
- from transformers import DPTFeatureExtractor, DPTForDepthEstimation
4
  import torch
5
  import numpy as np
6
  from PIL import Image, ImageOps
@@ -9,12 +9,14 @@ import glob
9
  from autostereogram.converter import StereogramConverter
10
  from datetime import datetime
11
  import time
 
12
 
13
- feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
14
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
15
 
16
  stereo_converter = StereogramConverter()
17
 
 
18
  def process_image(image_path):
19
  print("\n\n\n")
20
  print("Processing image:", image_path)
@@ -23,7 +25,8 @@ def process_image(image_path):
23
 
24
  image = image_raw.resize(
25
  (1280, int(1280 * image_raw.size[1] / image_raw.size[0])),
26
- Image.Resampling.LANCZOS)
 
27
 
28
  # prepare image for the model
29
  encoding = feature_extractor(image, return_tensors="pt")
@@ -41,85 +44,59 @@ def process_image(image_path):
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(ImageOps.pad(
46
- Image.fromarray(depth_image), (1280, 720)))
 
47
 
48
  stereo_image = stereo_converter.convert_depth_to_stereogram_with_thread_pool(
49
- depth_image_padded, False).astype(np.uint8)
50
-
51
- stereo_image_pil = Image.fromarray(stereo_image).convert('RGB')
52
- image_name = f'stereo_image_{datetime.now().strftime("%Y%m%d_%H%M%S")}.jpg'
53
- stereo_image_pil.save(image_name)
54
- print(time.time() - last_time)
55
- print("\n\n\n")
56
- return [depth_image_padded, stereo_image, image_name]
57
-
58
 
59
- examples_images = [[f] for f in sorted(glob.glob('examples/*.jpg'))]
 
 
 
60
 
61
- blocks = gr.Blocks()
62
-
63
- input_image = gr.Image(type="filepath", label="Input Image")
64
- predicted_depth = gr.Image(label="Predicted Depth", type="pil")
65
- autostereogram = gr.Image(label="Autostereogram", type="pil")
66
- file_download = gr.File(label="Download Image")
67
 
68
 
69
- def load_example(example_id):
70
- processed_examples = [
71
- component.preprocess_example(sample)
72
- for component, sample in zip(
73
- [input_image], examples_images[example_id]
74
- )
75
- ]
76
- if len(processed_examples) == 1:
77
- return processed_examples[0]
78
- else:
79
- return processed_examples
80
 
81
 
82
- with blocks:
83
- gr.Markdown('''
 
84
  ## Depth Image to Autostereogram (Magic Eye)
85
  This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
86
  Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
87
  to generate the autostereogram (Magic Eye)
88
  <base target="_blank">
89
 
90
- ''')
91
-
92
- with gr.Row():
93
- examples_c = gr.components.Dataset(
94
- components=[input_image],
95
- samples=examples_images,
96
- type="index",
97
- )
98
-
99
- examples_c.click(
100
- load_example,
101
- inputs=[examples_c],
102
- outputs=[input_image],
103
- postprocess=False,
104
- queue=False,
105
- )
106
-
107
  with gr.Row():
108
  with gr.Column():
109
- input_image.render()
110
  button = gr.Button("Predict")
111
- button.click(fn=process_image, inputs=[input_image],
112
- outputs=[predicted_depth,
113
- autostereogram, file_download],
114
- )
115
-
116
  with gr.Column():
117
- predicted_depth.render()
118
  with gr.Row():
119
- autostereogram.render()
120
  with gr.Row():
121
  with gr.Column():
122
- file_download.render()
123
-
124
- if __name__ == "__main__":
125
- blocks.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
 
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)
 
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")
 
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
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  with gr.Row():
79
  with gr.Column():
80
+ input_image = gr.Image(type="filepath", label="Input Image")
81
  button = gr.Button("Predict")
 
 
 
 
 
82
  with gr.Column():
83
+ predicted_depth = gr.Image(label="Predicted Depth", type="pil")
84
  with gr.Row():
85
+ autostereogram = gr.Image(label="Autostereogram", type="pil")
86
  with gr.Row():
87
  with gr.Column():
88
+ file_download = gr.File(label="Download Image")
89
+ with gr.Row():
90
+ gr.Examples(
91
+ examples=examples_images,
92
+ fn=process_image,
93
+ inputs=[input_image],
94
+ outputs=[predicted_depth, autostereogram, file_download],
95
+ cache_examples=True,
96
+ )
97
+ button.click(
98
+ fn=process_image,
99
+ inputs=[input_image],
100
+ outputs=[predicted_depth, autostereogram, file_download],
101
+ )
102
+ blocks.launch(debug=True)
requirements.txt CHANGED
@@ -1,8 +1,7 @@
1
  torch
2
- git+https://github.com/nielsrogge/transformers.git@add_dpt_redesign#egg=transformers
3
- numpy
4
  Pillow
5
- gradio==3.0b8
6
  jinja2
7
  transformers
8
  scikit-image
 
1
  torch
2
+ transformers
 
3
  Pillow
4
+ gradio==3.43.2
5
  jinja2
6
  transformers
7
  scikit-image