NightRaven109 commited on
Commit
fd7e9e3
·
verified ·
1 Parent(s): 4483b37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -17
app.py CHANGED
@@ -1,8 +1,9 @@
1
- import spaces # Import spaces first
2
  import cv2
3
  import torch
4
  import numpy as np
5
  import gradio as gr
 
6
  from depth_anything_v2.dpt import DepthAnythingV2
7
 
8
  # Model initialization
@@ -175,15 +176,22 @@ def process_normal_map(image):
175
  result = (converted_normal.squeeze(0).numpy() * 255).astype(np.uint8)
176
  return result
177
 
178
- # Initialize model globally
179
  def initialize_model():
180
  encoder = 'vitl'
181
  max_depth = 1
182
 
183
  model = DepthAnythingV2(**{**model_configs[encoder], 'max_depth': max_depth})
184
 
 
 
 
 
 
 
 
185
  # Load checkpoint
186
- checkpoint = torch.load('checkpoints/model2.pth', map_location='cpu')
187
 
188
  # Get state dict
189
  state_dict = {}
@@ -200,15 +208,15 @@ def initialize_model():
200
  model.load_state_dict(my_state_dict)
201
  return model
202
 
 
203
  MODEL = initialize_model()
204
 
205
- @spaces.GPU
206
  def process_image(input_image):
207
  """
208
- Process the input image and return depth maps, intensity map, blended result, and normal map
209
  """
210
  if input_image is None:
211
- return None, None, None, None, None
212
 
213
  # Move model to GPU for processing
214
  MODEL.to('cuda')
@@ -223,10 +231,6 @@ def process_image(input_image):
223
 
224
  # Normalize depth for visualization (0-255)
225
  depth_normalized = ((depth - depth.min()) / (depth.max() - depth.min()) * 255).astype(np.uint8)
226
-
227
- # Apply colormap for better visualization
228
- depth_colormap = cv2.applyColorMap(depth_normalized, cv2.COLORMAP_INFERNO)
229
- depth_colormap = cv2.cvtColor(depth_colormap, cv2.COLOR_BGR2RGB)
230
 
231
  # Move model back to CPU after processing
232
  MODEL.to('cpu')
@@ -245,16 +249,15 @@ def process_image(input_image):
245
  # Generate normal map from blended result
246
  normal_map = process_normal_map(blended_result)
247
 
248
- return depth_normalized, depth_colormap, intensity_map, blended_result, normal_map
249
 
250
- @spaces.GPU
251
  def gradio_interface(input_img):
252
  try:
253
- depth_raw, depth_colored, intensity, blended, normal = process_image(input_img)
254
- return [input_img, depth_raw, depth_colored, intensity, blended, normal]
255
  except Exception as e:
256
  print(f"Error processing image: {str(e)}")
257
- return [input_img, None, None, None, None, None]
258
 
259
  # Define interface
260
  iface = gr.Interface(
@@ -264,8 +267,8 @@ iface = gr.Interface(
264
  gr.Image(label="Raw Depth Map"),
265
  gr.Image(label="Normal Map")
266
  ],
267
- title="Depth, Intensity, and Normal Map Estimation",
268
- description="Upload an image to generate its depth map, intensity map, blended result, and normal map.",
269
  examples=["image.jpg"]
270
  )
271
 
 
1
+ import os
2
  import cv2
3
  import torch
4
  import numpy as np
5
  import gradio as gr
6
+ from huggingface_hub import hf_hub_download
7
  from depth_anything_v2.dpt import DepthAnythingV2
8
 
9
  # Model initialization
 
176
  result = (converted_normal.squeeze(0).numpy() * 255).astype(np.uint8)
177
  return result
178
 
179
+ # Download and initialize model
180
  def initialize_model():
181
  encoder = 'vitl'
182
  max_depth = 1
183
 
184
  model = DepthAnythingV2(**{**model_configs[encoder], 'max_depth': max_depth})
185
 
186
+ # Download model from private repo
187
+ model_path = hf_hub_download(
188
+ "NightRaven109/DepthAnythingv2custom",
189
+ "model2.pth",
190
+ use_auth_token=os.environ['Read']
191
+ )
192
+
193
  # Load checkpoint
194
+ checkpoint = torch.load(model_path, map_location='cpu')
195
 
196
  # Get state dict
197
  state_dict = {}
 
208
  model.load_state_dict(my_state_dict)
209
  return model
210
 
211
+ # Initialize model at startup
212
  MODEL = initialize_model()
213
 
 
214
  def process_image(input_image):
215
  """
216
+ Process the input image and return depth map and normal map
217
  """
218
  if input_image is None:
219
+ return None, None
220
 
221
  # Move model to GPU for processing
222
  MODEL.to('cuda')
 
231
 
232
  # Normalize depth for visualization (0-255)
233
  depth_normalized = ((depth - depth.min()) / (depth.max() - depth.min()) * 255).astype(np.uint8)
 
 
 
 
234
 
235
  # Move model back to CPU after processing
236
  MODEL.to('cpu')
 
249
  # Generate normal map from blended result
250
  normal_map = process_normal_map(blended_result)
251
 
252
+ return depth_normalized, normal_map
253
 
 
254
  def gradio_interface(input_img):
255
  try:
256
+ depth_raw, normal = process_image(input_img)
257
+ return [depth_raw, normal]
258
  except Exception as e:
259
  print(f"Error processing image: {str(e)}")
260
+ return [None, None]
261
 
262
  # Define interface
263
  iface = gr.Interface(
 
267
  gr.Image(label="Raw Depth Map"),
268
  gr.Image(label="Normal Map")
269
  ],
270
+ title="Depth and Normal Map Generation",
271
+ description="Upload an image to generate its depth map and normal map.",
272
  examples=["image.jpg"]
273
  )
274