postprocess_lprnet.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*************************************************************************
  2. * Copyright (C) [2021] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <algorithm>
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. #include "cnstream_frame_va.hpp"
  25. #include "cnstream_logging.hpp"
  26. #include "postproc.hpp"
  27. static inline
  28. size_t ArgMax(float* begin, int len) {
  29. return std::distance(begin, std::max_element(begin, begin + len));
  30. }
  31. class PostprocLprnet : public cnstream::ObjPostproc {
  32. public:
  33. int Execute(const std::vector<float*>& net_outputs, const std::shared_ptr<edk::ModelLoader>& model,
  34. const cnstream::CNFrameInfoPtr& finfo, const std::shared_ptr<cnstream::CNInferObject>& obj) override;
  35. DECLARE_REFLEX_OBJECT_EX(PostprocLprnet, cnstream::ObjPostproc)
  36. }; // classd PostprocLprnet
  37. IMPLEMENT_REFLEX_OBJECT_EX(PostprocLprnet, cnstream::ObjPostproc)
  38. int PostprocLprnet::Execute(const std::vector<float*>& net_outputs,
  39. const std::shared_ptr<edk::ModelLoader>& model,
  40. const cnstream::CNFrameInfoPtr& finfo,
  41. const std::shared_ptr<cnstream::CNInferObject>& obj) {
  42. static const std::string kChars[] = {
  43. "京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  44. "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤",
  45. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁",
  46. "新",
  47. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  48. "A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
  49. "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
  50. "W", "X", "Y", "Z"
  51. };
  52. static constexpr int kNChar = 65;
  53. static constexpr int kPlateLen = 7;
  54. const int seq_len = model->OutputShape(0).C();
  55. float* data = net_outputs[0];
  56. LOGF_IF(POSTPROC_LPRNET, seq_len <= kNChar) << "Can not deal with this lprnet model!";
  57. const int nlabel = model->OutputShape(0).H();
  58. std::string plate_number = "";
  59. int pre_ch_idx = kNChar;
  60. float score = 0.0f;
  61. int len = 0;
  62. for (int label_idx = 0; label_idx < nlabel; ++label_idx) {
  63. int ch_idx = ArgMax(data + label_idx * seq_len, seq_len);
  64. if (ch_idx >= kNChar) continue;
  65. if (pre_ch_idx != ch_idx) {
  66. plate_number += kChars[ch_idx];
  67. score += data[label_idx * seq_len + ch_idx];
  68. len++;
  69. }
  70. pre_ch_idx = ch_idx;
  71. }
  72. if (len != kPlateLen) return 0;
  73. score /= len;
  74. if (score < threshold_) return 0;
  75. if (obj->collection.HasValue("plate_container")) {
  76. // plate_container set in PostprocMSSDPlateDetection
  77. // see CNStream/samples/common/postprocess/postprocess_mobilenet_ssd_plate_detection.cpp
  78. obj->collection.Get<decltype(obj)>("plate_container")->AddExtraAttribute("plate_number", plate_number);
  79. obj->collection.Get<decltype(obj)>("plate_container")->AddExtraAttribute("plate_ocr_score", std::to_string(score));
  80. }
  81. return 0;
  82. }