@@ -139,8 +139,13 @@ int main(int argc, char** argv) {
139139
140140 torch::NoGradGuard no_grad; // inference only
141141
142- auto start_total = std::chrono::steady_clock::now ();
142+ std::chrono::time_point<std::chrono::system_clock> start_total = std::chrono::system_clock::now ();
143+ std::chrono::time_point<std::chrono::system_clock> last_update = std::chrono::system_clock::now ();
144+ const double max_fps = 30.0 ;
145+ const double max_frame_delay = 1000.0 / max_fps;
146+
143147 size_t frame_count = 0 ;
148+ size_t last_frame_count = 0 ;
144149
145150 while (true ) {
146151 // std::cout << "\r[INFO] Processing frame... " << frame_count + 1 << std::flush;
@@ -149,6 +154,9 @@ int main(int argc, char** argv) {
149154 if (video_loop && frame_count > 0 ) {
150155 cap = open_camera (vid_path);
151156 frame_count = 0 ;
157+ last_frame_count = 0 ;
158+ start_total = std::chrono::system_clock::now ();
159+ last_update = std::chrono::system_clock::now (); // ??? not sure
152160 cap.set (cv::CAP_PROP_POS_FRAMES, 0 );
153161 std::cout << " \r [INFO] Replaying video..." << std::flush;
154162 continue ;
@@ -183,17 +191,39 @@ int main(int argc, char** argv) {
183191
184192 // Display FPS
185193 ++frame_count;
186- auto now = std::chrono::steady_clock::now ();
187- auto seconds = std::chrono::duration_cast<std::chrono::duration<double >>(now - start_total).count ();
188- if (seconds >= 1.0 ) {
189- double fps = frame_count / seconds;
190- std::cout << " \r [INFO] FPS: " << fps << std::flush;
191- frame_count = 0 ;
192- start_total = now;
193- }
194+ const std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now ();
195+ auto delta = now - last_update;
196+ // std::chrono::milliseconds delta_millis = std::chrono::duration_cast<std::chrono::microseconds>(delta);
197+ double delta_time = std::chrono::duration_cast<std::chrono::duration<double ,std::milli>>(delta).count ();
198+ std::cout << " \r [INFO] Frame time: " << delta_time * 1000.0 << " ms" << std::flush;
199+
200+
201+ // auto now = std::chrono::steady_clock::now();
202+ // auto delta = std::chrono::duration_cast<std::chrono::duration<double>>(now - start_total);
203+ // double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(delta).count();
204+ // double max_frame_count = max_fps / seconds;
205+ // double fps = frame_count / seconds;
206+
207+
208+
209+ // std::this_thread::sleep_for(delta);
210+
211+ // Sleep just to avoid too high FPS
212+
213+ // if (fps > max_fps) {
214+ // double missed_frames = fps - max_fps;
215+ // std::this_thread::sleep_for(std::chrono::milliseconds(missed_frames / max_fps));
216+ // }
217+
218+
219+
220+ double fps = (frame_count - last_frame_count) / delta_time;
221+ std::cout << " \r [INFO] FPS: " << fps << std::flush;
222+ last_update = now;
194223
195224 // Display (optional)
196225 cv::imshow (" webcam" , output_bgr);
226+ last_frame_count = frame_count;
197227 if (cv::waitKey (1 ) == 27 ) { // ESC key
198228 break ;
199229 }
@@ -205,3 +235,12 @@ int main(int argc, char** argv) {
205235}
206236
207237
238+
239+
240+
241+ // if (seconds >= 1.0) {
242+ // double fps = frame_count / seconds;
243+ // std::cout << "\r[INFO] FPS: " << fps << std::flush;
244+ // frame_count = 0;
245+ // start_total = now;
246+ // }
0 commit comments