sam2 安装使用笔记

目录

sam2不能全景分割,可以分割跟踪

sam2 windows 安装

安装

cannot import name ‘_C‘ from ‘sam2‘解决

box测试;

sam2图片分割


sam2不能全景分割,可以分割跟踪

sam2 windows 安装

SAM2 安装与运行问题解决方案_sam2部署-CSDN博客

安装

cannot import name ‘_C‘ from ‘sam2‘解决

http://t.csdnimg.cn/CEbKa

box测试;

ann_frame_idx = 0  # the frame index we interact with
ann_obj_id = 4  # give a unique id to each object we interact with (it can be any integers)

# Let's add a box at (x_min, y_min, x_max, y_max) = (300, 0, 500, 400) to get started
box = np.array([300, 0, 500, 400], dtype=np.float32)
_, out_obj_ids, out_mask_logits = predictor.add_new_points_or_box(
    inference_state=inference_state,
    frame_idx=ann_frame_idx,
    obj_id=ann_obj_id,
    box=box,
)

# show the results on the current (interacted) frame
plt.figure(figsize=(9, 6))
plt.title(f"frame {ann_frame_idx}")
plt.imshow(Image.open(os.path.join(video_dir, frame_names[ann_frame_idx])))
show_box(box, plt.gca())
show_mask((out_mask_logits[0] > 0.0).cpu().numpy(), plt.gca(), obj_id=out_obj_ids[0])

ann_frame_idx = 0  # the frame index we interact with
ann_obj_id = 4  # give a unique id to each object we interact with (it can be any integers)

# Let's add a positive click at (x, y) = (460, 60) to refine the mask
points = np.array([[460, 60]], dtype=np.float32)
# for labels, `1` means positive click and `0` means negative click
labels = np.array([1], np.int32)
# note that we also need to send the original box input along with
# the new refinement click together into `add_new_points_or_box`
box = np.array([300, 0, 500, 400], dtype=np.float32)
_, out_obj_ids, out_mask_logits = predictor.add_new_points_or_box(
    inference_state=inference_state,
    frame_idx=ann_frame_idx,
    obj_id=ann_obj_id,
    points=points,
    labels=labels,
    box=box,
)

# show the results on the current (interacted) frame
plt.figure(figsize=(9, 6))
plt.title(f"frame {ann_frame_idx}")
plt.imshow(Image.open(os.path.join(video_dir, frame_names[ann_frame_idx])))
show_box(box, plt.gca())
show_points(points, labels, plt.gca())
show_mask((out_mask_logits[0] > 0.0).cpu().numpy(), plt.gca(), obj_id=out_obj_ids[0])

sam2图片分割

https://zhuanlan.zhihu.com/p/714031640

import torch
import numpy as np
import cv2
from PIL import Image
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor

from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor

import time

import hydra

New_SAM = True

# use bfloat16 for the entire notebook
if New_SAM:
 torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()

# image = Image.open('/home/taohu/Projects/Data/RGB/thumbnail_Picture1.png')
# image = np.array(image.convert("RGB"))

image = cv2.imread('/home/taohu/Projects/Data/RGB/thumbnail_Picture1.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


if New_SAM:
 method = "SAM2"
else:
 method = "SAM1"

start_time1 = time.time()

if New_SAM:
 sam2_checkpoint = "models/sam2_hiera_large.pt"
 model_cfg = "sam2_hiera_l.yaml"

 sam2_model = build_sam2(model_cfg, sam2_checkpoint, device="cuda")

 predictor = SAM2ImagePredictor(sam2_model)
 predictor.set_image(image)
else:
 model_type = "vit_h"
 sam_checkpoint = "models/sam_vit_h_4b8939.pth" 
 sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
 sam.to("cuda")
 predictor = SamPredictor(sam)
 predictor.set_image(image)

end_time1 = time.time()
load_time = end_time1 - start_time1
print(f"Loading time ({method}): {load_time} seconds")

input_box = np.array([58,107, 213,281])
input_point = np.array([[104, 163]])
input_label = np.array([1])

start_time2 = time.time()

masks, scores, logits = predictor.predict(
 point_coords=input_point,
 point_labels=input_label,
 box=input_box,
 multimask_output=False,
)

end_time2 = time.time()
execution_time = end_time2 - start_time2
print(f"Execution time ({method}): {execution_time} seconds")

mask_array = np.array(masks[0]) 

if New_SAM:
 mask_array = mask_array.astype(np.uint8)*255 # SAM2 use 0~1 values for the mask
 mask_image = Image.fromarray(mask_array)
 mask_image.save("sam2-bw.jpg")
else:
 mask_image = Image.fromarray(mask_array)
 mask_image.save("sam1-bw.jpg")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值