{"id":28145,"library":"rf-sam-2","title":"RF SAM 2","description":"Roboflow-maintained package for Meta's SAM 2 (Segment Anything Model 2) for image and video segmentation. Currently version 1.0.3, updated March 2025.","status":"active","version":"1.0.3","language":"python","source_language":"en","source_url":"https://github.com/roboflow/sam2","tags":["sam2","segment-anything","roboflow","image-segmentation","video-segmentation"],"install":[{"cmd":"pip install rf-sam-2","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"PyTorch backend required for model inference","package":"torch","optional":false},{"reason":"Image/video I/O utilities","package":"opencv-python","optional":true}],"imports":[{"note":"The package uses rf_sam_2 module name, not sam2","wrong":"from sam2 import SAM2","symbol":"SAM2","correct":"from rf_sam_2 import SAM2"},{"note":"Use rf_sam_2 prefix","wrong":"from sam2 import SAM2VideoPredictor","symbol":"SAM2VideoPredictor","correct":"from rf_sam_2 import SAM2VideoPredictor"},{"note":"Different API from original SAM","wrong":"from segment_anything import sam_model_registry","symbol":"SAM2ImagePredictor","correct":"from rf_sam_2 import SAM2ImagePredictor"}],"quickstart":{"code":"import torch\nfrom rf_sam_2 import SAM2\n\n# Load model (set environment variable for API key if needed)\nmodel = SAM2.from_pretrained(\"sam2-hiera-large\")\n\n# Predict with an image\nimport cv2\nimage = cv2.imread(\"path/to/image.jpg\")\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\nwith torch.inference_mode():\n    masks, scores, logits = model.predict(image, point_coords=[[500, 375]], point_labels=[1])\n\nprint(masks.shape, scores)","lang":"python","description":"Basic image segmentation with a single point prompt."},"warnings":[{"fix":"Use 'from rf_sam_2 import SAM2' instead of 'from sam2 import SAM2'.","message":"The import path changed from 'sam2' to 'rf_sam_2' in version 1.0.0.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Replace 'sam_model_registry' with 'SAM2.from_pretrained()'.","message":"The old 'segment_anything' API is not supported; use the new SAM2 classes.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"os.environ['SAM2_DEVICE'] = 'cpu' before importing rf_sam_2.","message":"CPU inference requires setting environment variable 'SAM2_DEVICE=cpu' before import.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Install rf-sam-2 and use: from rf_sam_2 import SAM2","cause":"Incorrect import path; the module is 'rf_sam_2' not 'sam2'.","error":"ImportError: cannot import name 'SAM2' from 'sam2'"},{"fix":"Set environment variable SAM2_DEVICE=cpu before importing: os.environ['SAM2_DEVICE'] = 'cpu'","cause":"CUDA not available but default device is cuda.","error":"RuntimeError: No CUDA GPUs are available"},{"fix":"Use SAM2.from_pretrained() or SAM2ImagePredictor.from_pretrained() instead.","cause":"Using old SAM API with new SAM2 package.","error":"AttributeError: module 'rf_sam_2' has no attribute 'sam_model_registry'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}