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
31
32
33
34
35
36
37
38
39
const axios = require('axios');
const fs = require('fs');
const path = require('path');
async function toB64(imgPath) {
const data = fs.readFileSync(path.resolve(imgPath));
return Buffer.from(data).toString('base64');
}
const api_key = "YOUR API-KEY";
const url = "https://api.segmind.com/v1/sam-v21-hiera-large";
const data = {
"image": "toB64('https://huggingface.co/datasets/hf-internal-testing/sam2-fixtures/resolve/main/truck.jpg')",
"make_overlay": true,
"save_polygons": true,
"save_rle": true,
"save_pngs": true,
"points_per_side": 32,
"points_per_batch": 64,
"pred_iou_thresh": 0.86,
"stability_score_thresh": 0,
"min_mask_region_area": 0,
"nms_iou_thresh": 0.7,
"max_masks": 0,
"polygon_epsilon": 0,
"tile_size": 0,
"tile_stride": 0
};
(async function() {
try {
const response = await axios.post(url, data, { headers: { 'x-api-key': api_key } });
console.log(response.data);
} catch (error) {
console.error('Error:', error.response.data);
}
})();
Input image URL or base64 encoded image
Generate overlay image with masks
Include polygon coordinates in output
Include COCO RLE format in output
Generate individual PNG masks in ZIP
Number of points per side for mask generation
min : 8,
max : 64
Number of points per batch for processing
min : 16,
max : 128
Prediction IoU threshold for mask filtering
min : 0.5,
max : 1
Stability score threshold for mask filtering
min : 0,
max : 1
Minimum mask area in pixels
min : 0,
max : 10000
Non-Maximum Suppression IoU threshold
min : 0,
max : 1
Maximum number of masks to return (0 = no limit)
min : 0,
max : 100
Epsilon for polygon simplification
min : 0,
max : 10
Tile size for large images (0 = disabled)
min : 0,
max : 2048
Tile stride for large images (0 = use tile_size)
min : 0,
max : 2048
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.1 (Segment Anything Model 2.1) represents the next evolution in promptable visual segmentation by Meta AI, delivering more accurate and efficient mask generation for a wide range of image types and contexts.