Archived
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7589898c4a |
Generated
+15
@@ -1642,6 +1642,21 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object_detection_open_vino_server"
|
||||
version = "0.3.8"
|
||||
dependencies = [
|
||||
"async-channel",
|
||||
"async-trait",
|
||||
"common",
|
||||
"plugin",
|
||||
"pretty_assertions",
|
||||
"test-case",
|
||||
"thiserror 2.0.11",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object_detection_tflite"
|
||||
version = "0.3.8"
|
||||
|
||||
@@ -6,6 +6,7 @@ members = [
|
||||
"plugins/mqtt",
|
||||
"plugins/object_detection",
|
||||
"plugins/object_detection_tflite",
|
||||
"plugins/object_detection_open_vino_server",
|
||||
"plugins/thumb_scale",
|
||||
|
||||
"src/sentryshot",
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ if [ -z "${CARGO_TARGET_DIR}" ]; then
|
||||
export CARGO_TARGET_DIR="$target_dir"
|
||||
fi
|
||||
|
||||
plugins="auth_basic auth_none motion mqtt object_detection object_detection_tflite thumb_scale"
|
||||
plugins="auth_basic auth_none motion mqtt object_detection object_detection_tflite object_detection_open_vino_server thumb_scale"
|
||||
packages="-p sentryshot"
|
||||
for plugin in $plugins; do
|
||||
packages="$packages -p $plugin"
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
use libloading::{Library, Symbol};
|
||||
use plugin::{
|
||||
CheckPluginVersionError, FindPluginPathError, check_plugin_version, find_plugin_path,
|
||||
get_version2, object_detection::DynTfliteBackend,
|
||||
get_version2,
|
||||
object_detection::{DynOpenVinoServerBackend, DynTfliteBackend},
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use thiserror::Error;
|
||||
@@ -15,6 +16,7 @@ pub(crate) struct BackendLoader {
|
||||
plugin_version: String,
|
||||
|
||||
tflite: Option<DynTfliteBackend>,
|
||||
open_vino_server: Option<DynOpenVinoServerBackend>,
|
||||
}
|
||||
|
||||
impl BackendLoader {
|
||||
@@ -24,6 +26,7 @@ impl BackendLoader {
|
||||
plugin_dir,
|
||||
plugin_version: get_version2(),
|
||||
tflite: None,
|
||||
open_vino_server: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +50,32 @@ impl BackendLoader {
|
||||
}
|
||||
Ok(self.tflite.as_mut().expect("Some"))
|
||||
}
|
||||
|
||||
pub(crate) fn open_vino_server_backend(
|
||||
&mut self,
|
||||
) -> Result<&mut DynOpenVinoServerBackend, LoadBackendError> {
|
||||
// No fallible Option::create_or_insert()
|
||||
// https://github.com/rust-lang/libs-team/issues/577
|
||||
use LoadBackendError::*;
|
||||
if self.open_vino_server.is_none() {
|
||||
let plugin_name = "object_detection_open_vino_server";
|
||||
let plugin_path = find_plugin_path(&self.plugin_dir, plugin_name)?;
|
||||
let dylib = unsafe { Library::new(plugin_path).map_err(LoadLibrary)? };
|
||||
check_plugin_version(&dylib, &self.plugin_version).map_err(CheckPluginVersion)?;
|
||||
|
||||
let new_fn: Symbol<fn(rt_handle: Handle) -> DynOpenVinoServerBackend> = unsafe {
|
||||
dylib
|
||||
.get(b"new_open_vino_server_backend")
|
||||
.map_err(LoadSymbol)?
|
||||
};
|
||||
|
||||
self.open_vino_server = Some(new_fn(self.rt_handle.clone()));
|
||||
|
||||
// Keep the shared library loaded until the program exits.
|
||||
Box::leak(Box::new(dylib));
|
||||
}
|
||||
Ok(self.open_vino_server.as_mut().expect("Some"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
||||
@@ -29,6 +29,9 @@ struct RawDetectorConfigs {
|
||||
|
||||
#[serde(rename = "detector_edgetpu")]
|
||||
edgetpu: Vec<RawDetectorConfigEdgeTpu>,
|
||||
|
||||
#[serde(rename = "detector_open_vino_server")]
|
||||
open_vino_server: Vec<RawDetectorConfigOpenVinoServer>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq, Eq)]
|
||||
@@ -61,6 +64,17 @@ struct RawDetectorConfigEdgeTpu {
|
||||
device: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq, Eq)]
|
||||
struct RawDetectorConfigOpenVinoServer {
|
||||
enable: bool,
|
||||
name: DetectorName,
|
||||
host: String,
|
||||
input_tensor: String,
|
||||
output_tensor: String,
|
||||
input_width: NonZeroU16,
|
||||
input_height: NonZeroU16,
|
||||
}
|
||||
|
||||
type DetectorConfigs = HashMap<DetectorName, DetectorConfig>;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -183,6 +197,7 @@ pub(crate) fn write_detector_config(path: &Path) -> Result<(), std::io::Error> {
|
||||
common::write_file(path, DEFAULT_CONFIG.as_bytes())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
async fn parse_detector_configs(
|
||||
backend_loader: &mut BackendLoader,
|
||||
task_token: TaskTrackerToken,
|
||||
@@ -269,6 +284,44 @@ async fn parse_detector_configs(
|
||||
detectors.insert(edgetpu.name, detector);
|
||||
}
|
||||
|
||||
for vino in configs.open_vino_server {
|
||||
if !vino.enable {
|
||||
logger.log(
|
||||
LogLevel::Debug,
|
||||
&format!("detector '{}' disabled", vino.name),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if detector_configs.contains_key(&vino.name) {
|
||||
return Err(Duplicate(vino.name));
|
||||
};
|
||||
|
||||
let detector = backend_loader
|
||||
.open_vino_server_backend()?
|
||||
.new_open_vino_server_detector(
|
||||
&task_token,
|
||||
&logger,
|
||||
&vino.name,
|
||||
vino.host,
|
||||
vino.input_tensor,
|
||||
vino.output_tensor,
|
||||
vino.input_width,
|
||||
vino.input_height,
|
||||
)
|
||||
.map_err(CreateDetector)?;
|
||||
detectors.insert(vino.name.clone(), detector);
|
||||
|
||||
let config = DetectorConfig {
|
||||
width: vino.input_width,
|
||||
height: vino.input_height,
|
||||
// The types of objects the server can detect must be known at this point.
|
||||
// If they are known by the model server then `new_open_vino_server_detector()`
|
||||
// should return `Result<(ArcDetector, Vec<Label>), DynError>`.
|
||||
labels: vec!["person".to_owned().try_into().expect("")],
|
||||
};
|
||||
detector_configs.insert(vino.name, config);
|
||||
}
|
||||
|
||||
Ok(DetectorManager {
|
||||
detectors,
|
||||
configs: detector_configs,
|
||||
@@ -432,6 +485,15 @@ threads = 1
|
||||
label_map = \"file:///13\"
|
||||
format = \"nolo\"
|
||||
device = \"14\"
|
||||
|
||||
[[detector_open_vino_server]]
|
||||
enable = false
|
||||
name = \"15\"
|
||||
host = \"host\"
|
||||
input_tensor = \"input_0\"
|
||||
output_tensor = \"output_0\"
|
||||
input_width = 16
|
||||
input_height = 17
|
||||
";
|
||||
let got = parse_raw_detector_configs(raw).unwrap();
|
||||
let want = RawDetectorConfigs {
|
||||
@@ -461,6 +523,15 @@ threads = 1
|
||||
format: TfliteFormat::Nolo,
|
||||
device: "14".parse().unwrap(),
|
||||
}],
|
||||
open_vino_server: vec![RawDetectorConfigOpenVinoServer {
|
||||
enable: false,
|
||||
name: "15".to_owned().try_into().unwrap(),
|
||||
host: "host".to_owned(),
|
||||
input_tensor: "input_0".to_owned(),
|
||||
output_tensor: "output_0".to_owned(),
|
||||
input_width: NonZeroU16::new(16).unwrap(),
|
||||
input_height: NonZeroU16::new(17).unwrap(),
|
||||
}],
|
||||
};
|
||||
assert_eq!(want, got);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
[package]
|
||||
name = "object_detection_open_vino_server"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
name = "object_detection_open_vino_server"
|
||||
path = "open_vino_server.rs"
|
||||
crate-type = ["cdylib"]
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
common.path = "../../src/common"
|
||||
plugin.path = "../../src/plugin"
|
||||
|
||||
async-trait.workspace = true
|
||||
thiserror.workspace = true
|
||||
tokio.workspace = true
|
||||
tokio-util.workspace = true
|
||||
async-channel.workspace = true
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions.workspace = true
|
||||
test-case.workspace = true
|
||||
@@ -0,0 +1,84 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
use async_trait::async_trait;
|
||||
use common::{
|
||||
ArcMsgLogger, Detection, Detections, DynError, LogLevel, RectangleNormalized, Region,
|
||||
recording::normalize,
|
||||
};
|
||||
use plugin::object_detection::{
|
||||
ArcDetector, Detector, DetectorName, DynOpenVinoServerBackend, OpenVinoServerBackend,
|
||||
};
|
||||
use std::{
|
||||
ffi::c_char,
|
||||
num::{NonZeroU16, NonZeroU32},
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio::runtime::Handle;
|
||||
use tokio_util::task::task_tracker::TaskTrackerToken;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn version() -> *const c_char {
|
||||
plugin::get_version()
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "Rust" fn new_open_vino_server_backend(rt_handle: Handle) -> DynOpenVinoServerBackend {
|
||||
Box::new(OpenVinoServerBackendImpl { rt_handle })
|
||||
}
|
||||
|
||||
pub(crate) struct OpenVinoServerBackendImpl {
|
||||
rt_handle: Handle,
|
||||
}
|
||||
|
||||
impl OpenVinoServerBackend for OpenVinoServerBackendImpl {
|
||||
fn new_open_vino_server_detector(
|
||||
&self,
|
||||
_task_token: &TaskTrackerToken,
|
||||
logger: &ArcMsgLogger,
|
||||
name: &DetectorName,
|
||||
_host: String,
|
||||
_input_tensor: String,
|
||||
_output_tensor: String,
|
||||
input_width: NonZeroU16,
|
||||
input_height: NonZeroU16,
|
||||
) -> Result<ArcDetector, DynError> {
|
||||
logger.log(LogLevel::Info, &format!("starting detector '{name}'"));
|
||||
|
||||
Ok(Arc::new(OpenVinoServerDetector {
|
||||
_rt_handle: self.rt_handle.clone(),
|
||||
input_width,
|
||||
input_height,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
struct OpenVinoServerDetector {
|
||||
_rt_handle: Handle,
|
||||
input_width: NonZeroU16,
|
||||
input_height: NonZeroU16,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Detector for OpenVinoServerDetector {
|
||||
async fn detect(&self, _data: Vec<u8>) -> Result<Option<Detections>, DynError> {
|
||||
Ok(Some(vec![Detection {
|
||||
label: "person".to_owned().try_into().expect(""),
|
||||
score: 100.0,
|
||||
region: Region {
|
||||
rectangle: Some(RectangleNormalized {
|
||||
x: normalize(10, 100),
|
||||
y: normalize(10, 100),
|
||||
width: NonZeroU32::new(normalize(40, 100)).expect(""),
|
||||
height: NonZeroU32::new(normalize(40, 100)).expect(""),
|
||||
}),
|
||||
polygon: None,
|
||||
},
|
||||
}]))
|
||||
}
|
||||
fn width(&self) -> NonZeroU16 {
|
||||
self.input_width
|
||||
}
|
||||
fn height(&self) -> NonZeroU16 {
|
||||
self.input_height
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,23 @@ pub trait TfliteBackend {
|
||||
) -> Result<ArcDetector, DynError>;
|
||||
}
|
||||
|
||||
pub type DynOpenVinoServerBackend = Box<dyn OpenVinoServerBackend>;
|
||||
|
||||
pub trait OpenVinoServerBackend {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn new_open_vino_server_detector(
|
||||
&self,
|
||||
task_token: &TaskTrackerToken,
|
||||
logger: &ArcMsgLogger,
|
||||
name: &DetectorName,
|
||||
host: String,
|
||||
input_tensor: String,
|
||||
output_tensor: String,
|
||||
input_width: NonZeroU16,
|
||||
input_height: NonZeroU16,
|
||||
) -> Result<ArcDetector, DynError>;
|
||||
}
|
||||
|
||||
pub type LabelMap = HashMap<u16, Label>;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Deserialize, PartialEq, Eq)]
|
||||
|
||||
Reference in New Issue
Block a user