Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,43 +11,32 @@ depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-hybrid-mida
|
|
| 11 |
# Function to process the image and return depth map
|
| 12 |
def launch(input_image):
|
| 13 |
try:
|
| 14 |
-
# Resize the image to a fixed size (e.g., 640x480)
|
| 15 |
-
input_image = input_image.resize((640, 480))
|
| 16 |
-
|
| 17 |
# Ensure the input image is in RGB mode
|
| 18 |
if input_image.mode != "RGB":
|
| 19 |
-
print(f"Converting image from {input_image.mode} to RGB.")
|
| 20 |
input_image = input_image.convert("RGB")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
print(f"Received image with size: {input_image.size}")
|
| 24 |
-
|
| 25 |
-
# Run depth estimation
|
| 26 |
out = depth_estimator(input_image)
|
| 27 |
-
print(f"Model output: {out}")
|
| 28 |
-
|
| 29 |
-
# Check if the model output contains 'predicted_depth'
|
| 30 |
-
if "predicted_depth" in out:
|
| 31 |
-
predicted_depth = out["predicted_depth"].view(1, 1, 480, 640) # Assuming single image
|
| 32 |
-
else:
|
| 33 |
-
raise ValueError("Model output does not contain 'predicted_depth'.")
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
mode="bicubic",
|
| 40 |
-
align_corners=False
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Normalize the prediction
|
| 44 |
-
output =
|
| 45 |
formatted = (output * 255 / np.max(output)).astype("uint8")
|
| 46 |
-
|
| 47 |
-
# Convert the depth map to an image
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Error processing the image: {str(e)}")
|
|
|
|
| 11 |
# Function to process the image and return depth map
|
| 12 |
def launch(input_image):
|
| 13 |
try:
|
|
|
|
|
|
|
|
|
|
| 14 |
# Ensure the input image is in RGB mode
|
| 15 |
if input_image.mode != "RGB":
|
|
|
|
| 16 |
input_image = input_image.convert("RGB")
|
| 17 |
|
| 18 |
+
# Run the image segmentation model
|
|
|
|
|
|
|
|
|
|
| 19 |
out = depth_estimator(input_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Assuming output contains the segmentation mask or predicted depth map
|
| 22 |
+
predicted_output = out["predicted_depth"] if "predicted_depth" in out else out["segmentation_mask"]
|
| 23 |
+
|
| 24 |
+
# Resize the output to match the input image size
|
| 25 |
+
predicted_output_resized = torch.nn.functional.interpolate(
|
| 26 |
+
predicted_output.unsqueeze(0), # Add batch dimension
|
| 27 |
+
size=input_image.size[::-1], # Match input image size (H, W)
|
| 28 |
mode="bicubic",
|
| 29 |
+
align_corners=False
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Normalize the prediction for display
|
| 33 |
+
output = predicted_output_resized.squeeze().cpu().numpy()
|
| 34 |
formatted = (output * 255 / np.max(output)).astype("uint8")
|
| 35 |
+
|
| 36 |
+
# Convert the depth map or segmentation mask to an image
|
| 37 |
+
output_image = Image.fromarray(formatted)
|
| 38 |
+
|
| 39 |
+
return output_image
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
print(f"Error processing the image: {str(e)}")
|