plugin  0.1.0
performance_metrics.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
10 #pragma once
11 
12 #include <chrono>
13 #include <iomanip>
14 #include <iostream>
15 #include <sstream>
16 
17 #include "utils/ocv_common.hpp"
18 
20 public:
21  using Clock = std::chrono::steady_clock;
22  using TimePoint = std::chrono::time_point<Clock>;
23  using Duration = Clock::duration;
24  using Ms = std::chrono::duration<double, std::ratio<1, 1000>>;
25  using Sec = std::chrono::duration<double, std::ratio<1, 1>>;
26 
27  struct Metrics {
28  double latency;
29  double fps;
30  };
31 
32  enum MetricTypes {
33  ALL,
34  FPS,
35  LATENCY
36  };
37 
38  PerformanceMetrics(Duration timeWindow = std::chrono::seconds(1));
39  void update(TimePoint lastRequestStartTime,
40  const cv::Mat& frame,
41  cv::Point position = {15, 30},
42  int fontFace = cv::FONT_HERSHEY_COMPLEX,
43  double fontScale = 0.75,
44  cv::Scalar color = {200, 10, 10},
45  int thickness = 2, MetricTypes metricType = ALL);
46  void update(TimePoint lastRequestStartTime);
47 
54  void paintMetrics(const cv::Mat& frame,
55  cv::Point position = { 15, 30 },
56  int fontFace = cv::FONT_HERSHEY_COMPLEX,
57  double fontScale = 0.75,
58  cv::Scalar color = { 200, 10, 10 },
59  int thickness = 2, MetricTypes metricType = ALL) const;
60 
61  Metrics getLast() const;
62  Metrics getTotal() const;
63  void logTotal() const;
64 
65 private:
66  struct Statistic {
67  Duration latency;
68  Duration period;
69  int frameCount;
70 
71  Statistic() {
72  latency = Duration::zero();
73  period = Duration::zero();
74  frameCount = 0;
75  }
76 
77  void combine(const Statistic& other) {
78  latency += other.latency;
79  period += other.period;
80  frameCount += other.frameCount;
81  }
82  };
83 
84  Duration timeWindowSize;
85  Statistic lastMovingStatistic;
86  Statistic currentMovingStatistic;
87  Statistic totalStatistic;
88  TimePoint lastUpdateTime;
89  bool firstFrameProcessed;
90 };
91 
92 void logLatencyPerStage(double readLat, double preprocLat, double inferLat, double postprocLat, double renderLat);
Definition: performance_metrics.hpp:19
void paintMetrics(const cv::Mat &frame, cv::Point position={ 15, 30 }, int fontFace=cv::FONT_HERSHEY_COMPLEX, double fontScale=0.75, cv::Scalar color={ 200, 10, 10 }, int thickness=2, MetricTypes metricType=ALL) const
Definition: performance_metrics.cpp:48
a header file with common samples functionality using OpenCV
Definition: performance_metrics.hpp:27