codic commited on
Commit
1728fe1
·
verified ·
1 Parent(s): 08794bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -14,10 +14,11 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
14
  def process_image(image_path, depth_map_path=None):
15
  image_path = Path(image_path)
16
  image_raw = Image.open(image_path)
17
- image = image_raw.resize(
18
- (1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
19
- Image.Resampling.LANCZOS,
20
- )
 
21
 
22
  if depth_map_path:
23
  # Determine if depth map is npy or image format
@@ -47,10 +48,10 @@ def process_image(image_path, depth_map_path=None):
47
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
48
 
49
  # Create inpainted background with OpenCV
50
- inpainted_bg = inpaint_background(image, np.array(image_raw))
51
 
52
- # Resize depth_image to match inpainted_bg size
53
- depth_image = Image.fromarray(depth_image).resize(inpainted_bg.shape[1::-1], Image.Resampling.NEAREST)
54
  depth_image = np.array(depth_image)
55
 
56
  try:
@@ -58,11 +59,7 @@ def process_image(image_path, depth_map_path=None):
58
  img = Image.fromarray(depth_image)
59
  return [img, gltf_path, gltf_path]
60
  except Exception as e:
61
- gltf_path = create_3d_obj(np.array(image), depth_image, image_path, inpainted_bg, depth=8)
62
- img = Image.fromarray(depth_image)
63
- return [img, gltf_path, gltf_path]
64
- except:
65
- print("Error reconstructing 3D model")
66
  raise Exception("Error reconstructing 3D model")
67
 
68
  def inpaint_background(image, original_image):
 
14
  def process_image(image_path, depth_map_path=None):
15
  image_path = Path(image_path)
16
  image_raw = Image.open(image_path)
17
+
18
+ # Resize image to 1600 width while maintaining aspect ratio
19
+ new_width = 1600
20
+ new_height = int(new_width * image_raw.size[1] / image_raw.size[0])
21
+ image = image_raw.resize((new_width, new_height), Image.Resampling.LANCZOS)
22
 
23
  if depth_map_path:
24
  # Determine if depth map is npy or image format
 
48
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
49
 
50
  # Create inpainted background with OpenCV
51
+ inpainted_bg = inpaint_background(image, np.array(image_raw.resize((new_width, new_height))))
52
 
53
+ # Ensure depth_image matches final image dimensions
54
+ depth_image = Image.fromarray(depth_image).resize((new_width, new_height), Image.Resampling.NEAREST)
55
  depth_image = np.array(depth_image)
56
 
57
  try:
 
59
  img = Image.fromarray(depth_image)
60
  return [img, gltf_path, gltf_path]
61
  except Exception as e:
62
+ print(f"Error reconstructing 3D model: {e}")
 
 
 
 
63
  raise Exception("Error reconstructing 3D model")
64
 
65
  def inpaint_background(image, original_image):