Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import torch
|
2 |
-
from torchvision import transforms
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
import numpy as np
|
6 |
import gradio as gr
|
7 |
from io import BytesIO
|
8 |
-
from torchvision.models.segmentation import deeplabv3_resnet101
|
9 |
import cv2
|
10 |
|
11 |
# Step 1: Load the Image from URL
|
@@ -29,57 +28,56 @@ def crop_image(image, bounding_box):
|
|
29 |
return image.crop((x_min, y_min, x_max, y_max))
|
30 |
|
31 |
# Step 4: Preprocessing for Segmentation Model
|
32 |
-
def preprocess_image(image
|
33 |
-
|
34 |
-
transforms.
|
35 |
-
transforms.ToTensor(),
|
36 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
37 |
])
|
38 |
-
return
|
39 |
|
40 |
-
# Step 5: Load
|
41 |
def load_model():
|
42 |
-
model =
|
43 |
model.eval() # Set the model to evaluation mode
|
44 |
if torch.cuda.is_available():
|
45 |
model = model.to("cuda")
|
46 |
return model
|
47 |
|
48 |
-
# Step 6: Perform Segmentation
|
49 |
-
def segment_image(model, input_tensor):
|
50 |
if torch.cuda.is_available():
|
51 |
input_tensor = input_tensor.to("cuda")
|
52 |
with torch.no_grad():
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
|
71 |
# Create RGBA image
|
72 |
image_np = np.array(image)
|
73 |
rgba_image = np.zeros((image_np.shape[0], image_np.shape[1], 4), dtype=np.uint8)
|
74 |
rgba_image[..., :3] = image_np # Copy RGB channels
|
75 |
-
rgba_image[..., 3] =
|
76 |
|
77 |
return Image.fromarray(rgba_image)
|
78 |
|
79 |
# Gradio Interface to handle input and output
|
80 |
def segment_object(image_url, x_min, y_min, x_max, y_max):
|
81 |
bounding_box = adjust_bounding_box({"x_min": x_min, "y_min": y_min, "x_max": x_max, "y_max": y_max})
|
82 |
-
|
83 |
# Load and process the image
|
84 |
image = load_image(image_url)
|
85 |
cropped_image = crop_image(image, bounding_box)
|
@@ -87,10 +85,10 @@ def segment_object(image_url, x_min, y_min, x_max, y_max):
|
|
87 |
|
88 |
# Load model and perform segmentation
|
89 |
model = load_model()
|
90 |
-
|
91 |
|
92 |
-
# Apply
|
93 |
-
result_image =
|
94 |
return result_image
|
95 |
|
96 |
# Set up the Gradio Interface
|
|
|
1 |
import torch
|
2 |
+
from torchvision import models, transforms
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
import numpy as np
|
6 |
import gradio as gr
|
7 |
from io import BytesIO
|
|
|
8 |
import cv2
|
9 |
|
10 |
# Step 1: Load the Image from URL
|
|
|
28 |
return image.crop((x_min, y_min, x_max, y_max))
|
29 |
|
30 |
# Step 4: Preprocessing for Segmentation Model
|
31 |
+
def preprocess_image(image):
|
32 |
+
transform = transforms.Compose([
|
33 |
+
transforms.ToTensor(), # Convert to Tensor
|
|
|
|
|
34 |
])
|
35 |
+
return transform(image).unsqueeze(0) # Add batch dimension
|
36 |
|
37 |
+
# Step 5: Load Mask R-CNN Model
|
38 |
def load_model():
|
39 |
+
model = models.detection.maskrcnn_resnet50_fpn(pretrained=True) # Pre-trained Mask R-CNN
|
40 |
model.eval() # Set the model to evaluation mode
|
41 |
if torch.cuda.is_available():
|
42 |
model = model.to("cuda")
|
43 |
return model
|
44 |
|
45 |
+
# Step 6: Perform Object Segmentation
|
46 |
+
def segment_image(model, input_tensor, confidence_threshold=0.6):
|
47 |
if torch.cuda.is_available():
|
48 |
input_tensor = input_tensor.to("cuda")
|
49 |
with torch.no_grad():
|
50 |
+
outputs = model(input_tensor) # Perform inference
|
51 |
+
|
52 |
+
# Process results: filter by confidence and get masks
|
53 |
+
scores = outputs[0]["scores"].cpu().numpy()
|
54 |
+
masks = outputs[0]["masks"].cpu().numpy()
|
55 |
+
boxes = outputs[0]["boxes"].cpu().numpy()
|
56 |
+
|
57 |
+
# Filter masks based on confidence threshold
|
58 |
+
filtered_masks = [masks[i, 0] for i in range(len(scores)) if scores[i] > confidence_threshold]
|
59 |
+
return filtered_masks
|
60 |
+
|
61 |
+
# Step 7: Combine Masks and Extract Object
|
62 |
+
def apply_masks(image, masks):
|
63 |
+
combined_mask = np.zeros((image.height, image.width), dtype=np.uint8)
|
64 |
+
|
65 |
+
for mask in masks:
|
66 |
+
resized_mask = cv2.resize(mask, (image.width, image.height), interpolation=cv2.INTER_NEAREST)
|
67 |
+
combined_mask = np.maximum(combined_mask, (resized_mask > 0.5).astype(np.uint8)) # Combine masks
|
68 |
|
69 |
# Create RGBA image
|
70 |
image_np = np.array(image)
|
71 |
rgba_image = np.zeros((image_np.shape[0], image_np.shape[1], 4), dtype=np.uint8)
|
72 |
rgba_image[..., :3] = image_np # Copy RGB channels
|
73 |
+
rgba_image[..., 3] = combined_mask * 255 # Alpha channel based on combined mask
|
74 |
|
75 |
return Image.fromarray(rgba_image)
|
76 |
|
77 |
# Gradio Interface to handle input and output
|
78 |
def segment_object(image_url, x_min, y_min, x_max, y_max):
|
79 |
bounding_box = adjust_bounding_box({"x_min": x_min, "y_min": y_min, "x_max": x_max, "y_max": y_max})
|
80 |
+
|
81 |
# Load and process the image
|
82 |
image = load_image(image_url)
|
83 |
cropped_image = crop_image(image, bounding_box)
|
|
|
85 |
|
86 |
# Load model and perform segmentation
|
87 |
model = load_model()
|
88 |
+
masks = segment_image(model, input_tensor)
|
89 |
|
90 |
+
# Apply masks to extract objects
|
91 |
+
result_image = apply_masks(cropped_image, masks)
|
92 |
return result_image
|
93 |
|
94 |
# Set up the Gradio Interface
|