Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add manuel_mask feature #20

Merged
merged 2 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ autoseg_video = SegAutoMaskGenerator().save_video(
- [x] Support for video files
- [x] Support for pip installation
- [x] Support for web application
- [x] Support for box to polygon conversion
- [x] Support for automatic download model weights

4 changes: 2 additions & 2 deletions metaseg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from metaseg.auto_mask_demo import SegAutoMaskGenerator
from metaseg.automatic_mask_generator import SamAutomaticMaskGenerator
from metaseg.build_sam import build_sam, build_sam_vit_b, build_sam_vit_h, build_sam_vit_l, sam_model_registry
from metaseg.demo import SegAutoMaskGenerator
from metaseg.predictor import SamPredictor

__version__ = "0.3.0"
__version__ = "0.3.2"
6 changes: 4 additions & 2 deletions metaseg/demo.py → metaseg/auto_mask_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from metaseg import SamAutomaticMaskGenerator, sam_model_registry
from metaseg.utils import download_model, load_image, load_video


class SegAutoMaskGenerator:
def __init__(self):
self.model = None
Expand All @@ -21,8 +22,9 @@ def load_model(self, model_type):
def predict(self, frame, model_type, points_per_side, points_per_batch, min_area):
model = self.load_model(model_type)
mask_generator = SamAutomaticMaskGenerator(
model, points_per_side=points_per_side, points_per_batch=points_per_batch, min_mask_region_area=min_area)

model, points_per_side=points_per_side, points_per_batch=points_per_batch, min_mask_region_area=min_area
)

masks = mask_generator.generate(frame)

return frame, masks
Expand Down
63 changes: 63 additions & 0 deletions metaseg/manuel_mask_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import cv2
import numpy as np
import torch

from metaseg import SamPredictor, sam_model_registry
from metaseg.utils import download_model, load_image


class SegManualMaskGenerator:
def __init__(self):
self.model = None
self.device = "cuda" if torch.cuda.is_available() else "cpu"

def load_model(self, model_type):
if self.model is None:
self.model_path = download_model(model_type)
self.model = sam_model_registry[model_type](checkpoint=self.model_path)
self.model.to(device=self.device)

return self.model

def load_mask(mask, random_color=True):
if random_color:
color = np.random.rand(3) * 255
else:
color = np.array([255, 200, 0])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
mask_image = mask_image.astype(np.uint8)
return mask_image

def load_box(box, image):
x0, y0 = box[0], box[1]
x1, y1 = box[2], box[3]
cv2.rectangle(image, (x0, y0), (x1, y1), (0, 255, 0), 2)
return image

def predict(self, frame, model_type, x0, y0, x1, y1):
model = self.load_model(model_type)
predictor = SamPredictor(model)
predictor.set_image(frame)
input_box = np.array([x0, y0, x1, y1])
masks, _, _ = predictor.predict(
point_coords=None,
point_labels=None,
box=input_box[None, :],
multimask_output=False,
)

return frame, masks, input_box

def save_image(self, source, model_type, x0, y0, x1, y1):
read_image = load_image(source)
image, anns, input_box = self.predict(read_image, model_type, x0, y0, x1, y1)
if len(anns) == 0:
return

mask_image = self.load_mask(anns, random_color=True)
image = self.load_box(input_box, image)
combined_mask = cv2.add(image, mask_image)
cv2.imwrite("output.jpg", combined_mask)

return "output.jpg"
2 changes: 1 addition & 1 deletion metaseg/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from metaseg.utils.file_utils import download_model
from metaseg.utils.data_utils import load_image, load_video
from metaseg.utils.file_utils import download_model
2 changes: 2 additions & 0 deletions metaseg/utils/data_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import cv2


def load_image(image_path):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image


def load_video(video_path):
cap = cv2.VideoCapture(video_path)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
Expand Down
Empty file added metaseg/webapp/__init__.py
Empty file.
6 changes: 2 additions & 4 deletions metaseg/app.py → metaseg/webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def image_app():
value=64,
label="Points per Batch",
)

seg_automask_image_min_area = gr.Number(
value=0,
label="Min Area",
)
)

seg_automask_image_predict = gr.Button(value="Generator")

Expand Down Expand Up @@ -99,8 +99,6 @@ def video_app():
label="Min Area",
)



seg_automask_video_predict = gr.Button(value="Generator")
with gr.Column():
output_video = gr.Video()
Expand Down