(model_id: str, image_file_name: str)
| 212 | return result |
| 213 | @task |
| 214 | def image_segmentation(model_id: str, image_file_name: str) -> str: |
| 215 | inference = InferenceApi(repo_id=model_id, token=CONFIG["huggingface"]["token"]) |
| 216 | img_data = image_to_bytes(f"{DIRPATH}/{INPUT_PATH}/{image_file_name}") |
| 217 | image = Image.open(BytesIO(img_data)) |
| 218 | predicted = inference(data=img_data) |
| 219 | colors = [] |
| 220 | for i in range(len(predicted)): |
| 221 | colors.append((random.randint(100, 255), random.randint(100, 255), random.randint(100, 255), 155)) |
| 222 | for i, pred in enumerate(predicted): |
| 223 | mask = pred.pop("mask").encode("utf-8") |
| 224 | mask = base64.b64decode(mask) |
| 225 | mask = Image.open(BytesIO(mask), mode='r') |
| 226 | mask = mask.convert('L') |
| 227 | |
| 228 | layer = Image.new('RGBA', mask.size, colors[i]) |
| 229 | image.paste(layer, (0, 0), mask) |
| 230 | name = str(uuid.uuid4())[:4] |
| 231 | image.save(f"{DIRPATH}/{OUTPUT_PATH}/{name}.jpg") |
| 232 | result = {} |
| 233 | result["generated image"] = f"{name}.jpg" |
| 234 | result["predicted"] = predicted |
| 235 | return str(result) |
| 236 | @task |
| 237 | def object_detection(model_id: str, image_file_name: str) -> str: |
| 238 | inference = InferenceApi(repo_id=model_id, token=CONFIG["huggingface"]["token"]) |
nothing calls this directly
no test coverage detected