Project04 / app.py
Noeies's picture
Create app.py
f602b02 verified
raw
history blame
742 Bytes
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)