ghost233lism commited on
Commit
a63396a
Β·
verified Β·
1 Parent(s): 81273c4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -122
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
- def capture_and_predict(camera_image, colormap_choice):
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 or use your camera to generate corresponding depth maps! Different colors in the depth map represent different distances, allowing you to see the three-dimensional structure of the image.
158
 
159
  ## How to Use
160
- 1. **Upload Mode**: Click the upload area to select an image file
161
- 2. **Camera Mode**: Use your camera to capture a live image
162
- 3. Choose your preferred colormap style
163
- 4. Click the "Generate Depth Map" button
164
- 5. View the results and download
165
  """)
166
 
167
- # Input controls row
168
  with gr.Row():
169
- input_source = gr.Radio(
170
- choices=["Upload Image", "Use Camera"],
171
- value="Upload Image",
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=450,
198
- visible=True,
199
- show_label=False,
200
- container=False
201
  )
202
 
203
- # Camera component
204
- camera_image = gr.Image(
205
- type="pil",
206
- sources=["webcam"],
207
- height=450,
208
- visible=False,
209
- show_label=False,
210
- container=False
211
  )
212
 
213
- # Right column for output
214
- with gr.Column(scale=1):
215
- output_image = gr.Image(
216
- type="pil",
217
- height=450,
218
- show_label=False,
219
- container=False
220
  )
221
 
222
- # Add download button to match the height of upload controls
223
- download_btn = gr.DownloadButton(
224
- label="πŸ“₯ Download Depth Map",
225
- variant="secondary",
226
- size="sm",
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=[upload_image, colormap_choice],
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=handle_prediction,
277
- inputs=[input_source, upload_image, camera_image, colormap_choice],
278
- outputs=[output_image, download_btn],
279
  show_progress=True
280
  )
281
 
282
  gr.Markdown("""
283
- ## πŸ“ Color Map Descriptions
284
  - **Spectral**: Rainbow spectrum with distinct near-far contrast
285
- - **Inferno**: Flame spectrum with warm tones
286
- - **Gray**: Classic grayscale depth representation
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