Update app.py
Browse files
app.py
CHANGED
|
@@ -14,9 +14,60 @@ 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 |
-
|
|
|
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
print(f"Image size: {image.size}")
|
| 21 |
if depth_map_path:
|
| 22 |
print(f"Depth map path: {depth_map_path}")
|
|
|
|
| 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)
|
| 19 |
|
| 20 |
+
# Resize image to 1600 width while maintaining aspect ratio
|
| 21 |
+
new_width = 1600
|
| 22 |
+
new_height = int(new_width * image_raw.size[1] / image_raw.size[0])
|
| 23 |
+
image = image_raw.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 24 |
+
|
| 25 |
+
if depth_map_path:
|
| 26 |
+
# Determine if depth map is npy or image format
|
| 27 |
+
if depth_map_path.endswith(".npy"):
|
| 28 |
+
depth_image = np.load(depth_map_path)
|
| 29 |
+
else:
|
| 30 |
+
depth_image_raw = Image.open(depth_map_path)
|
| 31 |
+
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 32 |
+
depth_image = np.array(depth_image)
|
| 33 |
+
|
| 34 |
+
# Normalize depth if needed
|
| 35 |
+
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 36 |
+
depth_image = (depth_image * 255).astype('uint8')
|
| 37 |
+
else:
|
| 38 |
+
# Generate depth using DPT if not provided
|
| 39 |
+
encoding = feature_extractor(image, return_tensors="pt")
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = model(**encoding)
|
| 42 |
+
predicted_depth = outputs.predicted_depth
|
| 43 |
+
|
| 44 |
+
prediction = torch.nn.functional.interpolate(
|
| 45 |
+
predicted_depth.unsqueeze(1),
|
| 46 |
+
size=image.size[::-1],
|
| 47 |
+
mode="bicubic",
|
| 48 |
+
align_corners=False,
|
| 49 |
+
).squeeze()
|
| 50 |
+
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 51 |
+
|
| 52 |
+
# Create a mask of the main subject using rembg
|
| 53 |
+
main_subject_mask = create_main_subject_mask(image)
|
| 54 |
+
|
| 55 |
+
# Apply mask to depth map: keep depth for main subject only, set background depth to zero
|
| 56 |
+
subject_depth_map = np.where(main_subject_mask == 255, depth_image, 0)
|
| 57 |
+
|
| 58 |
+
# Create inpainted background with OpenCV
|
| 59 |
+
inpainted_bg = inpaint_background(image, np.array(image_raw.resize((new_width, new_height))))
|
| 60 |
+
|
| 61 |
+
# Scale inpainted background slightly to fill view
|
| 62 |
+
inpainted_bg = cv2.resize(inpainted_bg, None, fx=1.05, fy=1.05, interpolation=cv2.INTER_LINEAR)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
gltf_path = create_3d_obj(np.array(image), subject_depth_map, image_path, inpainted_bg, main_subject_offset=0.2)
|
| 66 |
+
img = Image.fromarray(subject_depth_map)
|
| 67 |
+
return [img, gltf_path, gltf_path]
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error reconstructing 3D model: {e}")
|
| 70 |
+
raise Exception("Error reconstructing 3D model")
|
| 71 |
print(f"Image size: {image.size}")
|
| 72 |
if depth_map_path:
|
| 73 |
print(f"Depth map path: {depth_map_path}")
|