pedestrian_attribute_recognition.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 List
  15. from fastapi import FastAPI, HTTPException
  16. from pydantic import BaseModel, Field
  17. from typing_extensions import Annotated, TypeAlias
  18. from .....utils import logging
  19. from ...attribute_recognition import PedestrianAttributeRecPipeline
  20. from .. import utils as serving_utils
  21. from ..app import AppConfig, create_app
  22. from ..models import Response, ResultResponse
  23. class InferRequest(BaseModel):
  24. image: str
  25. BoundingBox: TypeAlias = Annotated[List[float], Field(min_length=4, max_length=4)]
  26. class Attribute(BaseModel):
  27. label: str
  28. score: float
  29. class Pedestrian(BaseModel):
  30. bbox: BoundingBox
  31. attributes: List[Attribute]
  32. score: float
  33. class InferResult(BaseModel):
  34. pedestrians: List[Pedestrian]
  35. image: str
  36. def create_pipeline_app(
  37. pipeline: PedestrianAttributeRecPipeline, app_config: AppConfig
  38. ) -> FastAPI:
  39. app, ctx = create_app(
  40. pipeline=pipeline, app_config=app_config, app_aiohttp_session=True
  41. )
  42. @app.post(
  43. "/pedestrian-attribute-recognition",
  44. operation_id="infer",
  45. responses={422: {"model": Response}},
  46. )
  47. async def _infer(request: InferRequest) -> ResultResponse[InferResult]:
  48. pipeline = ctx.pipeline
  49. aiohttp_session = ctx.aiohttp_session
  50. try:
  51. file_bytes = await serving_utils.get_raw_bytes(
  52. request.image, aiohttp_session
  53. )
  54. image = serving_utils.image_bytes_to_array(file_bytes)
  55. result = (await pipeline.infer(image))[0]
  56. pedestrians: List[Pedestrian] = []
  57. for obj in result["boxes"]:
  58. pedestrians.append(
  59. Pedestrian(
  60. bbox=obj["coordinate"],
  61. attributes=[
  62. Attribute(label=l, score=s)
  63. for l, s in zip(obj["labels"], obj["cls_scores"])
  64. ],
  65. score=obj["det_score"],
  66. )
  67. )
  68. output_image_base64 = serving_utils.base64_encode(
  69. serving_utils.image_to_bytes(result.img)
  70. )
  71. return ResultResponse(
  72. logId=serving_utils.generate_log_id(),
  73. errorCode=0,
  74. errorMsg="Success",
  75. result=InferResult(pedestrians=pedestrians, image=output_image_base64),
  76. )
  77. except Exception as e:
  78. logging.exception(e)
  79. raise HTTPException(status_code=500, detail="Internal server error")
  80. return app