radames commited on
Commit
05e4845
Β·
1 Parent(s): 2acb2ce

better layout

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +67 -21
  3. requirements.txt +1 -1
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸ‘€Β πŸ˜΅β€πŸ’«Β πŸ‘€
4
  colorFrom: green
5
  colorTo: black
6
  sdk: gradio
7
- sdk_version: 2.9.4
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
5
  colorTo: black
6
  sdk: gradio
7
+ sdk_version: 2.9b22
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -5,10 +5,9 @@ import torch
5
  import numpy as np
6
  from PIL import Image, ImageOps
7
  from pathlib import Path
8
- import os
9
  import glob
10
  from autostereogram.sirds_converter import SirdsConverter
11
- from skimage import color
12
 
13
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
14
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
@@ -46,28 +45,75 @@ def process_image(image_path):
46
  stereo_image = stereo_converter.convert_depth_to_stereogram_with_sird(
47
  depth_image_padded, False, 0.5).astype(np.uint8)
48
 
49
- return [depth_image_padded, stereo_image]
 
 
 
50
 
51
 
52
- title = "Demo: zero-shot depth estimation with DPT + 3D Voxels reconstruction"
53
- description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then reconstruct the 3D model as voxels."
54
 
55
- examples = sorted(glob.glob('examples/*.jpg'))
56
 
57
- iface = gr.Interface(fn=process_image,
58
- inputs=[
59
- gr.inputs.Image(
60
- type="filepath", label="Input Image")
61
- ],
62
- outputs=[
63
- gr.outputs.Image(label="Predicted Depth", type="pil"),
64
- gr.outputs.Image(label="Stereogram", type="pil")
65
- ],
66
- description=description,
67
- examples=examples,
68
- allow_flagging="never",
69
- # cache_examples=False
70
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  if __name__ == "__main__":
73
- iface.launch(debug=True, enable_queue=False)
 
5
  import numpy as np
6
  from PIL import Image, ImageOps
7
  from pathlib import Path
 
8
  import glob
9
  from autostereogram.sirds_converter import SirdsConverter
10
+ from datetime import datetime
11
 
12
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
13
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
 
45
  stereo_image = stereo_converter.convert_depth_to_stereogram_with_sird(
46
  depth_image_padded, False, 0.5).astype(np.uint8)
47
 
48
+ stereo_image_pil = Image.fromarray(stereo_image).convert('RGB')
49
+ image_name = f'stereo_image_{datetime.now().strftime("%Y%m%d_%H%M%S")}.jpg'
50
+ stereo_image_pil.save(image_name)
51
+ return [depth_image_padded, stereo_image, image_name]
52
 
53
 
54
+ examples_images = [[f] for f in sorted(glob.glob('examples/*.jpg'))]
 
55
 
56
+ blocks = gr.Blocks()
57
 
58
+ input_image = gr.Image(type="filepath", label="Input Image")
59
+ predicted_depth = gr.Image(label="Predicted Depth", type="pil")
60
+ autostereogram = gr.Image(label="Autostereogram", type="pil")
61
+ file_download = gr.File(label="Download Image")
62
+
63
+
64
+ def load_example(example_id):
65
+ processed_examples = [
66
+ component.preprocess_example(sample)
67
+ for component, sample in zip(
68
+ [input_image], examples_images[example_id]
69
+ )
70
+ ]
71
+ if len(processed_examples) == 1:
72
+ return processed_examples[0]
73
+ else:
74
+ return processed_examples
75
+
76
+
77
+ with blocks:
78
+ gr.Markdown('''
79
+ ## Depth Image to Autostereogram (Magic Eye)
80
+ This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
81
+ Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
82
+ to generate the autostereogram (Magic Eye)
83
+ <base target="_blank">
84
+
85
+ ''')
86
+
87
+ with gr.Row():
88
+ examples_c = gr.components.Dataset(
89
+ components=[input_image],
90
+ samples=examples_images,
91
+ type="index",
92
+ )
93
+
94
+ examples_c._click_no_postprocess(
95
+ load_example,
96
+ inputs=[examples_c],
97
+ outputs=[input_image])
98
+
99
+ with gr.Row():
100
+ with gr.Column():
101
+ button = gr.Button("Predict")
102
+ button.click(fn=process_image, inputs=[input_image],
103
+ outputs=[predicted_depth,
104
+ autostereogram, file_download],
105
+ )
106
+
107
+ with gr.Row():
108
+ with gr.Column():
109
+ input_image.render()
110
+ with gr.Column():
111
+ predicted_depth.render()
112
+ with gr.Row():
113
+ autostereogram.render()
114
+ with gr.Row():
115
+ with gr.Column():
116
+ file_download.render()
117
 
118
  if __name__ == "__main__":
119
+ blocks.launch(debug=True)
requirements.txt CHANGED
@@ -2,7 +2,7 @@ torch
2
  git+https://github.com/nielsrogge/transformers.git@add_dpt_redesign#egg=transformers
3
  numpy
4
  Pillow
5
- gradio==2.9.4
6
  jinja2
7
  transformers
8
  scikit-image
 
2
  git+https://github.com/nielsrogge/transformers.git@add_dpt_redesign#egg=transformers
3
  numpy
4
  Pillow
5
+ gradio==2.9b22
6
  jinja2
7
  transformers
8
  scikit-image