codic commited on
Commit
eaf24aa
·
verified ·
1 Parent(s): 2d8a744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -56
app.py CHANGED
@@ -14,69 +14,68 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
14
 
15
  # Background removal API client
16
  client = Client("not-lain/background-removal")
17
-
18
  def process_image(image_path, depth_map_path=None):
19
- image_path = Path(image_path)
20
- image_raw = Image.open(image_path)
21
- image = image_raw.resize(
22
- (800, int(800 * image_raw.size[1] / image_raw.size[0])),
23
- Image.Resampling.LANCZOS,
24
- )
 
25
 
26
- # Step 1: Perform background removal
27
- result = client.predict(
28
- f=str(image_path), # Convert Path to string
29
- api_name="/png"
30
- )
31
 
32
- # Load the foreground image from the returned file path
33
- foreground_image_path = result # This contains the path to the processed image
34
- foreground_image = Image.open(foreground_image_path) # Open the image
35
-
36
- if depth_map_path:
37
- # Load and resize the user-provided depth map to match the RGB image size
38
- depth_image_raw = Image.open(depth_map_path)
39
- depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
40
- depth_image = np.array(depth_image)
41
-
42
- # Normalize depth map if needed
43
- depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
44
- depth_image = (depth_image * 255).astype('uint8')
45
-
46
- else:
47
- # Automatically generate the depth map using the DPT model
48
- encoding = feature_extractor(image, return_tensors="pt")
49
- with torch.no_grad():
50
- outputs = model(**encoding)
51
- predicted_depth = outputs.predicted_depth
52
-
53
- # Interpolate to match the image size
54
- prediction = torch.nn.functional.interpolate(
55
- predicted_depth.unsqueeze(1),
56
- size=image.size[::-1],
57
- mode="bicubic",
58
- align_corners=False,
59
- ).squeeze()
60
- depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
61
-
62
- # Step 2: Create flat background depth layer and combine with foreground
63
- background_depth_value = 20 # Slight depth for background
64
- background_depth = np.full_like(depth_image, background_depth_value) # Flat depth for background
65
-
66
- # Ensure foreground_mask is boolean
67
- foreground_mask_resized = np.array(foreground_image.convert("L").resize(image.size, Image.Resampling.NEAREST)) > 0
68
-
69
- # Combine the flat background with the foreground depth
70
- combined_depth_image = np.where(foreground_mask_resized[..., None], depth_image[..., None], background_depth[..., None])
71
-
72
- # Step 3: Generate 3D model using the combined depth map
73
- try:
74
  gltf_path = create_3d_obj(np.array(image), combined_depth_image, image_path)
75
  img = Image.fromarray(combined_depth_image)
76
  return [img, gltf_path, gltf_path]
 
77
  except Exception as e:
78
- print(f"Error creating 3D object: {e}")
79
- raise Exception("Error reconstructing 3D model")
80
 
81
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
82
  depth_o3d = o3d.geometry.Image(depth_image)
 
14
 
15
  # Background removal API client
16
  client = Client("not-lain/background-removal")
 
17
  def process_image(image_path, depth_map_path=None):
18
+ try:
19
+ image_path = Path(image_path)
20
+ image_raw = Image.open(image_path)
21
+ image = image_raw.resize(
22
+ (800, int(800 * image_raw.size[1] / image_raw.size[0])),
23
+ Image.Resampling.LANCZOS,
24
+ )
25
 
26
+ # Step 1: Perform background removal
27
+ result = client.predict(
28
+ f=str(image_path), # Convert Path to string
29
+ api_name="/png"
30
+ )
31
 
32
+ # Load the foreground image from the returned file path
33
+ foreground_image_path = result # This contains the path to the processed image
34
+ foreground_image = Image.open(foreground_image_path) # Open the image
35
+
36
+ if depth_map_path:
37
+ # Load and resize the user-provided depth map to match the RGB image size
38
+ depth_image_raw = Image.open(depth_map_path)
39
+ depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
40
+ depth_image = np.array(depth_image)
41
+
42
+ # Normalize depth map if needed
43
+ depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
44
+ depth_image = (depth_image * 255).astype('uint8')
45
+ else:
46
+ # Automatically generate the depth map using the DPT model
47
+ encoding = feature_extractor(image, return_tensors="pt")
48
+ with torch.no_grad():
49
+ outputs = model(**encoding)
50
+ predicted_depth = outputs.predicted_depth
51
+
52
+ # Interpolate to match the image size
53
+ prediction = torch.nn.functional.interpolate(
54
+ predicted_depth.unsqueeze(1),
55
+ size=image.size[::-1],
56
+ mode="bicubic",
57
+ align_corners=False,
58
+ ).squeeze()
59
+ depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
60
+
61
+ # Step 2: Create flat background depth layer and combine with foreground
62
+ background_depth_value = 20 # Slight depth for background
63
+ background_depth = np.full_like(depth_image, background_depth_value) # Flat depth for background
64
+
65
+ # Ensure foreground_mask is boolean
66
+ foreground_mask_resized = np.array(foreground_image.convert("L").resize(image.size, Image.Resampling.NEAREST)) > 0
67
+
68
+ # Combine the flat background with the foreground depth
69
+ combined_depth_image = np.where(foreground_mask_resized[..., None], depth_image[..., None], background_depth[..., None])
70
+
71
+ # Step 3: Generate 3D model using the combined depth map
 
 
72
  gltf_path = create_3d_obj(np.array(image), combined_depth_image, image_path)
73
  img = Image.fromarray(combined_depth_image)
74
  return [img, gltf_path, gltf_path]
75
+
76
  except Exception as e:
77
+ print(f"Error in process_image: {e}")
78
+ raise Exception("Error reconstructing 3D model") from e
79
 
80
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
81
  depth_o3d = o3d.geometry.Image(depth_image)