codic commited on
Commit
8856613
·
verified ·
1 Parent(s): aa18bbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -30
app.py CHANGED
@@ -16,45 +16,50 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
16
  def process_image(image_path, depth_map_path=None):
17
  image_path = Path(image_path)
18
  image_raw = Image.open(image_path).convert("RGB")
19
- # Resizing image to ensure background duplicates match the full size
20
  image = image_raw.resize(
21
- (2048, int(2048 * image_raw.size[1] / image_raw.size[0])),
22
  Image.Resampling.LANCZOS,
23
  )
24
 
25
- # Remove background using rembg
26
- foreground = remove(image_raw)
27
- foreground = Image.fromarray(np.array(foreground)).convert("RGBA") # Convert to RGBA for transparency support
28
-
29
- # Create a duplicate background and apply small depth adjustments
30
- bg_copy = image.convert("RGBA")
31
- bg_copy_no_depth = image.convert("RGBA") # Background without depth for flat appearance
32
-
33
- # Composite the foreground over the background copy
34
- bg_with_depth = Image.alpha_composite(bg_copy, foreground) # First background with slight depth
35
-
36
- # Normalize depth and convert to high quality texture
37
  if depth_map_path:
38
- depth_image = np.load(depth_map_path) if depth_map_path.endswith('.npy') else np.array(Image.open(depth_map_path).convert("L"))
 
 
 
 
 
 
 
39
  else:
40
- # Generate depth map with DPT model if none provided
41
- encoding = feature_extractor(foreground.convert("RGB"), return_tensors="pt")
42
  with torch.no_grad():
43
  outputs = model(**encoding)
44
  predicted_depth = outputs.predicted_depth
45
- depth_image = (predicted_depth.cpu().numpy() * 255 / np.max(predicted_depth.cpu().numpy())).astype("uint8")
46
-
47
- # Resize and blend depth image for final compositing
48
- depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
49
- depth_blur = cv2.GaussianBlur(depth_image, (3, 3), 0.8) # Lighter blur for texture detail
50
-
51
- # Composite with background without depth effects
52
- bg_final = Image.alpha_composite(bg_copy_no_depth, Image.fromarray(depth_blur).convert("RGBA"))
53
-
54
- # Return the final blended depth and 3D model path
55
- blended_image = Image.alpha_composite(bg_final, foreground)
56
- img = Image.fromarray(depth_blur)
57
- return [img, blended_image]
 
 
 
 
 
 
 
 
 
 
58
 
59
  def create_3d_obj(rgb_image, depth_image, image_path, depth=8):
60
  # Resize and normalize the depth image
 
16
  def process_image(image_path, depth_map_path=None):
17
  image_path = Path(image_path)
18
  image_raw = Image.open(image_path).convert("RGB")
 
19
  image = image_raw.resize(
20
+ (1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
21
  Image.Resampling.LANCZOS,
22
  )
23
 
24
+ # Generate or load depth map
 
 
 
 
 
 
 
 
 
 
 
25
  if depth_map_path:
26
+ # Load and resize depth map to match the RGB image size
27
+ depth_image_raw = Image.open(depth_map_path)
28
+ depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
29
+ depth_image = np.array(depth_image)
30
+
31
+ # Normalize depth map if needed
32
+ depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
33
+ depth_image = (depth_image * 255).astype('uint8')
34
  else:
35
+ # Generate depth map using DPT model if not provided
36
+ encoding = feature_extractor(image, return_tensors="pt")
37
  with torch.no_grad():
38
  outputs = model(**encoding)
39
  predicted_depth = outputs.predicted_depth
40
+ prediction = torch.nn.functional.interpolate(
41
+ predicted_depth.unsqueeze(1),
42
+ size=image.size[::-1],
43
+ mode="bicubic",
44
+ align_corners=False,
45
+ ).squeeze()
46
+ depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
47
+
48
+ # Make sure depth image matches dimensions and mode of RGB image for compositing
49
+ depth_rgba = Image.fromarray(depth_image).convert("RGBA").resize(image.size)
50
+ image_rgba = image.convert("RGBA")
51
+
52
+ # Composite depth onto the RGB image background without purple tint
53
+ bg_with_depth = Image.alpha_composite(image_rgba, depth_rgba)
54
+
55
+ try:
56
+ # Create 3D object from depth and RGB image
57
+ gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
58
+ return [Image.fromarray(depth_image), gltf_path, gltf_path]
59
+ except Exception as e:
60
+ # Optionally, handle specific reconstruction issues
61
+ print("Error reconstructing 3D model:", e)
62
+ raise Exception("Error reconstructing 3D model")
63
 
64
  def create_3d_obj(rgb_image, depth_image, image_path, depth=8):
65
  # Resize and normalize the depth image