Using SAM in VS Code
  1. Clone the Repo & Install Dependencies:
In VS Code terminal:
cd segment-anything
pip install -r requirements.txt
2. Download Pre-Trained Model:
Get the .pth model from segment-anything.com and save it in a models folder.
3. Write a Script:
Create a use_sam.py file:
from segment_anything import SamPredictor, sam_model_registry
from PIL import Image
import numpy as np
sam = sam_model_registry["vit_b"](checkpoint="models/sam_vit_b.pth")
predictor = SamPredictor(sam)
image = np.array(Image.open("your_image.jpg"))
predictor.set_image(image)
masks, _, _ = predictor.predict(point_coords=np.array([[100, 200]]), point_labels=np.array([1]))
print(masks)
4. Run It:
In the terminal:
python use_sam.py
That’s it—start experimenting with SAM!
1
1 comment
Derik Cred
3
Using SAM in VS Code
Data Alchemy
skool.com/data-alchemy
Your Community to Master the Fundamentals of Working with Data and AI — by Datalumina®
Leaderboard (30-day)
Powered by