codic commited on
Commit
452deee
·
verified ·
1 Parent(s): 2f1f3e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -13,13 +13,6 @@ from rembg import remove # For background removal
13
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
14
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
15
 
16
- from pathlib import Path
17
- import numpy as np
18
- import cv2
19
- from PIL import Image
20
- import torch
21
- from rembg import remove # Assuming you're using rembg for background removal
22
-
23
  def process_image(image_path, depth_map_path=None):
24
  image_path = Path(image_path)
25
  image_raw = Image.open(image_path)
@@ -32,13 +25,12 @@ def process_image(image_path, depth_map_path=None):
32
  image_np = np.array(image)
33
  subject = remove(image_np) # Removes background
34
 
35
- # Ensure subject is in RGB format if not already
36
  if len(subject.shape) == 2 or subject.shape[2] != 3:
37
  if subject.shape[2] == 4: # If the image has 4 channels (RGBA)
38
- subject = cv2.cvtColor(subject, cv2.COLOR_RGBA2GRAY)
39
  else:
40
- subject = cv2.cvtColor(subject, cv2.COLOR_BGR2GRAY)
41
- subject = cv2.cvtColor(subject, cv2.COLOR_GRAY2RGB) # Convert to 3-channel image
42
 
43
  # Step 2: Inpaint the background
44
  mask = cv2.inRange(subject, (0, 0, 0), (0, 0, 0)) # Binary mask
@@ -50,9 +42,11 @@ def process_image(image_path, depth_map_path=None):
50
 
51
  # Step 3: Generate or use existing depth map
52
  if depth_map_path:
 
53
  if depth_map_path.endswith('.npy'):
54
- # Load the depth map from a .npy file and invert it
55
  depth_image = np.load(depth_map_path)
 
56
  depth_image = 255 - (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image)) * 255
57
  depth_image = depth_image.astype('uint8')
58
  else:
@@ -60,6 +54,7 @@ def process_image(image_path, depth_map_path=None):
60
  depth_image_raw = Image.open(depth_map_path)
61
  depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
62
  depth_image = np.array(depth_image)
 
63
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
64
  depth_image = (depth_image * 255).astype('uint8')
65
  else:
@@ -86,6 +81,7 @@ def process_image(image_path, depth_map_path=None):
86
  raise Exception("Error reconstructing 3D model")
87
 
88
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
 
89
  depth_o3d = o3d.geometry.Image(depth_image)
90
  image_o3d = o3d.geometry.Image(rgb_image)
91
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
@@ -129,7 +125,7 @@ iface = gr.Interface(
129
  fn=process_image,
130
  inputs=[
131
  gr.Image(type="filepath", label="Input Image"),
132
- gr.Image(type="filepath", label="Input Depth Map (optional)"),
133
  ],
134
  outputs=[
135
  gr.Image(label="Predicted Depth", type="pil"),
@@ -142,4 +138,5 @@ iface = gr.Interface(
142
  allow_flagging="never",
143
  cache_examples=False,
144
  )
 
145
  iface.launch(debug=True, show_api=True, share=True)
 
13
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
14
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
15
 
 
 
 
 
 
 
 
16
  def process_image(image_path, depth_map_path=None):
17
  image_path = Path(image_path)
18
  image_raw = Image.open(image_path)
 
25
  image_np = np.array(image)
26
  subject = remove(image_np) # Removes background
27
 
28
+ # Ensure subject is in RGB format
29
  if len(subject.shape) == 2 or subject.shape[2] != 3:
30
  if subject.shape[2] == 4: # If the image has 4 channels (RGBA)
31
+ subject = cv2.cvtColor(subject, cv2.COLOR_RGBA2RGB)
32
  else:
33
+ subject = cv2.cvtColor(subject, cv2.COLOR_GRAY2RGB) # Ensure it's RGB if grayscale
 
34
 
35
  # Step 2: Inpaint the background
36
  mask = cv2.inRange(subject, (0, 0, 0), (0, 0, 0)) # Binary mask
 
42
 
43
  # Step 3: Generate or use existing depth map
44
  if depth_map_path:
45
+ # Check if the depth map path is a .npy file or an image
46
  if depth_map_path.endswith('.npy'):
47
+ # Load the depth map from a .npy file
48
  depth_image = np.load(depth_map_path)
49
+ # Normalize and invert depth map
50
  depth_image = 255 - (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image)) * 255
51
  depth_image = depth_image.astype('uint8')
52
  else:
 
54
  depth_image_raw = Image.open(depth_map_path)
55
  depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
56
  depth_image = np.array(depth_image)
57
+ # Normalize depth image
58
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
59
  depth_image = (depth_image * 255).astype('uint8')
60
  else:
 
81
  raise Exception("Error reconstructing 3D model")
82
 
83
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
84
+ # Your existing create_3d_obj implementation...
85
  depth_o3d = o3d.geometry.Image(depth_image)
86
  image_o3d = o3d.geometry.Image(rgb_image)
87
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
 
125
  fn=process_image,
126
  inputs=[
127
  gr.Image(type="filepath", label="Input Image"),
128
+ gr.Image(type="filepath", label="Input Depth Map (optional)"), # Keep this as gr.Image
129
  ],
130
  outputs=[
131
  gr.Image(label="Predicted Depth", type="pil"),
 
138
  allow_flagging="never",
139
  cache_examples=False,
140
  )
141
+
142
  iface.launch(debug=True, show_api=True, share=True)