pipeline.py 2.1 KB

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