1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
import base64
# Use this function to convert an image file from the filesystem to base64
def image_file_to_base64(image_path):
with open(image_path, 'rb') as f:
image_data = f.read()
return base64.b64encode(image_data).decode('utf-8')
# Use this function to fetch an image from a URL and convert it to base64
def image_url_to_base64(image_url):
response = requests.get(image_url)
image_data = response.content
return base64.b64encode(image_data).decode('utf-8')
api_key = "YOUR_API_KEY"
url = "https://api.segmind.com/v1/sam-v2-image"
# Request payload
data = {
"base64": False,
"image": image_url_to_base64("https://segmind-sd-models.s3.amazonaws.com/display_images/sam-v2-ip.jpg"), # Or use image_file_to_base64("IMAGE_PATH")
"overlay_mask": True,
"prompt": "boy"
}
headers = {'x-api-key': api_key}
response = requests.post(url, json=data, headers=headers)
print(response.content) # The response is the generated image
Output as base64 encoded string
Coordinates for image selection (optional): Provide either a prompt or coordinates. If a prompt is provided, coordinates will be ignored. For a single coordinate, use the format [834,74]. For multiple coordinates, use [[839,74], [844,20], ...].
Input image
Whether to overlay a mask on the image
Text prompt for object selection
Coordinates to be removed (optional), format is similar to Coordinates
To keep track of your credit usage, you can inspect the response headers of each API call. The x-remaining-credits property will indicate the number of remaining credits in your account. Ensure you monitor this value to avoid any disruptions in your API usage.
SAM v2 Image (Segmentation Anything Model 2) represents a significant advancement in the field of computer vision, specifically addressing promptable visual segmentation in images. Developed by Meta AI, SAM v2 builds upon its predecessor, SAM, to provide robust and efficient solutions for segmenting objects in diverse visual contexts.
Architecture: SAM v2 extends the original SAM model, incorporating innovations for image processing.
Promptable Segmentation: SAM v2 accepts various prompts (points, boxes, or masks) to identify and segment objects in images.
Upload an image (JPG, PNG, or WebP) using the provided field.
Coordinates for selecting objects in an image: Optionally, input coordinates (if needed). Provide either a prompt or coordinates. If a prompt is provided, coordinates will be ignored. For a single coordinate, use the format [834,74]. For multiple coordinates, use [[839,74], [844,20], ...].
Adjust advanced parameters:
Overlay Mask: Decide whether to overlay a mask on the image.
Remove Coordinates: If necessary, remove specific coordinates.
Click “Generate” to obtain the segmented image.
Assisted Image Labeling: SAM v2 assists in recommending annotations for images during labeling tasks.
Augmented Reality (AR) and Virtual Reality (VR): Accurate segmentation enhances realism in AR and VR applications.
Sonar Imaging: SAM v2 can be applied to underwater sonar images for object segmentation.
Autonomous Vehicles: Real-time segmentation aids in object detection for autonomous driving.
Environmental Monitoring: Use SAM v2 to segment objects in satellite imagery for environmental analysis.