这个仓库是segment-anything模型的打包版本。
安装
pip install metaseg
使用方法
from metaseg import SegAutoMaskPredictor, SegManualMaskPredictor
# 如果GPU内存不足,请减少points_per_side和points_per_batch的值。
# 图像处理
results = SegAutoMaskPredictor().image_predict(
source="image.jpg",
model_type="vit_l", # vit_l, vit_h, vit_b
points_per_side=16,
points_per_batch=64,
min_area=0,
output_path="output.jpg",
show=True,
save=False,
)
# 视频处理
results = SegAutoMaskPredictor().video_predict(
source="video.mp4",
model_type="vit_l", # vit_l, vit_h, vit_b
points_per_side=16,
points_per_batch=64,
min_area=1000,
output_path="output.mp4",
)
# 手动框选和点选
# 图像处理
results = SegManualMaskPredictor().image_predict(
source="image.jpg",
model_type="vit_l", # vit_l, vit_h, vit_b
input_point=[[100, 100], [200, 200]],
input_label=[0, 1],
input_box=[100, 100, 200, 200], # 或 [[100, 100, 200, 200], [100, 100, 200, 200]]
multimask_output=False,
random_color=False,
show=True,
save=False,
)
# 视频处理
results = SegManualMaskPredictor().video_predict(
source="video.mp4",
model_type="vit_l", # vit_l, vit_h, vit_b
input_point=[0, 0, 100, 100],
input_label=[0, 1],
input_box=None,
multimask_output=False,
random_color=False,
output_path="output.mp4",
)
SAHI + Segment Anything
pip install sahi metaseg
from metaseg.sahi_predict import SahiAutoSegmentation, sahi_sliced_predict
image_path = "image.jpg"
boxes = sahi_sliced_predict(
image_path=image_path,
detection_model_type="yolov5", # yolov8, detectron2, mmdetection, torchvision
detection_model_path="yolov5l6.pt",
conf_th=0.25,
image_size=1280,
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)
SahiAutoSegmentation().image_predict(
source=image_path,
model_type="vit_b",
input_box=boxes,
multimask_output=False,
random_color=False,
show=True,
save=False,
)
FalAI(云端GPU) + Segment Anything
pip install metaseg fal_serverless
fal-serverless auth login
# 自动蒙版
from metaseg import falai_automask_image
image = falai_automask_image(
image_path="image.jpg",
model_type="vit_b",
points_per_side=16,
points_per_batch=32,
min_area=0,
)
image.show() # 显示图像
image.save("output.jpg") # 保存图像
# 手动蒙版
from metaseg import falai_manuelmask_image
image = falai_manualmask_image(
image_path="image.jpg",
model_type="vit_b",
input_point=[[100, 100], [200, 200]],
input_label=[0, 1],
input_box=[100, 100, 200, 200], # 或 [[100, 100, 200, 200], [100, 100, 200, 200]],
multimask_output=False,
random_color=False,
)
image.show() # 显示图像
image.save("output.jpg") # 保存图像
附加功能
- 支持Yolov5/8、Detectron2、Mmdetection、Torchvision模型
- 支持视频和Web应用程序(Huggingface Spaces)
- 支持手动单框、多框和点选
- 支持pip安装
- 支持SAHI库
- 支持FalAI