mv_roi_tracker: more refactoring
authorSeungbae Shin <seungbae.shin@samsung.com>
Wed, 13 Jul 2022 09:29:56 +0000 (18:29 +0900)
committerInki Dae <inki.dae@samsung.com>
Wed, 20 Jul 2022 05:16:57 +0000 (14:16 +0900)
[Issue Type] Code Refactorying

- merge 'result' with 'boundingBox'
- rename member variables to start with __ prefix
- remove unnecessary initialized() method
- take mv_roi_tracker_type_e instead of mv_engine_config_h for the setting type
- return result by tuple instead

Change-Id: Id537086cbab2e56c474cae7343c40e6cf3af6e05

mv_roi_tracker/roi_tracker/include/ROITracker.h
mv_roi_tracker/roi_tracker/src/ROITracker.cpp
mv_roi_tracker/roi_tracker/src/mv_roi_tracker_open.cpp
packaging/capi-media-vision.spec

index 844c37c..3b6d6fd 100644 (file)
 
 namespace MediaVision {
 namespace ROITracker {
+
+using TrackerResult = std::tuple<int, int, int, int>;
+
 class ROITracker {
 public:
-       ROITracker();
-       ROITracker() = default;
-
-       void perform(cv::Mat frame);
-       void getResult(int *x, int *y, int *width, int *height);
-       bool initialized();
-       int setConfiguration(mv_engine_config_h engine_config);
-       int setRoi(int x, int y, int width, int height);
+       ROITracker() = default;
+       virtual ~ROITracker() = default;
+
+       void setType(mv_roi_tracker_type_e type) noexcept;
+       void setRoi(int x, int y, int width, int height) noexcept;
+       void perform(cv::Mat& frame);
+       TrackerResult result();
+
 private:
-       void initialize(cv::Mat &frame);
-       void update(cv::Mat &frame);
+       void initialize(cv::Matframe);
+       void update(cv::Matframe);
 
-       cv::Ptr<cv::Tracker> cvTracker;
-       cv::Rect boundingBox;
-       mv_roi_tracker_type_e type;
-       mv_roi_tracker_result_s result;
+       cv::Ptr<cv::Tracker> __cvTracker;
+       cv::Rect __boundingBox;
+       mv_roi_tracker_type_e __type { MV_ROI_TRACKER_TYPE_BALANCE };
+       bool __initialized { false };
 };
 
 } /* ROITracker */
index 86c3c2b..c473865 100644 (file)
 #include "ROITracker.h"
 #include "ROITrackerUtil.h"
 
-using namespace std;
-
 namespace MediaVision {
 namespace ROITracker {
 
-
-ROITracker::ROITracker()
-       : type(MV_ROI_TRACKER_TYPE_BALANCE)
-{
-       result = (mv_roi_tracker_result_s) {
-               .x = -1,
-               .y = -1,
-               .width = -1,
-               .height = -1,
-               .initialized = false
-       };
-}
-
-void ROITracker::initialize(cv::Mat &frame)
+void ROITracker::initialize(cv::Mat& frame)
 {
-       LOGI("ENTER");
-
-       //initializes the tracker
-       LOGD("Init pos : x:%d, y:%d, w:%d, h:%d is set.",
-               boundingBox.x,
-               boundingBox.y,
-               boundingBox.width,
-               boundingBox.height);
-
-       if (cvTracker) {
+       if (__cvTracker) {
                LOGE("cvTracker already exists. 'mv_roi_tracker_destroy' should be called for removing cvTracker.");
                throw std::runtime_error("tracker Initialize failed.");
        }
 
-       cvTracker = createTrackerByName(type);
-       cvTracker->init(frame, boundingBox);
-
-       result = (mv_roi_tracker_result_s) {
-               .x = boundingBox.x,
-               .y = boundingBox.y,
-               .width = boundingBox.width,
-               .height = boundingBox.height,
-               .initialized = true
-       };
-       LOGI("LEAVE");
+       LOGD("Init pos : x:%d, y:%d, w:%d, h:%d is set.",
+               __boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
+
+       __cvTracker = createTrackerByName(__type);
+       __cvTracker->init(frame, __boundingBox);
+
+       __initialized = true;
+
+       LOGI("Initialized done");
 }
 
-void ROITracker::update(cv::Mat &frame)
+void ROITracker::update(cv::Matframe)
 {
-       LOGI("ENTER");
-
-       //updates the tracker
-       if (cvTracker->update(frame, boundingBox)) {
-               result = (mv_roi_tracker_result_s) {
-                       .x = boundingBox.x,
-                       .y = boundingBox.y,
-                       .width = boundingBox.width,
-                       .height = boundingBox.height,
-                       .initialized = true
-               };
-               LOGD(" Updated: x: %d, y: %d, w: %d, h: %d",
-                       result.x,
-                       result.y,
-                       result.width,
-                       result.height);
-
-       } else {
+       if (!__cvTracker->update(frame, __boundingBox)) {
                LOGE("update failed.");
-               result.initialized = false;
+               __initialized = false;
                throw std::runtime_error("tracker update failed.");
        }
 
-       LOGI("LEAVE");
+       LOGD(" Updated: x: %d, y: %d, w: %d, h: %d",
+               __boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
 }
 
-void ROITracker::perform(cv::Mat frame)
+void ROITracker::perform(cv::Mat& frame)
 {
-       if (!result.initialized)
+       if (!__initialized)
                initialize(frame);
        else
                update(frame);
 }
 
-void ROITracker::getResult(int *x, int *y, int *width, int *height)
+TrackerResult ROITracker::result()
 {
-       if (!result.initialized)
-               throw std::runtime_error("tracker getResult failed.");
+       if (!__initialized)
+               throw std::runtime_error("not initialized yet!!!");
 
-       *x = result.x;
-       *y = result.y;
-       *width = result.width;
-       *height = result.height;
+       return std::make_tuple(__boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
 }
 
-int ROITracker::setConfiguration(mv_engine_config_h engine_config)
+void ROITracker::setType(mv_roi_tracker_type_e type) noexcept
 {
-       int tracker_type = 0;
-       int ret = mv_engine_config_get_int_attribute(
-                       engine_config, MV_ROI_TRACKER_TYPE, &tracker_type);
-       if (ret != MEDIA_VISION_ERROR_NONE) {
-               LOGE("Fail to get roi tracker type");
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-       }
-
-       type = static_cast<mv_roi_tracker_type_e>(tracker_type);
-       LOGD("tracker_type: [%d] is set.", type);
+       __type = type;
 
-       return MEDIA_VISION_ERROR_NONE;
+       LOGD("tracker_type: [%d] is set.", static_cast<int>(__type));
 }
 
-int ROITracker::setRoi(int x, int y, int width, int height)
+void ROITracker::setRoi(int x, int y, int width, int height) noexcept
 {
-       boundingBox = { x, y, width, height };
+       __boundingBox = { x, y, width, height };
 
        LOGD("ROI : x:%d, y:%d, w:%d, h:%d is set.",
-                       boundingBox.x,
-                       boundingBox.y,
-                       boundingBox.width,
-                       boundingBox.height);
-
-       LOGD("LEAVE");
-
-       return MEDIA_VISION_ERROR_NONE;
+                       __boundingBox.x, __boundingBox.y, __boundingBox.width, __boundingBox.height);
 }
 
-bool ROITracker::initialized()
-{
-       return result.initialized;
-}
 } /* ROITracker */
 } /* MediaVision */
index 7cecb8d..1f198f5 100644 (file)
@@ -52,7 +52,6 @@ int mv_roi_tracker_create_open(mv_roi_tracker_h *handle)
 {
        LOGD("ENTER");
 
-       //instantiates the specific Tracker
        ROITracker *pTracker = new (std::nothrow)ROITracker;
        if (!pTracker) {
                LOGE("Failed to create tracker");
@@ -70,6 +69,7 @@ int mv_roi_tracker_create_open(mv_roi_tracker_h *handle)
 int mv_roi_tracker_destroy_open(mv_roi_tracker_h handle)
 {
        LOGD("ENTER");
+
        if (!handle) {
                LOGE("Handle can't be destroyed because handle is NULL");
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
@@ -92,15 +92,15 @@ int mv_roi_tracker_configure_engine_open(mv_roi_tracker_h handle,
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
 
+       int tracker_type;
+       if (mv_engine_config_get_int_attribute(engine_config,
+                       MV_ROI_TRACKER_TYPE, &tracker_type) != MEDIA_VISION_ERROR_NONE)
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+
        auto pTracker = static_cast<ROITracker *>(handle);
+       pTracker->setType(static_cast<mv_roi_tracker_type_e>(tracker_type));
 
-       int ret = pTracker->setConfiguration(engine_config);
-       if (ret != MEDIA_VISION_ERROR_NONE) {
-               LOGE("Fail to set configuration");
-               return ret;
-       }
        LOGI("LEAVE");
-
        return MEDIA_VISION_ERROR_NONE;
 }
 
@@ -114,27 +114,21 @@ int mv_roi_tracker_prepare_open(mv_roi_tracker_h handle, int x, int y, int width
        }
 
        auto pTracker = static_cast<ROITracker *>(handle);
-       int ret = pTracker->setRoi(x, y, width, height);
-       if (ret != MEDIA_VISION_ERROR_NONE) {
-               LOGE("Fail to set roi");
-               return ret;
-       }
+       pTracker->setRoi(x, y, width, height);
 
        LOGI("LEAVE");
-
        return MEDIA_VISION_ERROR_NONE;
 }
 
 int mv_roi_tracker_perform_open(mv_roi_tracker_h handle, mv_source_h source, mv_roi_tracker_tracked_cb tracked_cb, void *user_data)
 {
        LOGD("ENTER");
+
        if (!handle) {
                LOGE("Handle is NULL.");
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
 
-       auto pTracker = static_cast<ROITracker *>(handle);
-
        unsigned int channels = 0;
        unsigned int width = 0, height = 0;
        unsigned int bufferSize = 0;
@@ -152,17 +146,19 @@ int mv_roi_tracker_perform_open(mv_roi_tracker_h handle, mv_source_h source, mv_
 
        LOGD(" w: %d, h: %d, c: %d", width, height, channels);
        try {
-               pTracker->perform(getTrackerFrame(colorspace, channels, width, height, buffer));
+               cv::Mat frame = getTrackerFrame(colorspace, channels, width, height, buffer);
+
+               auto pTracker = static_cast<ROITracker *>(handle);
+               pTracker->perform(frame);
+
+               mv_rectangle_s roi;
+               std::tie(roi.point.x, roi.point.y, roi.width, roi.height) = pTracker->result();
+               tracked_cb(source, roi, user_data);
        } catch (const std::exception &e) {
                LOGE("Failed : %s", e.what());
                return MEDIA_VISION_ERROR_INVALID_OPERATION;
        }
 
-       mv_rectangle_s roi;
-       pTracker->getResult(&roi.point.x, &roi.point.y, &roi.width, &roi.height);
-
-       tracked_cb(source, roi, user_data);
-
        LOGD("LEAVE");
        return MEDIA_VISION_ERROR_NONE;
 }
index a8807b8..e597cbf 100644 (file)
@@ -1,6 +1,6 @@
 Name:        capi-media-vision
 Summary:     Media Vision library for Tizen Native API
-Version:     0.23.3
+Version:     0.23.4
 Release:     0
 Group:       Multimedia/Framework
 License:     Apache-2.0 and BSD-3-Clause