change service configuration schema
authorInki Dae <inki.dae@samsung.com>
Thu, 21 Mar 2024 07:01:03 +0000 (16:01 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 26 Mar 2024 03:43:33 +0000 (12:43 +0900)
Change service configuration schema like below.

Before
"..., input=camera, camera_backend=opencv, ..."

After
"..., input_feed=camera, camera_id=n, ..."

Signed-off-by: Inki Dae <inki.dae@samsung.com>
12 files changed:
capi/singleo_native_capi_internal.h
input/backends/opencv/include/OpencvBackend.h
input/backends/opencv/src/OpencvBackend.cpp
input/include/InputTypes.h
input/src/CameraServiceDefault.cpp
input/src/InputCamera.cpp
packaging/singleo.spec
services/auto_zoom/src/AutoZoom.cpp
services/common/include/ServiceConfigParser.h
services/common/src/ServiceConfigParser.cpp
test/services/test_autozoom.cpp
test/services/test_autozoom_on_screen.cpp

index 8439d26334821480c7dc7e1bf088868543a72ab2..a3c350ce69a17759452ad6d7fd8528d69e763a4d 100644 (file)
@@ -29,7 +29,11 @@ typedef void (*singleo_user_cb_t)(unsigned char *buffer, unsigned int width, uns
  * @remarks With this function, user can receive result mixed with input data by callback.
  *          This API is valid only in case of using asynchronous mode.
  *
- * @param[in] handle   The handle to the service.
+ * @param[in] handle       The handle to the service.
+ * @param[in] callback_ptr A user callback pointer to be called by the service framework internally
+ *                         after the completion of the service request. User can get the input image data
+ *                         mixed with result such as bounding box - it depedens on service implemetation.
+ *                         This API is an internal API for the testing and debugging.
  *
  * @return 0 on success, otherwise a negative error value
  * @retval #SINGLEO_SERVICE_ERROR_NONE Successful
index 9d85faa9485872410bff339c263a3a440e8b5013..10d8520bb6e494c484c2ae2cf46070e5504849f9 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <thread>
 #include <memory>
+#include <unordered_set>
 #include <opencv2/opencv.hpp>
 
 #include "ICameraBackend.h"
@@ -38,12 +39,15 @@ private:
        void *_userData;
        std::unique_ptr<std::thread> _thread_handle;
        bool _exit_thread { false };
+       std::unordered_set<unsigned int> _camera_ids;
+       unsigned int _active_camera_id {};
 
-       int findFirstAvailableCamera();
+       void updateAvailableCameraDevices();
+       void setActivateCameraDevice(unsigned int id);
        void threadLoop();
 
 public:
-       OpencvBackend();
+       explicit OpencvBackend(const CameraConfig &config);
        virtual ~OpencvBackend();
 
        void setUserCb(const InputServiceCallbackType &userCb, void *user_data) override;
index e5ff654af7c4a40ef60f1cfc0eae195c4774de2f..bdc4687c518945c41fcf1dc30fa5414fef15a637 100644 (file)
@@ -25,22 +25,10 @@ namespace singleo
 {
 namespace input
 {
-OpencvBackend::OpencvBackend()
+OpencvBackend::OpencvBackend(const CameraConfig &config)
 {
-       // TODO. There are some cases that one camera device has one more camera ID.
-       //       For these cases, I will introduce camera specific meta file approach later.
-       //       In this case, camera IDs will be parsed from the given camera specific meta file.
-       int cameraId = findFirstAvailableCamera();
-       if (cameraId == -1)
-               throw InvalidOperation("No available camera device.");
-
-       SINGLEO_LOGD("Camera ID is %d", cameraId);
-
-       _video_capture = make_unique<cv::VideoCapture>(cameraId);
-       if (!_video_capture->isOpened()) {
-               SINGLEO_LOGE("Failed to open WebCam device.");
-               throw InvalidOperation("Failed to open WebCap device.");
-       }
+       updateAvailableCameraDevices();
+       setActivateCameraDevice(config.device_id);
 }
 
 OpencvBackend::~OpencvBackend()
@@ -48,19 +36,20 @@ OpencvBackend::~OpencvBackend()
        _video_capture->release();
 }
 
-int OpencvBackend::findFirstAvailableCamera()
+void OpencvBackend::updateAvailableCameraDevices()
 {
        const unsigned int maxCameraCnt = 10;
 
        for (unsigned int idx = 0; idx < maxCameraCnt; ++idx) {
                cv::VideoCapture testCap(idx);
                if (testCap.isOpened()) {
+                       // Update valid camera device ids.
+                       _camera_ids.insert(idx);
                        testCap.release();
-                       return idx;
+
+                       SINGLEO_LOGD("camera device id %d is valid.", idx);
                }
        }
-
-       return -1;
 }
 
 void OpencvBackend::setUserCb(const std::function<void(ISingleoCommonData &data, void *user_data)> &userCb,
@@ -72,8 +61,24 @@ void OpencvBackend::setUserCb(const std::function<void(ISingleoCommonData &data,
        SINGLEO_LOGD("OpencvBackend: user callback has been registered.");
 }
 
+void OpencvBackend::setActivateCameraDevice(unsigned int id)
+{
+       if (_camera_ids.count(id) == 0)
+               throw InvalidOperation("A given camera device id(%d) is not supported.", id);
+
+       _active_camera_id = id;
+       SINGLEO_LOGD("Camera device id(%d) has been activated.", id);
+       return;
+}
+
 void OpencvBackend::configure()
-{}
+{
+       _video_capture = make_unique<cv::VideoCapture>(_active_camera_id);
+       if (!_video_capture->isOpened()) {
+               SINGLEO_LOGE("Failed to open camera device(%d).", _active_camera_id);
+               throw InvalidOperation("Failed to open WebCap device.");
+       }
+}
 
 void OpencvBackend::capture(SingleoImageData &out_data)
 {
index 2dcabb345584d743cbb9fe94644ff4491473978b..db42f692bb2474dc6732048c16210c26d5dca579 100644 (file)
@@ -40,8 +40,10 @@ struct CameraConfig : public InputConfigBase {
        {}
 
        CameraBackendType backend_type { CameraBackendType::NONE };
+       unsigned int device_id {};
        unsigned int fps {};
-       bool async { false };
+       unsigned int x_resolution {};
+       unsigned int y_resolution {};
 };
 
 struct ScreenCaptureConfig : public InputConfigBase {
@@ -49,7 +51,8 @@ struct ScreenCaptureConfig : public InputConfigBase {
        {}
 
        unsigned int fps {};
-       bool async { false };
+       unsigned int x_resolution {};
+       unsigned int y_resolution {};
 };
 
 using InputServiceCallbackType = std::function<void(ISingleoCommonData &data, void *user_data)>;
index 01bbf61c6332c7d1ed9e46303922e72c3d04f016..ccd107a456a5f5639d34f0813deda212422865cc 100644 (file)
@@ -33,7 +33,7 @@ CameraServiceDefault::CameraServiceDefault(CameraConfig &config)
        if (config.backend_type != CameraBackendType::OPENCV)
                throw InvalidOperation("Camera backend type not supported.");
 
-       _camera = make_unique<OpencvBackend>();
+       _camera = make_unique<OpencvBackend>(config);
 }
 
 CameraServiceDefault::~CameraServiceDefault()
index 7cdda471f68022b9d1d3f67713cd39319fb6584b..a373e288de35ba2198979f62fff48ed4e634a593 100644 (file)
@@ -41,7 +41,9 @@ void InputCamera::setUserCb(const std::function<void(ISingleoCommonData &data, v
 }
 
 void InputCamera::configure()
-{}
+{
+       _input->configure();
+}
 
 void InputCamera::capture(ISingleoCommonData &data)
 {
index 5b082e976932a62931ca9c4eb19748a5dfdfe376..58b247129f864ae5424b359a80616ebfabb1c858 100644 (file)
@@ -89,4 +89,4 @@ rm -rf %{buildroot}
 
 %files test
 %{_bindir}/test_singleo
-%{_bindir}/test_autozoom_on_screen
+%{_bindir}/test_singleo_on_screen
index 16c1288ca92accc2155f06669101eb490ca5f6fa..6f683017893209ec89af48be4bdf47d66301e3fd 100644 (file)
@@ -179,7 +179,7 @@ void AutoZoom::updateResult(BaseDataType &in_data)
 
        // If user callback exists then call it. This callback is used for the debug.
        if (_user_cb) {
-               auto imageData = dynamic_cast<ImageDataType &>(preprocessed_result.getData());
+               auto &imageData = dynamic_cast<ImageDataType &>(preprocessed_result.getData());
 
                _user_cb(imageData.ptr, imageData.width, imageData.height, imageData.byte_per_pixel, _user_data);
        }
index 14a8b7e9db6f1cfc52c2f1e39ec2098f40689d7c..f519bc08f34fcd42e1824a4e3d352ff91cb79911 100644 (file)
@@ -30,7 +30,6 @@ private:
        std::map<std::string, std::string> _params;
        ServiceType _service_type { ServiceType::NONE };
        InputFeedType _input_feed_type { InputFeedType::NONE };
-       CameraBackendType _camera_backend { CameraBackendType::NONE };
        unsigned int _fps {};
        bool _async_mode {};
        input::InputConfigBase _default_config;
@@ -43,7 +42,7 @@ private:
        void update();
        void setServiceType(std::string key);
        void setInputFeedType(std::string key);
-       void setCameraBackendType(std::string key);
+       void setCameraDeviceId(std::string key);
        void setFps(std::string key);
        void setAsyncMode(std::string key);
 
index 89d3f42b4f52428440450f30b70a284839ad863d..2f37be8c4a39a26fc43a66027577b17a6286498b 100644 (file)
@@ -82,17 +82,9 @@ void ServiceConfigParser::setInputFeedType(std::string key)
        _input_feed_type = it->second;
 }
 
-void ServiceConfigParser::setCameraBackendType(std::string key)
+void ServiceConfigParser::setCameraDeviceId(std::string key)
 {
-       static map<string, CameraBackendType> valid_services = { { "OPENCV", CameraBackendType::OPENCV },
-                                                                                                                        { "CAMERA_API", CameraBackendType::CAMERA_API },
-                                                                                                                        { "VISION_SOURCE", CameraBackendType::VISION_SOURCE } };
-
-       auto it = valid_services.find(key);
-       if (it == valid_services.end())
-               throw InvalidParameter("Invalid camera backend type.");
-
-       _camera_backend = it->second;
+       _camera_config.device_id = std::stoi(key);
 }
 
 void ServiceConfigParser::setFps(std::string key)
@@ -137,7 +129,7 @@ void ServiceConfigParser::trim(string &str)
 void ServiceConfigParser::parse(string option)
 {
        map<string, bool> valid_keys = {
-               { "SERVICE", false }, { "INPUT", false }, { "CAMERA_BACKEND", false }, { "FPS", false }, { "ASYNC", false }
+               { "SERVICE", false }, { "INPUT_FEED", false }, { "CAMERA_ID", false }, { "FPS", false }, { "ASYNC", false }
        };
        istringstream iss(option);
        string token;
@@ -187,8 +179,8 @@ void ServiceConfigParser::update()
        };
 
        setParam("SERVICE", &ServiceConfigParser::setServiceType);
-       setParam("INPUT", &ServiceConfigParser::setInputFeedType);
-       setParam("CAMERA_BACKEND", &ServiceConfigParser::setCameraBackendType);
+       setParam("INPUT_FEED", &ServiceConfigParser::setInputFeedType);
+       setParam("CAMERA_ID", &ServiceConfigParser::setCameraDeviceId);
        setParam("FPS", &ServiceConfigParser::setFps);
        setParam("ASYNC", &ServiceConfigParser::setAsyncMode);
 }
@@ -200,9 +192,10 @@ InputConfigBase &ServiceConfigParser::getConfig(InputFeedType input_feed_type)
        if (input_feed_type != InputFeedType::CAMERA)
                return _default_config;
 
-       _camera_config.backend_type = _camera_backend;
-       _camera_config.fps = _fps;
-       _camera_config.async = _async_mode;
+       // TODO. consider for camera backend meta file approach later
+       //       so that given camera backend can be given through the meta file.
+       //       Now, it is hard-coded.
+       _camera_config.backend_type = CameraBackendType::OPENCV;
 
        return _camera_config;
 }
index 5d5a8e09a90d627fda2b9808d4c801f16dfed7ee..af45d51c14d40496ef21af98fb82f9bd8d640fd2 100644 (file)
@@ -67,8 +67,7 @@ TEST(AutoZoomTest, InferenceRequestWithCameraInputFeedShouldBeOk)
 {
        singleo_service_h handle;
 
-       int ret =
-                       singleo_service_create("service=auto_zoom, input=camera, camera_backend=opencv, fps=30, async=0", &handle);
+       int ret = singleo_service_create("service=auto_zoom, input_feed=camera, camera_id=0, fps=30, async=0", &handle);
        ASSERT_EQ(ret, SINGLEO_ERROR_NONE);
 
        ret = singleo_service_perform(handle);
@@ -137,8 +136,7 @@ TEST(AutoZoomAsyncTest, InferenceRequestWithCameraInputFeedShouldBeOk)
 {
        singleo_service_h handle;
 
-       int ret =
-                       singleo_service_create("service=auto_zoom, input=camera, camera_backend=opencv, fps=30, async=1", &handle);
+       int ret = singleo_service_create("service=auto_zoom, input_feed=camera, camera_id=0, fps=30, async=1", &handle);
        ASSERT_EQ(ret, SINGLEO_ERROR_NONE);
 
        ret = singleo_service_perform(handle);
index 4e8065936082ae592e7824617921b70a4f6772e9..ab4149022b1da477966090f2696aad1b6b62b97f 100644 (file)
@@ -82,8 +82,7 @@ TEST(AutoZoomAsyncOnScreenTest, InferenceRequestWithCameraInputFeedShouldBeOk)
 {
        singleo_service_h handle;
 
-       int ret =
-                       singleo_service_create("service=auto_zoom, input=camera, camera_backend=opencv, fps=30, async=1", &handle);
+       int ret = singleo_service_create("service=auto_zoom, input_feed=camera, camera_id=0, fps=30, async=1", &handle);
        ASSERT_EQ(ret, SINGLEO_ERROR_NONE);
 
        ret = singleo_service_register_user_callback(handle, user_callback, nullptr);