ghost233lism commited on
Commit
aa8913d
Β·
verified Β·
1 Parent(s): 34923aa

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -17
app.py CHANGED
@@ -111,6 +111,10 @@ print("Model loaded successfully!")
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,31 +136,55 @@ def predict_depth(input_image, colormap_choice):
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(
@@ -165,38 +193,66 @@ with gr.Blocks(title="Depth Anything AC - Depth Estimation Demo", theme=gr.theme
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
 
 
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
  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()) as demo:
145
  gr.Markdown("""
146
  # 🌊 Depth Anything AC - Depth Estimation Demo
147
 
148
+ 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.
149
 
150
  ## How to Use
151
+ 1. **Upload Mode**: Click the upload area to select an image file
152
+ 2. **Camera Mode**: Use your camera to capture a live image
153
+ 3. Choose your preferred colormap style
154
+ 4. Click the "Generate Depth Map" button
155
+ 5. View the results and download
156
  """)
157
 
158
  with gr.Row():
159
+ with gr.Column(scale=1):
160
+ # Input source selection
161
+ input_source = gr.Radio(
162
+ choices=["Upload Image", "Use Camera"],
163
+ value="Upload Image",
164
+ label="Input Source"
165
+ )
166
+
167
+ # Upload image component
168
+ upload_image = gr.Image(
169
  label="Upload Image",
170
  type="pil",
171
+ height=450,
172
+ visible=True
173
+ )
174
+
175
+ # Camera component
176
+ camera_image = gr.Image(
177
+ label="Camera Input",
178
+ type="pil",
179
+ source="webcam",
180
+ height=450,
181
+ visible=False
182
  )
183
 
184
  colormap_choice = gr.Dropdown(
185
  choices=["Spectral", "Inferno", "Gray"],
186
  value="Spectral",
187
+ label="Colormap Style"
188
  )
189
 
190
  submit_btn = gr.Button(
 
193
  size="lg"
194
  )
195
 
196
+ with gr.Column(scale=1):
197
  output_image = gr.Image(
198
  label="Depth Map Result",
199
  type="pil",
200
+ height=450
201
  )
202
 
203
+ # Function to switch between upload and camera input
204
+ def switch_input_source(source):
205
+ if source == "Upload Image":
206
+ return gr.update(visible=True), gr.update(visible=False)
207
+ else:
208
+ return gr.update(visible=False), gr.update(visible=True)
209
+
210
+ # Update visibility based on input source selection
211
+ input_source.change(
212
+ fn=switch_input_source,
213
+ inputs=[input_source],
214
+ outputs=[upload_image, camera_image]
215
+ )
216
+
217
+ # Function to handle both input sources
218
+ def handle_prediction(input_source, upload_img, camera_img, colormap):
219
+ if input_source == "Upload Image":
220
+ return predict_depth(upload_img, colormap)
221
+ else:
222
+ return predict_depth(camera_img, colormap)
223
+
224
+ # Examples section
225
  gr.Examples(
226
  examples=[
227
  ["toyset/1.png", "Spectral"],
228
  ["toyset/2.png", "Spectral"],
229
  ["toyset/good.png", "Spectral"],
230
  ] if os.path.exists("toyset") else [],
231
+ inputs=[upload_image, colormap_choice],
232
  outputs=output_image,
233
  fn=predict_depth,
234
  cache_examples=False,
235
  label="Try these example images"
236
  )
237
 
238
+ # Submit button click handler
239
  submit_btn.click(
240
+ fn=handle_prediction,
241
+ inputs=[input_source, upload_image, camera_image, colormap_choice],
242
  outputs=output_image,
243
  show_progress=True
244
  )
245
 
246
  gr.Markdown("""
247
+ ## πŸ“ Color Map Descriptions
248
  - **Spectral**: Rainbow spectrum with distinct near-far contrast
249
+ - **Inferno**: Flame spectrum with warm tones
250
+ - **Gray**: Classic grayscale depth representation
251
+
252
+ ## πŸ“· Camera Tips
253
+ - Make sure to allow camera access when prompted
254
+ - Click the camera button to capture the current frame
255
+ - The captured image will be used as input for depth estimation
256
  """)
257
 
258