pipeline.py 2.3 KB

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