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>
* @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
#include <thread>
#include <memory>
+#include <unordered_set>
#include <opencv2/opencv.hpp>
#include "ICameraBackend.h"
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;
{
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()
_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,
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)
{
{}
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 {
{}
unsigned int fps {};
- bool async { false };
+ unsigned int x_resolution {};
+ unsigned int y_resolution {};
};
using InputServiceCallbackType = std::function<void(ISingleoCommonData &data, void *user_data)>;
if (config.backend_type != CameraBackendType::OPENCV)
throw InvalidOperation("Camera backend type not supported.");
- _camera = make_unique<OpencvBackend>();
+ _camera = make_unique<OpencvBackend>(config);
}
CameraServiceDefault::~CameraServiceDefault()
}
void InputCamera::configure()
-{}
+{
+ _input->configure();
+}
void InputCamera::capture(ISingleoCommonData &data)
{
%files test
%{_bindir}/test_singleo
-%{_bindir}/test_autozoom_on_screen
+%{_bindir}/test_singleo_on_screen
// 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);
}
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;
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);
_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)
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;
};
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);
}
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;
}
{
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);
{
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);
{
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);