当前位置: 首页 > news >正文

sam2 安装使用笔记

目录

sam2 windows 安装

安装

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

box测试;

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 SAM2ImagePredictorfrom segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictorimport timeimport hydraNew_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 maskmask_image = Image.fromarray(mask_array)mask_image.save("sam2-bw.jpg")
else:mask_image = Image.fromarray(mask_array)mask_image.save("sam1-bw.jpg")

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 高耐用性工业MRAM存储芯片解决方案
  • 【论文阅读】为大规模航空图像应用神经辐射场
  • 什么是数据库 DevOps?
  • C语言:strtok的注意事项及模拟实现
  • 集成电路学习:什么是Bootloader启动加载程序
  • 数据结构:树
  • selenium使用指南
  • 在centos系统中kill掉指定进程
  • Vue3 ref 和 reactive 的区别
  • Linux操作文件和文件夹的常用基础命令
  • RTC相关
  • vmware解决虚拟机空间占用不断增大问题
  • Eclipse 自定义字体大小
  • Android 模拟器的简单操作
  • 【算法】演员~评论家方法
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 10个确保微服务与容器安全的最佳实践
  • android 一些 utils
  • css选择器
  • es的写入过程
  • JAVA之继承和多态
  • js对象的深浅拷贝
  • leetcode讲解--894. All Possible Full Binary Trees
  • Linux快速复制或删除大量小文件
  • mysql外键的使用
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • PV统计优化设计
  • Redis 懒删除(lazy free)简史
  • SpiderData 2019年2月16日 DApp数据排行榜
  • Tornado学习笔记(1)
  • 半理解系列--Promise的进化史
  • 测试如何在敏捷团队中工作?
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 全栈开发——Linux
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 通过几道题目学习二叉搜索树
  • 学习ES6 变量的解构赋值
  • 走向全栈之MongoDB的使用
  • 7行Python代码的人脸识别
  • 带你开发类似Pokemon Go的AR游戏
  • ​2020 年大前端技术趋势解读
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • #define 用法
  • #ifdef 的技巧用法
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (3)nginx 配置(nginx.conf)
  • (笔记)M1使用hombrew安装qemu
  • (算法)Game
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • ***通过什么方式***网吧
  • .gitignore文件_Git:.gitignore