From: Hyunsoo Park Date: Mon, 23 May 2022 09:14:32 +0000 (+0900) Subject: mv_roi_tracker: Improve API usage X-Git-Tag: submit/tizen/20220720.053259~20 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=529434bee237e7e39825939bee2eac8c752eb723;p=platform%2Fcore%2Fapi%2Fmediavision.git mv_roi_tracker: Improve API usage [Version] : 0.23.1-0 [Issue type] : code refactoring For improving api usage convenient, some are modified. - Remove coordinates parameter of 'mv_roi_tracker_create'. - Add new API 'mv_roi_tracker_set_coordinate' Change-Id: I5837e9ad524a8ba03b3b103c8eccb97ede60c4e0 Signed-off-by: Hyunsoo Park --- diff --git a/include/mv_roi_tracker.h b/include/mv_roi_tracker.h index 24762ad..057e1c9 100644 --- a/include/mv_roi_tracker.h +++ b/include/mv_roi_tracker.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __MEDIAVISION_TRACKER_H__ -#define __MEDIAVISION_TRACKER_H__ +#ifndef __MEDIAVISION_ROI_TRACKER_H__ +#define __MEDIAVISION_ROI_TRACKER_H__ #include #include @@ -32,20 +32,17 @@ extern "C" { * * @remarks The @a handle should be released using mv_roi_tracker_destroy(). * - * @param[in] x The x coordiante to set initial ROI to be tracked - * @param[in] y The y coordiante to set initial ROI to be tracked - * @param[in] width The width to set initial ROI to be tracked - * @param[in] height The height to set initial ROI to be tracked * @param[out] handle The handle to the tracker to be created * * @return @c 0 on success, otherwise a negative error value * @retval #MEDIA_VISION_ERROR_NONE Successful - * @retval #MEDIA_VISION_ERROR_OUT_OF_MEMORY Out of memory + * @retval #MEDIA_VISION_ERROR_NOT_SUPPORTED Not supported * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #MEDIA_VISION_ERROR_OUT_OF_MEMORY Out of memory * * @see mv_roi_tracker_destroy() */ -int mv_roi_tracker_create(int x, int y, int width, int height, mv_roi_tracker_h *handle); +int mv_roi_tracker_create(mv_roi_tracker_h *handle); /** * @brief Destroy tracker handle and release all its resources. @@ -65,21 +62,42 @@ int mv_roi_tracker_create(int x, int y, int width, int height, mv_roi_tracker_h int mv_roi_tracker_destroy(mv_roi_tracker_h handle); /** - * @brief Inference with a given tracker on the @a source + * @brief Sets initial roi coordinates to roi tracker object handle. + * @details Use this function to set the coordinates to the tracker object handle(for the tracking). + * + * @since_tizen 7.0 + * + * @param[in] handle The tracker handle to set coordinates. + * @param[in] x The x coordiante to set initial ROI to be tracked + * @param[in] y The y coordiante to set initial ROI to be tracked + * @param[in] width The width to set initial ROI to be tracked + * @param[in] height The height to set initial ROI to be tracked + * + * @return @c 0 on success, otherwise a negative error value + * @retval #MEDIA_VISION_ERROR_NONE Successful + * @retval #MEDIA_VISION_ERROR_NOT_SUPPORTED Not supported + * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter + * + * @see mv_roi_tracker_create() + */ +int mv_roi_tracker_set_coordinate(mv_roi_tracker_h handle, int x, int y, int width, int height); + +/** + * @brief Inference with a given tracker on the @a source. * @details Use this function to inference with a given source. * This function returns a proper label string to a given source. * * @since_tizen 7.0 * - * @param [in] handle The handle to the tracker object. - * @param [in] source The handle to the source of the media. - * @param [out] out_result The structure which is filled with inference result, + * @param[in] handle The handle to the tracker object. + * @param[in] source The handle to the source of the media. + * @param[out] result The structure which is filled with inference result, * * @return @c 0 on success, otherwise a negative error value * @retval #MEDIA_VISION_ERROR_NONE Successful * @retval #MEDIA_VISION_ERROR_NOT_SUPPORTED Not supported - * @retval #MEDIA_VISION_ERROR_OUT_OF_MEMORY Out of memory * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #MEDIA_VISION_ERROR_OUT_OF_MEMORY Out of memory * @retval #MEDIA_VISION_ERROR_INVALID_OPERATION Invalid operation * @retval #MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT Source colorspace * isn't supported @@ -93,4 +111,4 @@ int mv_roi_tracker_perform(mv_roi_tracker_h handle, mv_source_h source, } #endif /* __cplusplus */ -#endif /* __MEDIAVISION_TRACKER_H__ */ \ No newline at end of file +#endif /* __MEDIAVISION_ROI_TRACKER_H__ */ \ No newline at end of file diff --git a/include/mv_roi_tracker_type.h b/include/mv_roi_tracker_type.h index 108d94b..34ada31 100644 --- a/include/mv_roi_tracker_type.h +++ b/include/mv_roi_tracker_type.h @@ -37,7 +37,6 @@ typedef struct { int y; int width; int height; - bool is_found; } mv_roi_tracker_result_s; /** diff --git a/mv_roi_tracker/roi_tracker/include/ROITracker.h b/mv_roi_tracker/roi_tracker/include/ROITracker.h index 3c9eee6..3826dae 100644 --- a/mv_roi_tracker/roi_tracker/include/ROITracker.h +++ b/mv_roi_tracker/roi_tracker/include/ROITracker.h @@ -18,24 +18,28 @@ #define __ROI_TRACKER_H__ #include "inference_engine_common_impl.h" +#include "mv_roi_tracker_type.h" #include #include #include #include +namespace MediaVision { +namespace ROITracker { class ROITracker { public: - ROITracker(int x, int y, int width, int height); + ROITracker(); ~ ROITracker() = default; + mv_roi_tracker_result_s Initialize(cv::Mat &frame); + mv_roi_tracker_result_s Update(cv::Mat &frame); cv::Ptr cvTracker; cv::Rect boundingBox; - int x; - int y; - int width; - int height; bool initialized; }; -#endif \ No newline at end of file +} /* ROITracker */ +} /* MediaVision */ + +#endif /* __ROI_TRACKER_H__ */ \ No newline at end of file diff --git a/mv_roi_tracker/roi_tracker/include/ROITrackerUtil.h b/mv_roi_tracker/roi_tracker/include/ROITrackerUtil.h new file mode 100644 index 0000000..f7d821a --- /dev/null +++ b/mv_roi_tracker/roi_tracker/include/ROITrackerUtil.h @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ROITracker.h" +#include "mv_roi_tracker.h" + +#ifndef __MEDIA_VISION_ROITRACKERUTIL_H__ +#define __MEDIA_VISION_ROITRACKERUTIL_H__ + +namespace MediaVision { +namespace ROITracker { + +/* TO-DO: Make util funtion for tracker */ + +} /* ROITracker */ +} /* MediaVision */ +#endif /* __MEDIA_VISION_ROITRACKERUTIL_H__ */ \ No newline at end of file diff --git a/mv_roi_tracker/roi_tracker/include/mv_roi_tracker_open.h b/mv_roi_tracker/roi_tracker/include/mv_roi_tracker_open.h index e36aa2a..2826976 100644 --- a/mv_roi_tracker/roi_tracker/include/mv_roi_tracker_open.h +++ b/mv_roi_tracker/roi_tracker/include/mv_roi_tracker_open.h @@ -31,15 +31,12 @@ extern "C" * @brief Create tracker object handle. * @details Use this function to create an tracker object handle. * After creation the handle has to be performed with + * @ref mv_roi_tracker_set_coordinate_open() function and * @ref mv_roi_tracker_perform_open() function to perform * an tracker object. * * @since_tizen 7.0 * - * @param[in] x The x coordiante to set initial ROI to be tracked - * @param[in] y The y coordiante to set initial ROI to be tracked - * @param[in] width The width to set initial ROI to be tracked - * @param[in] height The height to set initial ROI to be tracked * @param [out] handle The handle to the tracker object to be created * @return @c 0 on success, otherwise a negative error value * @retval #MEDIA_VISION_ERROR_NONE Successful @@ -52,7 +49,7 @@ extern "C" * * @see mv_roi_tracker_destroy_open() */ -int mv_roi_tracker_create_open(int x, int y, int width, int height, mv_roi_tracker_h *out_handle); +int mv_roi_tracker_create_open(mv_roi_tracker_h *handle); /** * @brief Destroy tracker handle and releases all its resources. @@ -72,15 +69,38 @@ int mv_roi_tracker_create_open(int x, int y, int width, int height, mv_roi_track int mv_roi_tracker_destroy_open(mv_roi_tracker_h handle); /** - * @brief Track with a given roi on the @a source + * @brief Set initial roi coordinates to roi tracker object handle. + * @details Use this function to set coordinates want to track to an tracker object handle. + * After setting coordinates, handle has to be performed with + * @ref mv_roi_tracker_perform_open() function to perform + * an tracker object. + * + * @since_tizen 7.0 + * + * @param [in] handle The handle to the tracker object to be created + * @param [in] x The x coordiante to set initial ROI to be tracked + * @param [in] y The y coordiante to set initial ROI to be tracked + * @param [in] width The width to set initial ROI to be tracked + * @param [in] height The height to set initial ROI to be tracked + * @return @c 0 on success, otherwise a negative error value + * @retval #MEDIA_VISION_ERROR_NONE Successful + * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter + * @pre Create an tracker handle by calling @ref mv_roi_tracker_create_open() + * + * @see mv_roi_tracker_create_open() + */ +int mv_roi_tracker_set_coordinate_open(mv_roi_tracker_h handle, int x, int y, int width, int height); + +/** + * @brief Track with a given roi on the @a source. * @details Use this function to track with a given source. * This function returns an proper roi to a given out_result. * * @since_tizen 7.0 * - * @param [in] handle The handle to the tracker object. - * @param [in] source The handle to the source of the media. - * @param [out] out_result The structure which is filled with track result. + * @param [in] handle The handle to the tracker object. + * @param [in] source The handle to the source of the media. + * @param [out] result The structure which is filled with track result. * * @return @c 0 on success, otherwise a negative error value * @retval #MEDIA_VISION_ERROR_NONE Successful diff --git a/mv_roi_tracker/roi_tracker/src/ROITracker.cpp b/mv_roi_tracker/roi_tracker/src/ROITracker.cpp index 2fcb987..cb002e4 100644 --- a/mv_roi_tracker/roi_tracker/src/ROITracker.cpp +++ b/mv_roi_tracker/roi_tracker/src/ROITracker.cpp @@ -31,12 +31,65 @@ #include #include #include + #include "ROITracker.h" using namespace std; -ROITracker::ROITracker(int x, int y, int width, int height) - : x(x), y(y), width(width), height(height), initialized(false) +namespace MediaVision { +namespace ROITracker { +ROITracker::ROITracker() + : initialized(false) { cvTracker = cv::TrackerKCF::create(); -} \ No newline at end of file +} + +mv_roi_tracker_result_s 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); + + cvTracker->init(frame, boundingBox); + initialized = true; + + LOGI("LEAVE"); + return mv_roi_tracker_result_s { + boundingBox.x, + boundingBox.y, + boundingBox.width, + boundingBox.height + }; +} + +mv_roi_tracker_result_s ROITracker::Update(cv::Mat &frame) +{ + LOGI("ENTER"); + + //updates the tracker + if (cvTracker->update(frame, boundingBox)) { + LOGD(" Updated: x: %d, y: %d, w: %d, h: %d", + boundingBox.x, + boundingBox.y, + boundingBox.width, + boundingBox.height); + } else { + LOGE("update failed."); + throw std::runtime_error("tracker update failed."); + } + + LOGI("LEAVE"); + return mv_roi_tracker_result_s { + boundingBox.x, + boundingBox.y, + boundingBox.width, + boundingBox.height + }; +} +} /* ROITracker */ +} /* MediaVision */ \ No newline at end of file diff --git a/mv_roi_tracker/roi_tracker/src/ROITrackerUtil.cpp b/mv_roi_tracker/roi_tracker/src/ROITrackerUtil.cpp new file mode 100644 index 0000000..d264386 --- /dev/null +++ b/mv_roi_tracker/roi_tracker/src/ROITrackerUtil.cpp @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ROITrackerUtil.h" + +namespace MediaVision { +namespace ROITracker { + /* TO-DO: Make util funtion for tracker */ + +} /* ROITracker */ +} /* MediaVision */ \ No newline at end of file diff --git a/mv_roi_tracker/roi_tracker/src/mv_roi_tracker.c b/mv_roi_tracker/roi_tracker/src/mv_roi_tracker.c index 1fd6fb6..60120af 100644 --- a/mv_roi_tracker/roi_tracker/src/mv_roi_tracker.c +++ b/mv_roi_tracker/roi_tracker/src/mv_roi_tracker.c @@ -18,16 +18,16 @@ #include "mv_roi_tracker.h" #include "mv_roi_tracker_open.h" -int mv_roi_tracker_create(int x, int y, int width, int height, mv_roi_tracker_h *out_handle) +int mv_roi_tracker_create(mv_roi_tracker_h *handle) { MEDIA_VISION_SUPPORT_CHECK( __mv_inference_face_check_system_info_feature_supported()); - MEDIA_VISION_NULL_ARG_CHECK(out_handle); + MEDIA_VISION_NULL_ARG_CHECK(handle); MEDIA_VISION_FUNCTION_ENTER(); - int ret = mv_roi_tracker_create_open(x, y, width, height, out_handle); + int ret = mv_roi_tracker_create_open(handle); MEDIA_VISION_FUNCTION_LEAVE(); @@ -50,6 +50,22 @@ int mv_roi_tracker_destroy(mv_roi_tracker_h handle) return ret; } +int mv_roi_tracker_set_coordinate(mv_roi_tracker_h handle, int x, int y, int width, int height) +{ + MEDIA_VISION_SUPPORT_CHECK( + __mv_inference_face_check_system_info_feature_supported()); + + MEDIA_VISION_INSTANCE_CHECK(handle); + + MEDIA_VISION_FUNCTION_ENTER(); + + int ret = mv_roi_tracker_set_coordinate_open(handle, x, y, width, height); + + MEDIA_VISION_FUNCTION_LEAVE(); + + return ret; +} + int mv_roi_tracker_perform(mv_roi_tracker_h handle, mv_source_h source, mv_roi_tracker_result_s *result) { @@ -58,7 +74,7 @@ int mv_roi_tracker_perform(mv_roi_tracker_h handle, mv_source_h source, MEDIA_VISION_INSTANCE_CHECK(handle); MEDIA_VISION_INSTANCE_CHECK(source); - MEDIA_VISION_INSTANCE_CHECK(result); + MEDIA_VISION_NULL_ARG_CHECK(result); MEDIA_VISION_FUNCTION_ENTER(); diff --git a/mv_roi_tracker/roi_tracker/src/mv_roi_tracker_open.cpp b/mv_roi_tracker/roi_tracker/src/mv_roi_tracker_open.cpp index c620b00..18b4801 100644 --- a/mv_roi_tracker/roi_tracker/src/mv_roi_tracker_open.cpp +++ b/mv_roi_tracker/roi_tracker/src/mv_roi_tracker_open.cpp @@ -15,24 +15,50 @@ */ #include -#include #include "ROITracker.h" +#include "ROITrackerUtil.h" #include "mv_roi_tracker_open.h" using namespace std; +using namespace MediaVision::ROITracker; -int mv_roi_tracker_create_open(int x, int y, int width, int height, mv_roi_tracker_h *handle) +static cv::Mat getTrackerFrame(mv_colorspace_e colorspace, unsigned int channels, unsigned int width, unsigned int height, unsigned char* buffer) +{ + switch (colorspace) { + case MEDIA_VISION_COLORSPACE_Y800: + return cv::Mat(width, height, CV_8UC1, buffer).clone(); + case MEDIA_VISION_COLORSPACE_I420: + case MEDIA_VISION_COLORSPACE_NV12: + case MEDIA_VISION_COLORSPACE_NV21: + case MEDIA_VISION_COLORSPACE_YV12: + return cv::Mat(width, height * 3/2, CV_8UC1, buffer).clone(); + case MEDIA_VISION_COLORSPACE_YUYV: + case MEDIA_VISION_COLORSPACE_UYVY: + case MEDIA_VISION_COLORSPACE_422P: + case MEDIA_VISION_COLORSPACE_RGB565: + return cv::Mat(height, width, CV_8UC2, buffer).clone(); + case MEDIA_VISION_COLORSPACE_RGB888: + return cv::Mat(height, width, CV_8UC3, buffer).clone(); + case MEDIA_VISION_COLORSPACE_RGBA: + return cv::Mat(height, width, CV_8UC4, buffer).clone(); + default: + LOGE("Error: mv_source has unsupported colorspace."); + throw std::invalid_argument("not support format."); + } +} + +int mv_roi_tracker_create_open(mv_roi_tracker_h *handle) { LOGD("ENTER"); //instantiates the specific Tracker - ROITracker *pTracker = new (std::nothrow) ROITracker { x, y, width, height }; + ROITracker *pTracker = new (std::nothrow)ROITracker; if (!pTracker) { LOGE("Failed to create tracker"); return MEDIA_VISION_ERROR_OUT_OF_MEMORY; } - LOGD("Init pos : x:%d, y:%d, w:%d, h:%d", x, y, width, height); + LOGD("Tracker handle [%p] has been created", pTracker); (*handle) = static_cast(pTracker); @@ -57,6 +83,28 @@ int mv_roi_tracker_destroy_open(mv_roi_tracker_h handle) return MEDIA_VISION_ERROR_NONE; } +int mv_roi_tracker_set_coordinate_open(mv_roi_tracker_h handle, int x, int y, int width, int height) +{ + LOGD("ENTER"); + + if (!handle) { + LOGE("Handle is NULL."); + return MEDIA_VISION_ERROR_INVALID_PARAMETER; + } + + auto pTracker = static_cast(handle); + + pTracker->boundingBox = { x, y, width, height }; + LOGD("Init pos : x:%d, y:%d, w:%d, h:%d is set.", + pTracker->boundingBox.x, + pTracker->boundingBox.y, + pTracker->boundingBox.width, + pTracker->boundingBox.height); + + LOGD("LEAVE"); + return MEDIA_VISION_ERROR_NONE; +} + int mv_roi_tracker_perform_open(mv_roi_tracker_h handle, mv_source_h source, mv_roi_tracker_result_s *result) { @@ -68,9 +116,6 @@ int mv_roi_tracker_perform_open(mv_roi_tracker_h handle, mv_source_h source, ROITracker *pTracker = static_cast(handle); - cv::Mat frame; - cv::Mat image; - int depth = CV_8U; /* Default depth. 1 byte for channel. */ unsigned int channels = 0; unsigned int width = 0, height = 0; unsigned int bufferSize = 0; @@ -86,86 +131,20 @@ int mv_roi_tracker_perform_open(mv_roi_tracker_h handle, mv_source_h source, MEDIA_VISION_ASSERT(mv_source_get_buffer(source, &buffer, &bufferSize), "Failed to get the buffer size."); - switch(colorspace) { - case MEDIA_VISION_COLORSPACE_Y800: - channels = 1; - /* Without conversion */ - break; - case MEDIA_VISION_COLORSPACE_I420: - channels = 1; - height *= 1.5; - break; - case MEDIA_VISION_COLORSPACE_NV12: - channels = 1; - height *= 1.5; - break; - case MEDIA_VISION_COLORSPACE_YV12: - channels = 1; - height *= 1.5; - break; - case MEDIA_VISION_COLORSPACE_NV21: - channels = 1; - height *= 1.5; - break; - case MEDIA_VISION_COLORSPACE_YUYV: - channels = 2; - break; - case MEDIA_VISION_COLORSPACE_UYVY: - channels = 2; - break; - case MEDIA_VISION_COLORSPACE_422P: - channels = 2; - break; - case MEDIA_VISION_COLORSPACE_RGB565: - channels = 2; - break; - case MEDIA_VISION_COLORSPACE_RGB888: - channels = 3; - break; - case MEDIA_VISION_COLORSPACE_RGBA: - channels = 4; - break; - default: - LOGE("Error: mv_source has unsupported colorspace."); - return MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT; - } + LOGD(" w: %d, h: %d, c: %d", width, height, channels); + try { + cv::Mat frame = getTrackerFrame(colorspace, channels, width, height, buffer); - LOGD(" w: %d, h: %d, d: %d, c: %d", width, height, depth, channels); - - frame = cv::Mat(cv::Size(width, height), - CV_MAKETYPE(depth, channels), buffer).clone(); - if (!pTracker->initialized) { - //initializes the tracker - pTracker->boundingBox.x = pTracker->x; - pTracker->boundingBox.y = pTracker->y; - pTracker->boundingBox.width = pTracker->width; - pTracker->boundingBox.height = pTracker->height; - - pTracker->cvTracker->init(frame, pTracker->boundingBox); - pTracker->initialized = true; - result->x = pTracker->boundingBox.x; - result->y = pTracker->boundingBox.y ; - result->width = pTracker->boundingBox.width; - result->height = pTracker->boundingBox.height; - result->is_found = true; - } else if (pTracker->initialized) { - //updates the tracker - if (pTracker->cvTracker->update(frame, pTracker->boundingBox)) { - result->x = pTracker->boundingBox.x; - result->y = pTracker->boundingBox.y ; - result->width = pTracker->boundingBox.width; - result->height = pTracker->boundingBox.height; - result->is_found = true; - LOGD(" Updated: x: %d, y: %d, w: %d, h: %d", pTracker->boundingBox.x, pTracker->boundingBox.y, pTracker->boundingBox.width, pTracker->boundingBox.height); - - } else { - result->is_found = false; - LOGE("update failed."); - return MEDIA_VISION_ERROR_INVALID_OPERATION; + if (!pTracker->initialized) { + *result = pTracker->Initialize(frame); + return MEDIA_VISION_ERROR_NONE; } - } + *result = pTracker->Update(frame); + } catch (const std::exception &e) { + LOGE("Failed : %s", e.what()); + return MEDIA_VISION_ERROR_INVALID_OPERATION; + } LOGD("LEAVE"); - return MEDIA_VISION_ERROR_NONE; -} +} \ No newline at end of file diff --git a/packaging/capi-media-vision.spec b/packaging/capi-media-vision.spec index a772b9d..a8807b8 100644 --- a/packaging/capi-media-vision.spec +++ b/packaging/capi-media-vision.spec @@ -1,6 +1,6 @@ Name: capi-media-vision Summary: Media Vision library for Tizen Native API -Version: 0.23.2 +Version: 0.23.3 Release: 0 Group: Multimedia/Framework License: Apache-2.0 and BSD-3-Clause