pipeline.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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. import numpy as np
  15. from ....utils.deps import pipeline_requires_extra
  16. from ...utils.benchmark import benchmark
  17. from ..pp_shitu_v2 import ShiTuV2Pipeline
  18. from .result import FaceRecResult
  19. @benchmark.time_methods
  20. @pipeline_requires_extra("cv")
  21. class FaceRecPipeline(ShiTuV2Pipeline):
  22. """Face Recognition Pipeline"""
  23. entities = "face_recognition"
  24. def get_rec_result(
  25. self, raw_img, det_res, indexer, rec_threshold, hamming_radius, topk
  26. ):
  27. if len(det_res["boxes"]) == 0:
  28. return {"label": [], "score": []}
  29. subs_of_img = list(self.crop_by_boxes(raw_img, det_res["boxes"]))
  30. img_list = [img["img"] for img in subs_of_img]
  31. all_rec_res = list(self.rec_model(img_list))
  32. all_rec_res = indexer(
  33. [rec_res["feature"] for rec_res in all_rec_res],
  34. score_thres=rec_threshold,
  35. hamming_radius=hamming_radius,
  36. topk=topk,
  37. )
  38. output = {"label": [], "score": []}
  39. for res in all_rec_res:
  40. output["label"].append(res["label"])
  41. output["score"].append(res["score"])
  42. return output
  43. def get_final_result(self, input_data, raw_img, det_res, rec_res):
  44. single_img_res = {"input_path": input_data, "input_img": raw_img, "boxes": []}
  45. for i, obj in enumerate(det_res["boxes"]):
  46. rec_scores = rec_res["score"][i]
  47. if isinstance(rec_scores, np.ndarray):
  48. rec_scores = rec_scores.tolist()
  49. labels = rec_res["label"][i]
  50. single_img_res["boxes"].append(
  51. {
  52. "labels": labels,
  53. "rec_scores": rec_scores,
  54. "det_score": obj["score"],
  55. "coordinate": obj["coordinate"],
  56. }
  57. )
  58. return FaceRecResult(single_img_res)