Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -111,10 +111,6 @@ print("Model loaded successfully!")
|
|
111 |
def predict_depth(input_image, colormap_choice):
|
112 |
"""Main depth prediction function"""
|
113 |
try:
|
114 |
-
# Handle case when no image is provided
|
115 |
-
if input_image is None:
|
116 |
-
return None
|
117 |
-
|
118 |
image_tensor, original_size = preprocess_image(input_image)
|
119 |
|
120 |
if torch.cuda.is_available():
|
@@ -136,159 +132,71 @@ def predict_depth(input_image, colormap_choice):
|
|
136 |
return None
|
137 |
|
138 |
|
139 |
-
|
140 |
-
"""Capture image from camera and predict depth"""
|
141 |
-
return predict_depth(camera_image, colormap_choice)
|
142 |
-
|
143 |
-
|
144 |
-
with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.themes.Soft(), css="""
|
145 |
-
.image-container {
|
146 |
-
display: flex !important;
|
147 |
-
align-items: flex-start !important;
|
148 |
-
justify-content: center !important;
|
149 |
-
}
|
150 |
-
.gradio-image {
|
151 |
-
vertical-align: top !important;
|
152 |
-
}
|
153 |
-
""") as demo:
|
154 |
gr.Markdown("""
|
155 |
# π Depth Anything AC - Depth Estimation Demo
|
156 |
|
157 |
-
Upload an image
|
158 |
|
159 |
## How to Use
|
160 |
-
1.
|
161 |
-
2.
|
162 |
-
3.
|
163 |
-
4.
|
164 |
-
5. View the results and download
|
165 |
""")
|
166 |
|
167 |
-
# Input controls row
|
168 |
with gr.Row():
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
label="Input Source"
|
173 |
-
)
|
174 |
-
colormap_choice = gr.Dropdown(
|
175 |
-
choices=["Spectral", "Inferno", "Gray"],
|
176 |
-
value="Spectral",
|
177 |
-
label="Colormap Style"
|
178 |
-
)
|
179 |
-
submit_btn = gr.Button(
|
180 |
-
"π― Generate Depth Map",
|
181 |
-
variant="primary",
|
182 |
-
size="lg"
|
183 |
-
)
|
184 |
-
|
185 |
-
# Labels row
|
186 |
-
with gr.Row():
|
187 |
-
gr.HTML("<h3 style='text-align: center; margin: 10px;'>π· Input Image</h3>")
|
188 |
-
gr.HTML("<h3 style='text-align: center; margin: 10px;'>π Depth Map Result</h3>")
|
189 |
-
|
190 |
-
# Images row - force vertical alignment
|
191 |
-
with gr.Row(equal_height=True):
|
192 |
-
# Left column for input
|
193 |
-
with gr.Column(scale=1):
|
194 |
-
# Upload image component
|
195 |
-
upload_image = gr.Image(
|
196 |
type="pil",
|
197 |
-
height=
|
198 |
-
visible=True,
|
199 |
-
show_label=False,
|
200 |
-
container=False
|
201 |
)
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
height=450,
|
208 |
-
visible=False,
|
209 |
-
show_label=False,
|
210 |
-
container=False
|
211 |
)
|
212 |
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
height=450,
|
218 |
-
show_label=False,
|
219 |
-
container=False
|
220 |
)
|
221 |
|
222 |
-
|
223 |
-
|
224 |
-
label="
|
225 |
-
|
226 |
-
|
227 |
-
visible=False
|
228 |
)
|
229 |
|
230 |
-
# Function to switch between upload and camera input
|
231 |
-
def switch_input_source(source):
|
232 |
-
if source == "Upload Image":
|
233 |
-
return gr.update(visible=True), gr.update(visible=False)
|
234 |
-
else:
|
235 |
-
return gr.update(visible=False), gr.update(visible=True)
|
236 |
-
|
237 |
-
# Update visibility based on input source selection
|
238 |
-
input_source.change(
|
239 |
-
fn=switch_input_source,
|
240 |
-
inputs=[input_source],
|
241 |
-
outputs=[upload_image, camera_image]
|
242 |
-
)
|
243 |
-
|
244 |
-
# Function to handle both input sources
|
245 |
-
def handle_prediction(input_source, upload_img, camera_img, colormap):
|
246 |
-
if input_source == "Upload Image":
|
247 |
-
result = predict_depth(upload_img, colormap)
|
248 |
-
else:
|
249 |
-
result = predict_depth(camera_img, colormap)
|
250 |
-
|
251 |
-
# Show download button when depth map is generated
|
252 |
-
if result is not None:
|
253 |
-
# Save result to temporary file for download
|
254 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
255 |
-
result.save(temp_file.name)
|
256 |
-
return result, gr.update(visible=True, value=temp_file.name)
|
257 |
-
else:
|
258 |
-
return None, gr.update(visible=False)
|
259 |
-
|
260 |
-
# Examples section
|
261 |
gr.Examples(
|
262 |
examples=[
|
263 |
["toyset/1.png", "Spectral"],
|
264 |
["toyset/2.png", "Spectral"],
|
265 |
["toyset/good.png", "Spectral"],
|
266 |
] if os.path.exists("toyset") else [],
|
267 |
-
inputs=[
|
268 |
outputs=output_image,
|
269 |
fn=predict_depth,
|
270 |
cache_examples=False,
|
271 |
label="Try these example images"
|
272 |
)
|
273 |
|
274 |
-
# Submit button click handler
|
275 |
submit_btn.click(
|
276 |
-
fn=
|
277 |
-
inputs=[
|
278 |
-
outputs=
|
279 |
show_progress=True
|
280 |
)
|
281 |
|
282 |
gr.Markdown("""
|
283 |
-
## π
|
284 |
- **Spectral**: Rainbow spectrum with distinct near-far contrast
|
285 |
-
- **Inferno**: Flame spectrum with warm tones
|
286 |
-
- **Gray**:
|
287 |
-
|
288 |
-
## π· Camera Tips
|
289 |
-
- Make sure to allow camera access when prompted
|
290 |
-
- Click the camera button to capture the current frame
|
291 |
-
- The captured image will be used as input for depth estimation
|
292 |
""")
|
293 |
|
294 |
|
|
|
111 |
def predict_depth(input_image, colormap_choice):
|
112 |
"""Main depth prediction function"""
|
113 |
try:
|
|
|
|
|
|
|
|
|
114 |
image_tensor, original_size = preprocess_image(input_image)
|
115 |
|
116 |
if torch.cuda.is_available():
|
|
|
132 |
return None
|
133 |
|
134 |
|
135 |
+
with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
gr.Markdown("""
|
137 |
# π Depth Anything AC - Depth Estimation Demo
|
138 |
|
139 |
+
Upload an image and AI will generate the corresponding depth map! Different colors in the depth map represent different distances, allowing you to see the three-dimensional structure of the image.
|
140 |
|
141 |
## How to Use
|
142 |
+
1. Click the upload area to select an image
|
143 |
+
2. Choose your preferred colormap style
|
144 |
+
3. Click the "Generate Depth Map" button
|
145 |
+
4. View the results and download
|
|
|
146 |
""")
|
147 |
|
|
|
148 |
with gr.Row():
|
149 |
+
with gr.Column():
|
150 |
+
input_image = gr.Image(
|
151 |
+
label="Upload Image",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
type="pil",
|
153 |
+
height=400
|
|
|
|
|
|
|
154 |
)
|
155 |
|
156 |
+
colormap_choice = gr.Dropdown(
|
157 |
+
choices=["Spectral", "Inferno", "Gray"],
|
158 |
+
value="Spectral",
|
159 |
+
label="Colormap"
|
|
|
|
|
|
|
|
|
160 |
)
|
161 |
|
162 |
+
submit_btn = gr.Button(
|
163 |
+
"π― Generate Depth Map",
|
164 |
+
variant="primary",
|
165 |
+
size="lg"
|
|
|
|
|
|
|
166 |
)
|
167 |
|
168 |
+
with gr.Column():
|
169 |
+
output_image = gr.Image(
|
170 |
+
label="Depth Map Result",
|
171 |
+
type="pil",
|
172 |
+
height=400
|
|
|
173 |
)
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
gr.Examples(
|
176 |
examples=[
|
177 |
["toyset/1.png", "Spectral"],
|
178 |
["toyset/2.png", "Spectral"],
|
179 |
["toyset/good.png", "Spectral"],
|
180 |
] if os.path.exists("toyset") else [],
|
181 |
+
inputs=[input_image, colormap_choice],
|
182 |
outputs=output_image,
|
183 |
fn=predict_depth,
|
184 |
cache_examples=False,
|
185 |
label="Try these example images"
|
186 |
)
|
187 |
|
|
|
188 |
submit_btn.click(
|
189 |
+
fn=predict_depth,
|
190 |
+
inputs=[input_image, colormap_choice],
|
191 |
+
outputs=output_image,
|
192 |
show_progress=True
|
193 |
)
|
194 |
|
195 |
gr.Markdown("""
|
196 |
+
## π Notes
|
197 |
- **Spectral**: Rainbow spectrum with distinct near-far contrast
|
198 |
+
- **Inferno**: Flame spectrum with warm tones
|
199 |
+
- **Gray**: Grayscale with classic effect
|
|
|
|
|
|
|
|
|
|
|
200 |
""")
|
201 |
|
202 |
|