File size: 742 Bytes
f602b02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from segment_anything import sam_model_registry
import torch
import cv2
import numpy as np
from utils import select_random_points, save_masks
# โหลดโมเดล SAM-Med2D
sam = sam_model_registry("vit_b", "pretrain_model/sam-med2d_b.pth")
sam.eval()
# โหลดภาพที่ต้องการแบ่งส่วน
image = cv2.imread("path_to_image.png")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# เลือกจุดที่ต้องการแบ่งส่วน
point = select_random_points(image_rgb)
# ประมวลผลและแบ่งส่วนภาพ
masks, scores = sam.predict(image_rgb, point)
# บันทึกมาสก์ที่ได้
save_masks(masks) |