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
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-img2img";
const data = {
"image": "toB64('https://segmind.com/kitchen.jpg')"
};
(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);
}
})();
Image to Segment
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.
The Segment Anything Model (SAM) is the latest groundbreaking AI model introduced by Meta AI. With the power to "cut out" any object from any image with just a single click, SAM is revolutionizing the world of image processing. Zero-shot capability; Unlike other models that require extensive training for each new object or image, SAM boasts of a promptable segmentation system. This means it can easily generalize to unfamiliar objects and images without the need for any additional training.
The SAM model architecture integrates a masked auto-encoder (MAE) and a Vision Transformer (ViT) to generate distinct image embeddings, typically initialized before model prompting. SAM's prompt encoding component transforms elements such as points, masks, and texts into immediate vectors, categorizing prompts into sparse or dense groups. Subsequently, SAM's mask decoder predicts segmentation masks by leveraging these embeddings, aligning them with a specific output token. Through bidirectional cross-attention, the labeled masks are processed to enhance SAM's weights, ensuring its adaptability and streamlined performance.
Zero-shot Learning: SAM can identify and segment unfamiliar objects without any prior training, making it highly versatile.
Promptable System: Users can easily guide SAM using prompts, ensuring more accurate results tailored to specific needs.
Speed and Efficiency: Thanks to its advanced architecture, SAM processes images rapidly, delivering results in real-time.
High Precision : SAM's extensive training and hybrid architecture ensure that the segmented results are of the highest quality.
E-commerce Product Listing: Retailers can use SAM to automatically segment product images, removing backgrounds and placing products on a consistent backdrop.
Photo Editing Apps: SAM can be integrated into photo editing software, allowing users to easily extract objects and create composite images.
Augmented Reality (AR) Applications: SAM can help in real-time object segmentation, enabling developers to overlay virtual objects onto real-world scenes seamlessly.
Medical Imaging: Healthcare professionals can use SAM to segment specific organs or anomalies in medical images, aiding in diagnosis and treatment planning.
Video Production: Film and video editors can utilize SAM to segment and replace backgrounds, create special effects, or integrate CGI elements into real footage.
The Segment Anything Model (SAM) is open source. Meta AI has released the SA-1B Dataset for research purposes and also made available the pre-trained models (approximately 2.4 GB) and associated code. All of these are licensed under the Apache 2.0, a permissive license, in line with FAIR's dedication to open research.