pipeline.py 2.3 KB

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