Download src/tools/segmentation_model.py from VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling: direct link, hf CLI and curl.
- Browser
- Download file 583 Bytes
-
https://huggingface.co/spaces/VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling/resolve/main/src/tools/segmentation_model.py
- Command line
-
hf download hf://spaces/VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling/src/tools/segmentation_model.py
-
curl -L -o segmentation_model.py https://huggingface.co/spaces/VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling/resolve/main/src/tools/segmentation_model.py
583 Bytes
| from transformers import SamModel, SamProcessor | |
| from PIL import Image | |
| segmentation_model_id = "facebook/sam-vit-base" | |
| sam_processor = SamProcessor.from_pretrained(segmentation_model_id) | |
| sam_model = SamModel.from_pretrained(segmentation_model_id) | |
| def run_segmentation(image_path: str): | |
| """SAM: return binary masks as nested lists""" | |
| img = Image.open(image_path).convert("RGB") | |
| inputs = sam_processor(images=img, return_tensors="pt") | |
| outputs = sam_model(**inputs) | |
| masks = outputs.pred_masks.squeeze(0).cpu().detach().numpy().tolist() | |
| return {"masks": masks} |