pipeline.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2022 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. #include "ultra_infer/pipeline/pptinypose/pipeline.h"
  15. namespace ultra_infer {
  16. namespace pipeline {
  17. PPTinyPose::PPTinyPose(
  18. ultra_infer::vision::detection::PicoDet *det_model,
  19. ultra_infer::vision::keypointdetection::PPTinyPose *pptinypose_model)
  20. : detector_(det_model), pptinypose_model_(pptinypose_model) {}
  21. bool PPTinyPose::Detect(cv::Mat *img,
  22. ultra_infer::vision::DetectionResult *detection_res) {
  23. if (!detector_->Predict(img, detection_res)) {
  24. FDERROR << "There's a error while detectiong human box in image."
  25. << std::endl;
  26. return false;
  27. }
  28. return true;
  29. }
  30. bool PPTinyPose::KeypointDetect(
  31. cv::Mat *img, ultra_infer::vision::KeyPointDetectionResult *result,
  32. ultra_infer::vision::DetectionResult &detection_result) {
  33. if (!pptinypose_model_->Predict(img, result, detection_result)) {
  34. FDERROR << "There's a error while detecting keypoint in image "
  35. << std::endl;
  36. return false;
  37. }
  38. return true;
  39. }
  40. bool PPTinyPose::Predict(cv::Mat *img,
  41. ultra_infer::vision::KeyPointDetectionResult *result) {
  42. result->Clear();
  43. ultra_infer::vision::DetectionResult detection_res;
  44. if (nullptr != detector_ && !Detect(img, &detection_res)) {
  45. FDERROR << "Failed to detect image." << std::endl;
  46. return false;
  47. }
  48. ultra_infer::vision::DetectionResult filter_detection_res;
  49. for (size_t i = 0; i < detection_res.boxes.size(); ++i) {
  50. if (detection_res.scores[i] > detection_model_score_threshold) {
  51. filter_detection_res.boxes.push_back(detection_res.boxes[i]);
  52. filter_detection_res.scores.push_back(detection_res.scores[i]);
  53. filter_detection_res.label_ids.push_back(detection_res.label_ids[i]);
  54. }
  55. }
  56. if (nullptr != pptinypose_model_ &&
  57. !KeypointDetect(img, result, filter_detection_res)) {
  58. FDERROR << "Failed to detect keypoint in image " << std::endl;
  59. return false;
  60. }
  61. return true;
  62. };
  63. } // namespace pipeline
  64. } // namespace ultra_infer