attribute_recognition.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 pathlib import Path
  15. import numpy as np
  16. from ..utils.io import ImageReader
  17. from ..components import CropByBoxes
  18. from ..results import AttributeRecResult
  19. from .base import BasePipeline
  20. class AttributeRecPipeline(BasePipeline):
  21. """Attribute Rec Pipeline"""
  22. def __init__(
  23. self,
  24. det_model,
  25. cls_model,
  26. det_batch_size=1,
  27. cls_batch_size=1,
  28. device=None,
  29. predictor_kwargs=None,
  30. ):
  31. super().__init__(device, predictor_kwargs)
  32. self._build_predictor(det_model, cls_model)
  33. self.set_predictor(det_batch_size, cls_batch_size, device)
  34. def _build_predictor(self, det_model, cls_model):
  35. self.det_model = self._create(model=det_model)
  36. self.cls_model = self._create(model=cls_model)
  37. self._crop_by_boxes = CropByBoxes()
  38. self._img_reader = ImageReader(backend="opencv")
  39. def set_predictor(self, det_batch_size=None, cls_batch_size=None, device=None):
  40. if det_batch_size:
  41. self.det_model.set_predictor(batch_size=det_batch_size)
  42. if cls_batch_size:
  43. self.cls_model.set_predictor(batch_size=cls_batch_size)
  44. if device:
  45. self.det_model.set_predictor(device=device)
  46. self.cls_model.set_predictor(device=device)
  47. def predict(self, input, **kwargs):
  48. self.set_predictor(**kwargs)
  49. for det_res in self.det_model(input):
  50. cls_res = self.get_cls_result(det_res)
  51. yield self.get_final_result(det_res, cls_res)
  52. def get_cls_result(self, det_res):
  53. subs_of_img = list(self._crop_by_boxes(det_res))
  54. img_list = [img["img"] for img in subs_of_img]
  55. all_cls_res = list(self.cls_model(img_list))
  56. output = {"label": [], "score": []}
  57. for res in all_cls_res:
  58. output["label"].append(res["label_names"])
  59. output["score"].append(res["scores"])
  60. return output
  61. def get_final_result(self, det_res, cls_res):
  62. single_img_res = {"input_path": det_res["input_path"], "boxes": []}
  63. for i, obj in enumerate(det_res["boxes"]):
  64. cls_scores = cls_res["score"][i]
  65. labels = cls_res["label"][i]
  66. single_img_res["boxes"].append(
  67. {
  68. "labels": labels,
  69. "cls_scores": cls_scores,
  70. "det_score": obj["score"],
  71. "coordinate": obj["coordinate"],
  72. }
  73. )
  74. return AttributeRecResult(single_img_res)
  75. class PedestrianAttributeRecPipeline(AttributeRecPipeline):
  76. entities = "pedestrian_attribute_recognition"
  77. class VehicleAttributeRecPipeline(AttributeRecPipeline):
  78. entities = "vehicle_attribute_recognition"