diff options
author | Stéphane Lenclud <github@lenclud.com> | 2019-04-16 12:23:22 +0200 |
---|---|---|
committer | Stéphane Lenclud <github@lenclud.com> | 2019-04-24 18:46:12 +0200 |
commit | 1fb6122dce1a5bc117e925006d87828a52cfecb6 (patch) | |
tree | 0c6250bc59b93cdc92a4df75ab77d0e88690e8b2 | |
parent | 5e2f6182f41d7c027f58b110bc8c6e539a50ac2c (diff) |
Easy Tracker: Cheap way to dramatically improve CPU usage. Now displaying FPS info on frame preview.
-rw-r--r-- | tracker-easy/preview.cpp | 5 | ||||
-rw-r--r-- | tracker-easy/preview.h | 3 | ||||
-rw-r--r-- | tracker-easy/tracker-easy.cpp | 36 | ||||
-rw-r--r-- | tracker-easy/tracker-easy.h | 10 | ||||
-rw-r--r-- | tracker-kinect-face/camera_kinect_ir.cpp | 40 | ||||
-rw-r--r-- | tracker-kinect-face/camera_kinect_ir.h | 6 |
6 files changed, 74 insertions, 26 deletions
diff --git a/tracker-easy/preview.cpp b/tracker-easy/preview.cpp index 364e45ca..4d6b7c61 100644 --- a/tracker-easy/preview.cpp +++ b/tracker-easy/preview.cpp @@ -100,6 +100,11 @@ namespace EasyTracker QImage::Format_ARGB32); } + void Preview::DrawInfo(const std::string& aString) + { + cv::putText(iFrameRgb, aString, cv::Point(4,iFrameRgb.size().height-4), cv::FONT_HERSHEY_PLAIN, 2, cv::Scalar(0, 255, 0),2); + } + void Preview::DrawCross(cv::Point aPoint) { constexpr int len = 9; diff --git a/tracker-easy/preview.h b/tracker-easy/preview.h index 979d272b..74ef89aa 100644 --- a/tracker-easy/preview.h +++ b/tracker-easy/preview.h @@ -22,10 +22,11 @@ namespace EasyTracker Preview& operator=(const cv::Mat& frame); QImage get_bitmap(); void DrawCross(cv::Point aPoint); + void DrawInfo(const std::string& aString); operator cv::Mat&() { return iFrameResized; } operator cv::Mat const&() const { return iFrameResized; } - + private: static void ensure_size(cv::Mat& frame, int w, int h, int type); diff --git a/tracker-easy/tracker-easy.cpp b/tracker-easy/tracker-easy.cpp index 8bae883a..68e3be36 100644 --- a/tracker-easy/tracker-easy.cpp +++ b/tracker-easy/tracker-easy.cpp @@ -27,8 +27,10 @@ using namespace options; // Disable debug #define dbgout if (true) {} else std::cout +//#define infout if (true) {} else std::cout // Enable debug //#define dbgout if (false) {} else std::cout +#define infout if (false) {} else std::cout namespace EasyTracker { @@ -125,10 +127,13 @@ namespace EasyTracker { maybe_reopen_camera(); + iFpsTimer.start(); + while (!isInterruptionRequested()) { - bool new_frame = false; + iTimer.start(); + bool new_frame = false; { QMutexLocker l(&camera_mtx); @@ -144,7 +149,7 @@ namespace EasyTracker // Create OpenCV matrix from our frame // TODO: Assert channel size is one or two iMatFrame = cv::Mat(iFrame.height, iFrame.width, CV_MAKETYPE((iFrame.channelSize == 2 ? CV_16U : CV_8U), iFrame.channels), iFrame.data, iFrame.stride); - + iFrameCount++; const bool preview_visible = check_is_visible(); if (preview_visible) @@ -268,7 +273,11 @@ namespace EasyTracker } if (preview_visible) - { + { + std::ostringstream ss; + ss << "FPS: " << iFps << "/" << iSkippedFps; + iPreview.DrawInfo(ss.str()); + // if (topPointIndex != -1) { @@ -302,6 +311,27 @@ namespace EasyTracker cv::destroyWindow("Preview"); } } + + dbgout << "Frame time:" << iTimer.elapsed_seconds() << "\n"; + } + else + { + iSkippedFrameCount++; + } + + // Pace ourselves, drastically reduce CPU usage + // TODO: Consider using QTimer instead of QThread + msleep(1000 / 55); + + // Compute FPS + double elapsed = iFpsTimer.elapsed_seconds(); + if (elapsed >= 1.0) + { + iFps = iFrameCount / elapsed; + iSkippedFps = iSkippedFrameCount / elapsed; + iFrameCount = 0; + iSkippedFrameCount = 0; + iFpsTimer.start(); } } } diff --git a/tracker-easy/tracker-easy.h b/tracker-easy/tracker-easy.h index 00e24811..257dd6ca 100644 --- a/tracker-easy/tracker-easy.h +++ b/tracker-easy/tracker-easy.h @@ -12,6 +12,8 @@ #include "cv/numeric.hpp" #include "video/video-widget.hpp" #include "video/camera.hpp" +#include "compat/timer.hpp" + #include "preview.h" #include "settings.h" #include "point-extractor.h" @@ -78,6 +80,14 @@ namespace EasyTracker std::atomic<bool> ever_success = false; mutable QMutex center_lock, data_lock; + // + Timer iTimer; + Timer iFpsTimer; + int iFrameCount = 0; + int iSkippedFrameCount = 0; + int iFps = 0; + int iSkippedFps = 0; + // Vertices defining the model we are tracking std::vector<cv::Point3f> iModel; // Bitmap points corresponding to model vertices diff --git a/tracker-kinect-face/camera_kinect_ir.cpp b/tracker-kinect-face/camera_kinect_ir.cpp index 77621076..e170b15a 100644 --- a/tracker-kinect-face/camera_kinect_ir.cpp +++ b/tracker-kinect-face/camera_kinect_ir.cpp @@ -96,8 +96,8 @@ std::tuple<const video::impl::frame&, bool> CameraKinectIr::get_frame() new_frame = get_frame_(iMatFrame); iFrame.data = iMatFrame.ptr(); - iFrame.width = 512; - iFrame.height = 424; + iFrame.width = iWidth; + iFrame.height = iHeight; iFrame.stride = cv::Mat::AUTO_STEP; iFrame.channels = iMatFrame.channels(); iFrame.channelSize = iMatFrame.elemSize1(); @@ -115,6 +115,9 @@ inline void SafeRelease(Interface *& pInterfaceToRelease) } } +/// +/// +/// bool CameraKinectIr::start(info& aInfo) { stop(); @@ -162,8 +165,8 @@ bool CameraKinectIr::start(info& aInfo) if (success) { // Provide frame info - aInfo.width = width; - aInfo.height = height; + aInfo.width = iWidth; + aInfo.height = iHeight; CameraIntrinsics intrinsics; hr = iCoordinateMapper->GetDepthCameraIntrinsics(&intrinsics); @@ -223,13 +226,7 @@ bool CameraKinectIr::get_frame_(cv::Mat& aFrame) if (SUCCEEDED(hr)) { - INT64 nTime = 0; - UINT nBufferSize = 0; - UINT16 *pBuffer = NULL; - - hr = iInfraredFrame->get_RelativeTime(&nTime); - - if (first_frame) + if (iFirstFrame) { IFrameDescription* frameDescription = NULL; @@ -238,28 +235,33 @@ bool CameraKinectIr::get_frame_(cv::Mat& aFrame) hr = iInfraredFrame->get_FrameDescription(&frameDescription); } - // TODO: should not request those info for a every frame really if (SUCCEEDED(hr)) { - hr = frameDescription->get_Width(&width); + hr = frameDescription->get_Width(&iWidth); } if (SUCCEEDED(hr)) { - hr = frameDescription->get_Height(&height); + hr = frameDescription->get_Height(&iHeight); } if (SUCCEEDED(hr)) { - hr = frameDescription->get_DiagonalFieldOfView(&fov); + hr = frameDescription->get_DiagonalFieldOfView(&iFov); } if (SUCCEEDED(hr)) - first_frame = false; - + { + iFirstFrame = false; + } + SafeRelease(frameDescription); } - + + + UINT nBufferSize = 0; + UINT16 *pBuffer = NULL; + if (SUCCEEDED(hr)) { hr = iInfraredFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer); @@ -268,7 +270,7 @@ bool CameraKinectIr::get_frame_(cv::Mat& aFrame) if (SUCCEEDED(hr)) { // Create an OpenCV matrix with our 16-bits IR buffer - aFrame = cv::Mat(height, width, CV_16UC1, pBuffer, cv::Mat::AUTO_STEP); + aFrame = cv::Mat(iHeight, iWidth, CV_16UC1, pBuffer, cv::Mat::AUTO_STEP); // Any processing of the frame is left to the user success = true; } diff --git a/tracker-kinect-face/camera_kinect_ir.h b/tracker-kinect-face/camera_kinect_ir.h index 7cd24651..b88efdcd 100644 --- a/tracker-kinect-face/camera_kinect_ir.h +++ b/tracker-kinect-face/camera_kinect_ir.h @@ -71,9 +71,9 @@ private: video::frame iFrame; cv::Mat iMatFrame; - float fov = 0; - int width = 0, height = 0; - bool first_frame = true; + float iFov = 0; + int iWidth = 0, iHeight = 0; + bool iFirstFrame = true; }; } |