🌐
English
Motion Detection API#
Header File#
OasisMotion.h
MotionDetectObserver Interface#
class MotionDetectObserver : public std::enable_shared_from_this<MotionDetectObserver>
{
public:
MotionDetectObserver();
virtual ~MotionDetectObserver();
virtual void onMotionDetected(int32_t camera_id, int32_t detected);
};
void
onMotionDetected
(
int32_t
camera_id
,
int32_t
detected
)
OasisMotion.h
Called when motion is detected.
Parameters
camera_id
The camera ID passed as a key-value map parameter when calling
createMotionDetector.
detected
The intensity of the detected motion.
Functions#
MotionDetectorRef
createMotionDetector
(
const MediaSourceRef &
source
,
key_value_map_t &
parameters
)
OasisMotion.h
Creates a motion detector.
Parameters
source
The video source for motion detection. It can be a MediaSource that provides video, such as a camera or
playerGetVideoTrackSource of a media player.
parameters
The key-value map parameters required for motion detection.
Return Value
Returns a MotionDetector object on success. Returns nullptr on failure.
The key-value map required for motion detection is as follows:
Key
Default
M
Description
track-id
In the case of playback, source is the MediaSource corresponding to the track-id.
enable
Enables motion detection if "1". Disables motion detection if "0".
fps
6
The FPS for motion detection. Specify an appropriate value based on the CPU load.
use-g2d
0
Uses the hardware-accelerated scaling of the chipset if "1". Uses SIMD software scaling if "0". Software scaling can downscale up to a maximum of 4x. If scaling exceeds 4x, hardware acceleration is required.
sensitivity-level
5
Configures the motion detection sensitivity. Uses a value between 1 and 6. (Not used)
version
2
Selects the engine version to be used for motion detection. Uses a value of 1 or 2, where 2 is the latest version.
threshold
0
○
The minimum motion detection value to determine the occurrence of motion.
state-size
5
For internal algorithm use.
window-scale
3.0
For internal algorithm use.
window-threshold
3.5
For internal algorithm use.
shadow-factor
0.5
For internal algorithm use.
weight-factor
0.01
For internal algorithm use.
weight-threshold
0.9
For internal algorithm use.
height-max
100
The maximum allowed height of the video scaled for motion detection.
is-player
0
Specify a value of "1" when using it for media playback.
buffer-count
3
The number of video buffers.
MotionDetectorRef
createMotionDetector
(
key_value_map_t &
parameters
)
OasisMotion.h
Creates a motion detector for camera device video. The camera device uses the "camera-id" key value.
Parameters
parameters
The key-value map parameters required for motion detection and the camera device.
Return Value
Returns a MotionDetector object on success. Returns nullptr on failure.
int32_t
destroyMotionDetector
(
MotionDetectorRef &
motion_detector
)
OasisMotion.h
Releases the motion detector created via
createMotionDetector.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorId
(
const MotionDetectorRef &
motion_detector
)
OasisMotion.h
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
Return Value
Returns the motion detector ID. Identical to the MediaSource value.
int32_t
motionDetectorStart
(
const MotionDetectorRef &
motion_detector
,
const std::shared_ptr<MotionDetectObserver> &
observer
)
OasisMotion.h
Starts motion detection.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
observer
A user-defined observer object derived from MotionDetectObserver for calling motion detection events.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorStop
(
const MotionDetectorRef &
motion_detector
)
OasisMotion.h
Stops motion detection.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorSetThreshold
(
const MotionDetectorRef &
motion_detector
,
int32_t
threshold
)
OasisMotion.h
Configures the detection threshold value of the motion detector. If a pixel variation higher than this value occurs, a motion detection event is triggered. The threshold value is determined based on the "height-max" value of the key-value map. By initially setting the
threshold value to "0", you can check the motion detection values in detection events and choose an appropriate value that suits your operation.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
threshold
The motion detection threshold value.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorSetFrameRate
(
const MotionDetectorRef &
motion_detector
,
int32_t
fps
)
OasisMotion.h
Configures the frame rate for motion detection.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
fps
The frame rate value.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorGetThreshold
(
const MotionDetectorRef &
motion_detector
,
int32_t &
threshold
)
OasisMotion.h
Returns the currently configured detection threshold value of the motion detector.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
threshold
OUT
The motion detection threshold value is stored and returned.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorGetFrameRate
(
const MotionDetectorRef &
motion_detector
,
int32_t &
fps
)
OasisMotion.h
Returns the current frame rate configured in the motion detector.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
fps
OUT
The current frame rate value is stored and returned.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorAddROIRegion
(
const MotionDetectorRef &
motion_detector
,
int32_t
left
,
int32_t
top
,
int32_t
width
,
int32_t
height
,
bool
enabled
)
OasisMotion.h
Adds a region to the motion detector. Each coordinate value of the region is based on the original video dimensions. Setting at least one motion detection region in the motion detector shifts behavior from full motion detection to region-specific motion detection.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
left
The left coordinate of the region.
top
The top coordinate of the region.
width
The width of the region.
height
The height of the region.
enabled
Specifies whether to perform motion detection in this region. Toggles motion detection if
true.
Return Value
Returns the ID value of the motion detection region on success. Returns -1 on failure.
int32_t
motionDetectorEnableROIRegion
(
const MotionDetectorRef &
motion_detector
,
int32_t
roi_id
,
bool
enable
)
OasisMotion.h
Configures motion detection status for a specific region. Disabling all regions initiates full motion detection.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
roi_id
The region ID returned by
motionDetectorAddROIRegion. Configures all regions if -1.
enable
Performs motion detection if
true. Disables motion detection if false.
Return Value
- 0: Success
- -1: Failure
int32_t
motionDetectorRemoveROIRegion
(
const MotionDetectorRef &
motion_detector
,
int32_t
roi_id
)
OasisMotion.h
Removes a motion detection region. Removing all regions initiates full motion detection.
Parameters
motion_detector
Specifies the motion detector created via
createMotionDetector.
roi_id
The region ID returned by
motionDetectorAddROIRegion. Removes all regions if -1.
Return Value
- 0: Success
- -1: Failure
Example#
Below is an example of detecting motion from the camera device ID 0 video layout stream.
#include "OasisAPI.h"
#include "OasisLog.h"
#include "OasisMedia.h"
#include "OasisFS.h"
#include "OasisUtil.h"
#include "OasisMotion.h"
#include <thread>
#include <mutex>
#include <memory>
#include <condition_variable>
#include <signal.h>
#define DLOG_THIS 0x00010000
#define DLOG_RECORD 0x00020000
#define DLOG_FRAME 0x00040000
#define DLOG_TRACE 0x00080000
#undef DLOG_FLAGS
#define DLOG_FLAGS (DLOG_FLAGS_DEFAULT|DLOG_RECORD|DLOG_TRACE|DLOG_THIS/*|DLOG_FRAME*/)
#undef DLOG_TAG
#define DLOG_TAG "Motion"
using namespace oasis;
static bool continue_running_ = true;
class MyMotionDetectObserver : public MotionDetectObserver
{
public:
MyMotionDetectObserver() {}
virtual ~MyMotionDetectObserver() {}
virtual void onMotionDetected(int32_t camera_id, int32_t detected) {
TRACE0("camera<%d> motion detected#%d\n", camera_id, detected);
}
};
void print_usage(const char *pname)
{
DLOG0(DLOG_INFO, "USAGE: %s\n", pname);
}
void cancel_handler(int signum)
{
continue_running_ = false;
}
int main(int argc, char* argv[])
{
int32_t err;
int32_t i, c, n;
std::thread t1, t2, t3;
oasis::key_value_map_t parameters;
srand(time(NULL));
signal(SIGINT, cancel_handler);
////////////////////////////////////////////////////////////////////////////////////////////
// init
parameters["offs-disable"] = "1";
//parameters["oasis-log-flags"] = std::to_string(OASIS_LOG_DEBUG/*|OASIS_LOG_ENCODE_BITRATE*/);
if(oasis::initialize(parameters) < 0) {
DLOG(DLOG_ERROR, "Oasis init failed\n");
return -1;
}
////////////////////////////////////////////////////////////////////////////////////////////
// sources
parameters.clear();
parameters["source-count"] = "1";
parameters["source1-camera-id"] = "0";
parameters["source1-isp-id"] = "0";
parameters["source1-isp-wdr-mode"] = "0";
parameters["source1-capture-format"] = "YUV420";
parameters["source1-capture-buffers"] = "5";
parameters["source1-fps"] = "30";
parameters["source1-subchannel-rotation"] = "0";
parameters["source1-loc"] = "front";
parameters["source1-capture-resolution"] = "1080p";
parameters["source1-sensor-config"] = "./Resource_tp2863/VIC/0/tp2863_1920x1080_ch0.cfg";
parameters["source1-autoscene-config"] = "./Resource_tp2863/AutoScene/autoscene_conf.cfg";
parameters["source1-resource-dir"] = "./Resource_tp2863/";
configCameras(parameters);
////////////////////////////////////////////////////////////////////////////////////////////
//motion detector settings
std::shared_ptr<MyMotionDetectObserver> motion_detect_observer = std::make_shared<MyMotionDetectObserver>();
parameters.clear();
parameters["camera-id"] = "0";
parameters["enable"] = "1";
parameters["use-g2d"] = "1";
parameters["buffer-count"] = "3";
parameters["sensitivity-level"] = "5";
parameters["fps"] = "6";
parameters["version"] = "2";
parameters["height-max"] = "135";
MotionDetectorRef motion_detector = createMotionDetector(parameters);
err = motionDetectorStart(motion_detector, motion_detect_observer);
if(err < 0) {
DLOG0(DLOG_ERROR, "MotionDetectgor start failed\n");
} else {
TRACE0("MotionDetector<%d> started\n", motionDetectorId(motion_detector));
}
printf("Ctrl+C to exit...\n");
do {
usleep(100000);
} while (continue_running_ == true);
if(motion_detector) {
destroyMotionDetector(motion_detector);
}
oasis::finalize();
printf("goodbye.\n");
return 0;
}