forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
89 lines (76 loc) · 2.62 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Define the interface between Rust and OpenVINO's C++ [API](https://docs.openvinotoolkit.org/latest/usergroup16.html).
use cxx::UniquePtr;
/// This module uses the `cxx` library to safely bridge the gap to the C++ API exposed by OpenVINO.
#[cxx::bridge(namespace = InferenceEngine)]
mod ffi {
extern "C" {
include!("src/bridge.h");
type Core;
pub fn core_new(xml_config_file: &str) -> UniquePtr<Core>;
pub fn core_new_default() -> UniquePtr<Core>;
pub fn read_network(
core: UniquePtr<Core>,
model_path: &str,
bin_path: &str,
) -> UniquePtr<CNNNetwork>;
type CNNNetwork;
pub fn set_batch_size(network: UniquePtr<CNNNetwork>, size: usize);
}
}
/// See [Core](https://docs.openvinotoolkit.org/latest/classInferenceEngine_1_1Core.html).
pub struct Core {
instance: UniquePtr<ffi::Core>,
}
/// Unfortunately, the OpenVINO APIs return objects that we must wrap in a [UniquePtr] in `bridge.h`
/// before using in Rust.
impl Core {
pub fn new(xml_config_file: Option<&str>) -> Core {
let instance = match xml_config_file {
None => ffi::core_new_default(),
Some(f) => ffi::core_new(f),
};
Core { instance }
}
pub fn read_network(self, model_path: &str, bin_path: &str) -> CNNNetwork {
let instance = ffi::read_network(self.instance, model_path, bin_path);
CNNNetwork { instance }
}
}
pub struct CNNNetwork {
instance: UniquePtr<ffi::CNNNetwork>,
}
impl CNNNetwork {
pub fn set_batch_size(self, size: usize) {
ffi::set_batch_size(self.instance, size)
}
}
#[cfg(test)]
mod test {
use super::*;
use std::path::Path;
// FIXME this test relies on a plugins.xml file being moved to a default location; see build.rs.
#[test]
fn construct_core() {
Core::new(None);
}
// FIXME this test relies on a pre-built model in the filesystem--avoid this.
#[test]
fn read_network() {
let core = Core::new(None);
let dir = Path::new("../../../../test-openvino/");
core.read_network(
&dir.join("frozen_inference_graph.xml").to_string_lossy(),
&dir.join("frozen_inference_graph.bin").to_string_lossy(),
);
}
#[test]
fn set_batch_size() {
let core = Core::new(None);
let dir = Path::new("../../../../test-openvino/");
let network = core.read_network(
&dir.join("frozen_inference_graph.xml").to_string_lossy(),
&dir.join("frozen_inference_graph.bin").to_string_lossy(),
);
network.set_batch_size(1);
}
}