cnosd.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*************************************************************************
  2. * Copyright (C) [2019] 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. #ifndef EDK_SAMPLES_CNOSD_H_
  21. #define EDK_SAMPLES_CNOSD_H_
  22. #include <opencv2/highgui/highgui.hpp>
  23. #include <opencv2/imgproc/imgproc.hpp>
  24. #include <cstring>
  25. #include <fstream>
  26. #include <string>
  27. #include <vector>
  28. #include "easytrack/easy_track.h"
  29. using cv::Mat;
  30. using cv::Point;
  31. using cv::Scalar;
  32. using std::string;
  33. using std::vector;
  34. class CnOsd {
  35. private:
  36. int box_thickness_ = 2;
  37. vector<string> labels_;
  38. vector<Scalar> colors_;
  39. int font_ = cv::FONT_HERSHEY_SIMPLEX;
  40. cv::Size bm_size_ = {1920, 1080}; // benchmark size,used to calculate scale.
  41. float bm_rate_ = 1.0f; // benchmark rate, used to calculate scale.
  42. inline float CalScale(uint64_t area) const {
  43. float c = 0.3f;
  44. float a = (c - bm_rate_) / std::pow(bm_size_.width * bm_size_.height, 2);
  45. float b = 2 * (bm_rate_ - c) / (bm_size_.width * bm_size_.height);
  46. float scale = a * area * area + b * area + c;
  47. if (scale < 0) return 0;
  48. return scale;
  49. }
  50. public:
  51. CnOsd() = default;
  52. explicit CnOsd(const vector<std::string>& labels);
  53. explicit CnOsd(const std::string& label_fname);
  54. void LoadLabels(const std::string& fname);
  55. inline const std::vector<std::string> labels() const { return labels_; }
  56. void DrawId(Mat image, string text) const;
  57. void DrawFps(Mat image, float fps) const;
  58. void DrawLabel(Mat image, const vector<edk::DetectObject>& objects) const;
  59. void set_font(int font);
  60. inline void set_benchmark_size(cv::Size size) { bm_size_ = size; }
  61. inline cv::Size benchmark_size() const { return bm_size_; }
  62. inline void set_benchmark_rate(float rate) { bm_rate_ = rate; }
  63. inline float benchmark_rate() const { return bm_rate_; }
  64. inline void set_box_thickness(int box_thickness) { box_thickness_ = box_thickness; }
  65. inline int get_box_thickness() const { return box_thickness_; }
  66. };
  67. #endif // EDK_SAMPLES_CNOSD_H_