{"library":"segment-anything","title":"Segment Anything Model (SAM)","description":"The Segment Anything Model (SAM) from Meta AI is a new foundation model for image segmentation, capable of cutting out any object in any image with a single click. It is designed to be a general-purpose segmentation model, applicable to various downstream tasks. The current stable PyPI version is 1.0, with updates generally tied to significant advancements rather than frequent releases.","language":"python","status":"active","last_verified":"Fri May 15","install":{"commands":["pip install segment-anything"],"cli":null},"imports":["from segment_anything import sam_model_registry, SamAutomaticMaskGenerator","from segment_anything import sam_model_registry, SamPredictor"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import numpy as np\nimport torch\nimport os\n\n# NOTE: You must download a model checkpoint first (e.g., sam_vit_h_4b8939.pth)\n# from https://github.com/facebookresearch/segment-anything/releases/tag/v1.0\n# For this example, we'll assume a dummy path and model type.\nSAM_CHECKPOINT_PATH = os.environ.get('SAM_CHECKPOINT', 'sam_vit_h_4b8939.pth')\nMODEL_TYPE = os.environ.get('SAM_MODEL_TYPE', 'vit_h') # e.g., 'vit_h', 'vit_l', 'vit_b'\n\n# Dummy image data (replace with actual image loading, e.g., using OpenCV)\n# Assuming a 1024x1024 RGB image for demonstration\nimage = np.zeros((1024, 1024, 3), dtype=np.uint8)\n# Simulate loading a real image:\n# import cv2\n# image_path = 'path/to/your/image.jpg'\n# image = cv2.imread(image_path)\n# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Important: Convert BGR to RGB\n\n# Check if checkpoint exists\nif not os.path.exists(SAM_CHECKPOINT_PATH):\n    print(f\"Warning: Model checkpoint '{SAM_CHECKPOINT_PATH}' not found.\\n\"+\n          \"Please download it from the official Segment Anything GitHub releases.\")\n    # Exit or provide dummy output for demonstration purposes\n    exit()\n\nfrom segment_anything import sam_model_registry, SamPredictor\n\n# Initialize SAM model\nsam = sam_model_registry[MODEL_TYPE](checkpoint=SAM_CHECKPOINT_PATH)\n\n# Set device: 'cuda' for GPU if available, else 'cpu'\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nsam.to(device=device)\nprint(f\"Using device: {device}\")\n\n# Create a predictor\npredictor = SamPredictor(sam)\npredictor.set_image(image)\n\n# Example: Point prompt for a single object\ninput_point = np.array([[500, 375]]) # Coordinates [x, y]\ninput_label = np.array([1])      # 1 for foreground, 0 for background\n\n# Predict masks\nmasks, scores, logits = predictor.predict(\n    point_coords=input_point,\n    point_labels=input_label,\n    multimask_output=True,\n)\n\nprint(f\"Generated {len(masks)} masks.\")\nprint(f\"Scores: {scores}\")\n# print(f\"First mask shape: {masks[0].shape}, dtype: {masks[0].dtype}\")\n# The 'masks' array contains boolean masks: True for foreground, False for background\n","lang":"python","description":"This quickstart demonstrates how to initialize the Segment Anything Model (SAM) and use `SamPredictor` for point-based inference. It highlights the necessity of downloading a model checkpoint and correctly setting the device. For automatic mask generation, `SamAutomaticMaskGenerator` would be used instead.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-15","installed_version":"1.0","pypi_latest":"1.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":1.5,"avg_import_s":null,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"18.0M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"19M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"19.9M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.8M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.5M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.5M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"segment-anything","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.7,"import_time_s":null,"mem_mb":null,"disk_size":"18M"}]}}