instance_segmentation.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, List
  15. import numpy as np
  16. import pycocotools.mask as mask_util
  17. from fastapi import FastAPI
  18. from ...infra import utils as serving_utils
  19. from ...infra.config import AppConfig
  20. from ...infra.models import ResultResponse
  21. from ...schemas.instance_segmentation import INFER_ENDPOINT, InferRequest, InferResult
  22. from .._app import create_app, primary_operation
  23. def _rle(mask: np.ndarray) -> str:
  24. rle_res = mask_util.encode(np.asarray(mask[..., None], order="F", dtype="uint8"))[0]
  25. return rle_res["counts"].decode("utf-8")
  26. def create_pipeline_app(pipeline: Any, app_config: AppConfig) -> FastAPI:
  27. app, ctx = create_app(
  28. pipeline=pipeline,
  29. app_config=app_config,
  30. app_aiohttp_session=True,
  31. )
  32. @primary_operation(
  33. app,
  34. INFER_ENDPOINT,
  35. "infer",
  36. )
  37. async def _infer(request: InferRequest) -> ResultResponse[InferResult]:
  38. pipeline = ctx.pipeline
  39. aiohttp_session = ctx.aiohttp_session
  40. file_bytes = await serving_utils.get_raw_bytes_async(
  41. request.image, aiohttp_session
  42. )
  43. image = serving_utils.image_bytes_to_array(file_bytes)
  44. result = (await pipeline.infer(image, threshold=request.threshold))[0]
  45. instances: List[Dict[str, Any]] = []
  46. for obj, mask in zip(result["boxes"], result["masks"]):
  47. rle_res = _rle(mask)
  48. mask = dict(rleResult=rle_res, size=mask.shape)
  49. instances.append(
  50. dict(
  51. bbox=obj["coordinate"],
  52. categoryId=obj["cls_id"],
  53. categoryName=obj["label"],
  54. score=obj["score"],
  55. mask=mask,
  56. )
  57. )
  58. if ctx.config.visualize:
  59. output_image_base64 = serving_utils.base64_encode(
  60. serving_utils.image_to_bytes(result.img["res"])
  61. )
  62. else:
  63. output_image_base64 = None
  64. return ResultResponse[InferResult](
  65. logId=serving_utils.generate_log_id(),
  66. result=InferResult(instances=instances, image=output_image_base64),
  67. )
  68. return app