API (original) (raw)

At this moment, all features for single image have been accessible via API. I am not planning to support batch process. However, if you really need, you can submit an issue or pull request.

GET /sam/heartbeat

You can use this API to check if this extension is working.

Example:

import requests url = "http://localhost:7861/sam/heartbeat" response = requests.get(url) reply = response.json() print(reply["msg"])

If this extension is working, you should get Success!

GET /sam/sam-model

You can use this API to get the currently available SAM models.

Example:

import requests url = "http://localhost:7861/sam/sam-model" response = requests.get(url) reply = response.json() print(reply)

Example Output:

["sam_vit_b_01ec64.pth", "sam_vit_h_4b8939.pth", "sam_vit_l_0b3195.pth"]

You will receive a list of SAM models that are available, which you can then utilize to set the sam_model_name parameter for the predict API.

POST /sam/sam-predict

You can use this API to get masks from SAM.

Parameters:

One of point prompts and GroundingDINO text prompts must be provided to generate masks.

Returns:

Example:

import base64 import requests from PIL import Image from io import BytesIO

def filename_to_base64(filename): with open(filename, "rb") as fh: return base64.b64encode(fh.read())

img_filename = ".png" url = "http://localhost:7861/sam/sam-predict" payload = { "input_image": filename_to_base64(img_filename).decode(), "dino_enabled": True, "dino_text_prompt": "the girl with blue hair", "dino_preview_checkbox": False, } response = requests.post(url, json=payload) reply = response.json() print(reply["msg"])

grid = Image.new('RGBA', (3 * 512, 3 * 512)) def paste(img, row): for idx, img in enumerate(img): img_pil = Image.open(BytesIO(base64.b64decode(img))).resize((512, 512)) grid.paste(img_pil, (idx * 512, row * 512)) paste(reply["blended_images"], 0) paste(reply["masks"], 1) paste(reply["masked_images"], 2) grid.show()

The output should be very similar to the demo

POST /sam/dino-predict

You can get GroundingDINO bounding boxes with this API.

Parameters:

Returns:

Example:

url = "http://localhost:7861/sam/dino-predict" payload = { "dino_model_name": "GroundingDINO_SwinT_OGC (694MB)", "input_image": filename_to_base64(img_filename).decode(), "text_prompt": "the girl with red hair", } response = requests.post(url, json=payload) reply = response.json() print(reply["msg"])

grid = Image.new('RGBA', (512, 512)) paste([reply["image_with_box"]], 0) grid.show()

POST /sam/dilate-mask

You can use this API to expand the mask created by SAM.

Parameters:

Returns:

Example:

url = "http://localhost:7861/sam/dilate-mask" payload = { "input_image": filename_to_base64(img_filename).decode(), "mask": reply["mask"], } response = requests.post(url, json=payload) reply = response.json()

grid = Image.new('RGBA', (3 * 512, 512)) paste([reply["blended_image"], reply["mask"], reply["masked_image"]], 0) grid.show()

POST /sam/controlnet-seg

You can use this API to generate semantic segmentation enhanced by SAM.

Parameters:

Returns if the preprocessor is not random:

Returns if the preprocessor is random:

Example:

url = "http://localhost:7861/sam/controlnet-seg" payload = { "input_image": filename_to_base64(img_filename).decode(), } response = requests.post(url, json={"payload": payload, "autosam_conf": {}}) reply = response.json() print(reply["msg"])

grid = Image.new('RGBA', (2 * 512, 2 * 512)) paste([reply["blended_presam"], reply["blended_postsam"]], 0) paste([reply["sem_presam"], reply["sem_postsam"]], 1) grid.show()

POST /sam/category-mask

You can use this API to get masks generated by SAM + Semantic segmentation with catogory IDs.

Parameters:

Returns:

Example:

url = "http://localhost:7861/sam/category-mask" payload = { "input_image": filename_to_base64(img_filename).decode(), "category": "12", "processor_res": 1024, } response = requests.post(url, json={"payload": payload, "autosam_conf": {}}) reply = response.json() print(reply["msg"])

grid = Image.new('RGBA', (3 * 512, 512)) paste([reply["blended_image"], reply["mask"], reply["masked_image"]], 0) grid.show()