|
| 1 | +import base64 |
| 2 | +import io |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from io import BytesIO |
| 7 | + |
| 8 | +import imageio.v2 as imageio |
| 9 | +import numpy as np |
| 10 | +import requests |
| 11 | +import torch |
| 12 | +from PIL import Image |
| 13 | + |
| 14 | +# current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 15 | +# full_path = os.path.abspath(os.path.join(current_dir, "../../..")) |
| 16 | +# sys.path.append(full_path) |
| 17 | + |
| 18 | + |
| 19 | +def convert_image_to_rgb(image: Image.Image) -> Image.Image: |
| 20 | + if image.mode != "RGB": |
| 21 | + return image.convert("RGB") |
| 22 | + return image |
| 23 | + |
| 24 | + |
| 25 | +def encode_image_to_base64( |
| 26 | + image: Image.Image, format: str = "WEBP", quality: int = 100, **kwargs |
| 27 | +) -> str: |
| 28 | + image = convert_image_to_rgb(image) |
| 29 | + with io.BytesIO() as output: |
| 30 | + imageio.imwrite(output, image, format=format, quality=quality) |
| 31 | + output.seek(0) |
| 32 | + img_bytes = output.getvalue() |
| 33 | + return base64.b64encode(img_bytes).decode("utf-8") |
| 34 | + |
| 35 | + |
| 36 | +def send_request(create_task_url, payload): |
| 37 | + with TaskClient(create_task_url) as client: |
| 38 | + response = client.pull(payload) |
| 39 | + |
| 40 | + if response is None: |
| 41 | + raise RuntimeError() |
| 42 | + ret = response.json() |
| 43 | + |
| 44 | + if "result" in ret: |
| 45 | + msg = json.loads(ret["result"]) |
| 46 | + else: |
| 47 | + msg = ret |
| 48 | + # print("why msg: ", msg) |
| 49 | + msg = msg["data"] |
| 50 | + if msg["type"] not in ( |
| 51 | + "comfyair", |
| 52 | + "bizyair", |
| 53 | + ): |
| 54 | + raise Exception(f"Unexpected response type: {msg}") |
| 55 | + |
| 56 | + if "error" in msg: |
| 57 | + raise Exception(f"Error happens: {msg}") |
| 58 | + |
| 59 | + # img = msg["image"] |
| 60 | + # mask_img = msg["mask_image"] |
| 61 | + |
| 62 | + # output_file1 = "sam_test.webp" |
| 63 | + # output_file2 = "sam_test_mask.webp" |
| 64 | + # decode_base64_to_image(img, "webp").save(output_file1) |
| 65 | + # decode_base64_to_image(mask_img, "webp").save(output_file2) |
| 66 | + |
| 67 | + |
| 68 | +class TaskClient: |
| 69 | + def __init__(self, create_task_url): |
| 70 | + self.create_task_url = create_task_url |
| 71 | + |
| 72 | + def __enter__(self): |
| 73 | + return self |
| 74 | + |
| 75 | + def __exit__(self, exc_type, exc_value, traceback): |
| 76 | + pass |
| 77 | + |
| 78 | + def pull(self, payload): |
| 79 | + response = requests.post( |
| 80 | + self.create_task_url, |
| 81 | + json=payload, |
| 82 | + headers={"Content-Type": "application/json"}, |
| 83 | + ) |
| 84 | + return response |
| 85 | + |
| 86 | + |
| 87 | +def test_task_creation_and_result_retrieval(): |
| 88 | + create_task_url = "http://0.0.0.0:9899/supernode/sam" |
| 89 | + # create_task_url = "https://bizyair-api.siliconflow.cn/x/v1/supernode/sam" |
| 90 | + image_url = ( |
| 91 | + "https://bizy-air.oss-cn-beijing.aliyuncs.com/examples_asset/sam-people.png" |
| 92 | + ) |
| 93 | + |
| 94 | + # image_to_sam = "people.png" |
| 95 | + # img_pil = Image.open(image_to_sam) |
| 96 | + |
| 97 | + response = requests.get(image_url) |
| 98 | + if response.status_code == 200: |
| 99 | + img_pil = Image.open(BytesIO(response.content)) |
| 100 | + else: |
| 101 | + raise Exception( |
| 102 | + f"Failed to retrieve the image, status code: {response.status_code}" |
| 103 | + ) |
| 104 | + |
| 105 | + mode = 2 # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 106 | + |
| 107 | + ######################使用Point作为Prompt############################## |
| 108 | + input_points = np.array([[500, 375]]) |
| 109 | + input_points = json.dumps(input_points.tolist()) |
| 110 | + input_label = np.array([1]) |
| 111 | + input_label = json.dumps(input_label.tolist()) |
| 112 | + payload = { |
| 113 | + "image": encode_image_to_base64(img_pil), |
| 114 | + "mode": mode, # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 115 | + "params": { |
| 116 | + "input_points": input_points, |
| 117 | + "input_label": input_label, |
| 118 | + "input_boxes": None, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + send_request(create_task_url, payload) |
| 123 | + |
| 124 | + # ###################使用Box作为Prompt############################## |
| 125 | + input_boxes = np.array([451.8652, 71.6301, 648.0280, 1022.0955]) |
| 126 | + input_boxes = json.dumps(input_boxes.tolist()) |
| 127 | + payload = { |
| 128 | + "image": encode_image_to_base64(img_pil), |
| 129 | + "mode": mode, # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 130 | + "params": { |
| 131 | + "input_points": None, |
| 132 | + "input_label": None, |
| 133 | + "input_boxes": input_boxes, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + send_request(create_task_url, payload) |
| 138 | + |
| 139 | + # ######################使用Points和Box作为Prompt############################## |
| 140 | + input_boxes = np.array([451.8652, 71.6301, 648.0280, 1022.0955]) |
| 141 | + input_boxes = json.dumps(input_boxes.tolist()) |
| 142 | + |
| 143 | + input_points = np.array([[575, 750]]) |
| 144 | + input_points = json.dumps(input_points.tolist()) |
| 145 | + input_label = np.array([0]) |
| 146 | + input_label = json.dumps(input_label.tolist()) |
| 147 | + payload = { |
| 148 | + "image": encode_image_to_base64(img_pil), |
| 149 | + "mode": mode, # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 150 | + "params": { |
| 151 | + "input_points": input_points, |
| 152 | + "input_label": input_label, |
| 153 | + "input_boxes": input_boxes, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + send_request(create_task_url, payload) |
| 158 | + |
| 159 | + # #####################使用Batched Box作为Prompt############################## |
| 160 | + input_boxes = torch.tensor( |
| 161 | + [ |
| 162 | + [24.0652, 127.0906, 181.5945, 932.2192], |
| 163 | + [451.8652, 71.6301, 648.0280, 1022.0955], |
| 164 | + [731.7250, 201.1820, 956.5409, 1022.2478], |
| 165 | + [236.7201, 131.5688, 414.6645, 979.8284], |
| 166 | + [145.1680, 183.1925, 308.3481, 955.5884], |
| 167 | + [358.4024, 192.8287, 506.4283, 1011.5869], |
| 168 | + [588.2826, 152.5342, 798.1860, 1021.6285], |
| 169 | + ] |
| 170 | + ) |
| 171 | + input_boxes = json.dumps(input_boxes.tolist()) |
| 172 | + mode = 3 |
| 173 | + payload = { |
| 174 | + "image": encode_image_to_base64(img_pil), |
| 175 | + "mode": mode, # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 176 | + "params": { |
| 177 | + "input_boxes": input_boxes, |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + send_request(create_task_url, payload) |
| 182 | + |
| 183 | + # ####################使用自动模式作为Prompt############################## |
| 184 | + mode = 0 |
| 185 | + payload = { |
| 186 | + "image": encode_image_to_base64(img_pil), |
| 187 | + "mode": mode, # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 188 | + } |
| 189 | + send_request(create_task_url, payload) |
| 190 | + |
| 191 | + ######################使用text模式作为Prompt############################## |
| 192 | + text = "human" |
| 193 | + box_threshold = 0.3 |
| 194 | + text_threshold = 0.25 |
| 195 | + mode = 1 |
| 196 | + payload = { |
| 197 | + "image": encode_image_to_base64(img_pil), |
| 198 | + "mode": mode, # 0: auto mode 1:text mode 2: points/boxes 3: batched boxes |
| 199 | + "params": { |
| 200 | + "prompt": text, |
| 201 | + "box_threshold": box_threshold, # 检测框置信度 |
| 202 | + "text_threshold": text_threshold, # 文本置信度 |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + send_request(create_task_url, payload) |
| 207 | + |
| 208 | + |
| 209 | +if __name__ == "__main__": |
| 210 | + test_task_creation_and_result_retrieval() |
0 commit comments