3
3
//! See [`PrePostProcess Walkthrough`](https://docs.openvino.ai/2022.3/openvino_docs_OV_UG_Preprocessing_Overview.html).
4
4
//!
5
5
//! ```
6
- //!
7
6
//! # use openvino::{prepostprocess, Core, ElementType, Layout, Shape, Tensor, ResizeAlgorithm};
8
7
//! # use std::fs;
9
8
//! # let mut core = Core::new().expect("to instantiate the OpenVINO library");
18
17
//! // Pre-process the input by:
19
18
//! // - converting NHWC to NCHW
20
19
//! // - resizing the input image
21
- //! let mut pre_post_process = prepostprocess::PrePostProcess ::new(&model).expect("to create a new PrePostProcess instance ");
22
- //! let input_info = pre_post_process .get_input_info_by_name("input").expect("to get input info by name");
23
- //! let mut input_tensor_info = input_info.preprocess_input_info_get_tensor_info ().expect("to get tensor info");
24
- //! input_tensor_info.preprocess_input_tensor_set_from (&tensor).expect("to set tensor from");
25
- //! input_tensor_info.preprocess_input_tensor_set_layout (&Layout::new("NHWC").expect("to create a new layout")).expect("to set layout");
26
- //! let mut preprocess_steps = input_info.get_preprocess_steps ().expect("to get preprocess steps");
27
- //! preprocess_steps.preprocess_steps_resize (ResizeAlgorithm::Linear).expect("to resize");
28
- //! let model_info = input_info.get_model_info().expect("to get model info");
29
- //! model_info.model_info_set_layout (&Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
30
- //! let new_model = pre_post_process .build_new_model().expect("to build new model with above prepostprocess steps");
20
+ //! let mut pipeline = prepostprocess::Pipeline ::new(&model).expect("to create a new pipeline ");
21
+ //! let input_info = pipeline .get_input_info_by_name("input").expect("to get input info by name");
22
+ //! let mut input_tensor_info = input_info.get_tensor_info ().expect("to get tensor info");
23
+ //! input_tensor_info.set_from (&tensor).expect("to set tensor from");
24
+ //! input_tensor_info.set_layout (&Layout::new("NHWC").expect("to create a new layout")).expect("to set layout");
25
+ //! let mut preprocess_steps = input_info.get_steps ().expect("to get preprocess steps");
26
+ //! preprocess_steps.resize (ResizeAlgorithm::Linear).expect("to resize");
27
+ //! let mut model_info = input_info.get_model_info().expect("to get model info");
28
+ //! model_info.set_layout (&Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
29
+ //! let new_model = pipeline .build_new_model().expect("to build new model with above prepostprocess steps");
31
30
//! ```
32
31
use crate :: {
33
32
cstr, drop_using_function, layout:: Layout , try_unsafe, util:: Result , ElementType , Model ,
@@ -56,86 +55,12 @@ use openvino_sys::{
56
55
57
56
/// See [`PrePostProcess`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__prepostprocessor__t.html).
58
57
#[ derive( Debug ) ]
59
- pub struct PrePostProcess {
58
+ pub struct Pipeline {
60
59
ptr : * mut ov_preprocess_prepostprocessor_t ,
61
60
}
62
- drop_using_function ! ( PrePostProcess , ov_preprocess_prepostprocessor_free) ;
63
-
64
- /// See [`PreProcessInputInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__input__info__t.html).
65
- pub struct PreProcessInputInfo {
66
- ptr : * mut ov_preprocess_input_info_t ,
67
- }
68
- drop_using_function ! ( PreProcessInputInfo , ov_preprocess_input_info_free) ;
69
-
70
- /// See [`PreprocessOutputInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__output__info__t.html).
71
- pub struct PreProcessOutputInfo {
72
- ptr : * mut ov_preprocess_output_info_t ,
73
- }
74
- drop_using_function ! ( PreProcessOutputInfo , ov_preprocess_output_info_free) ;
75
-
76
- /// See [`PreprocessSteps`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__preprocess__steps__t.html).
77
- pub struct PreProcessSteps {
78
- ptr : * mut ov_preprocess_preprocess_steps_t ,
79
- }
80
- drop_using_function ! ( PreProcessSteps , ov_preprocess_preprocess_steps_free) ;
81
-
82
- /// See [`PreprocessInputModelInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__input__model__info__t.html).
83
- pub struct PreProcessInputModelInfo {
84
- ptr : * mut ov_preprocess_input_model_info_t ,
85
- }
86
- drop_using_function ! (
87
- PreProcessInputModelInfo ,
88
- ov_preprocess_input_model_info_free
89
- ) ;
90
-
91
- /// See [`PreprocessInputTensorInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__input__tensor__info__t.html).
92
- pub struct PreProcessInputTensorInfo {
93
- ptr : * mut ov_preprocess_input_tensor_info_t ,
94
- }
95
- drop_using_function ! (
96
- PreProcessInputTensorInfo ,
97
- ov_preprocess_input_tensor_info_free
98
- ) ;
99
-
100
- /// See [`PreprocessOutputTensorInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__output__tensor__info__t.html).
101
- pub struct PreProcessOutputTensorInfo {
102
- ptr : * mut ov_preprocess_output_tensor_info_t ,
103
- }
104
- drop_using_function ! (
105
- PreProcessOutputTensorInfo ,
106
- ov_preprocess_output_tensor_info_free
107
- ) ;
108
-
109
- impl PreProcessInputModelInfo {
110
- /// Sets the layout for the model information obj.
111
- pub fn model_info_set_layout ( & self , layout : & Layout ) -> Result < ( ) > {
112
- try_unsafe ! ( ov_preprocess_input_model_info_set_layout(
113
- self . ptr,
114
- layout. as_ptr( )
115
- ) )
116
- }
117
- }
118
-
119
- impl PreProcessInputTensorInfo {
120
- /// Sets the layout for the input tensor.
121
- pub fn preprocess_input_tensor_set_layout ( & self , layout : & Layout ) -> Result < ( ) > {
122
- try_unsafe ! ( ov_preprocess_input_tensor_info_set_layout(
123
- self . ptr,
124
- layout. as_ptr( )
125
- ) )
126
- }
127
-
128
- /// Sets the input tensor info from an existing tensor.
129
- pub fn preprocess_input_tensor_set_from ( & mut self , tensor : & Tensor ) -> Result < ( ) > {
130
- try_unsafe ! ( ov_preprocess_input_tensor_info_set_from(
131
- self . ptr,
132
- tensor. as_ptr( )
133
- ) )
134
- }
135
- }
136
-
137
- impl PrePostProcess {
138
- /// Creates a new `PrePostProcess` pipeline for the given model.
61
+ drop_using_function ! ( Pipeline , ov_preprocess_prepostprocessor_free) ;
62
+ impl Pipeline {
63
+ /// Creates a new [`Pipeline`] for the given [`Model`].
139
64
pub fn new ( model : & Model ) -> Result < Self > {
140
65
let mut ptr = std:: ptr:: null_mut ( ) ;
141
66
try_unsafe ! ( ov_preprocess_prepostprocessor_create(
@@ -146,65 +71,65 @@ impl PrePostProcess {
146
71
}
147
72
148
73
/// Retrieves the input information by index.
149
- pub fn get_input_info_by_index ( & self , index : usize ) -> Result < PreProcessInputInfo > {
74
+ pub fn get_input_info_by_index ( & self , index : usize ) -> Result < InputInfo > {
150
75
let mut ptr = std:: ptr:: null_mut ( ) ;
151
76
try_unsafe ! ( ov_preprocess_prepostprocessor_get_input_info_by_index(
152
77
self . ptr,
153
78
index,
154
79
std:: ptr:: addr_of_mut!( ptr)
155
80
) ) ?;
156
81
157
- Ok ( PreProcessInputInfo { ptr } )
82
+ Ok ( InputInfo { ptr } )
158
83
}
159
84
160
85
/// Retrieves the input information by name.
161
- pub fn get_input_info_by_name ( & self , name : & str ) -> Result < PreProcessInputInfo > {
86
+ pub fn get_input_info_by_name ( & self , name : & str ) -> Result < InputInfo > {
162
87
let mut ptr = std:: ptr:: null_mut ( ) ;
163
88
try_unsafe ! ( ov_preprocess_prepostprocessor_get_input_info_by_name(
164
89
self . ptr,
165
90
cstr!( name) ,
166
91
std:: ptr:: addr_of_mut!( ptr)
167
92
) ) ?;
168
93
169
- Ok ( PreProcessInputInfo { ptr } )
94
+ Ok ( InputInfo { ptr } )
170
95
}
171
96
172
97
/// Retrieves the output information by name.
173
- pub fn get_output_info_by_name ( & self , name : & str ) -> Result < PreProcessOutputInfo > {
98
+ pub fn get_output_info_by_name ( & self , name : & str ) -> Result < OutputInfo > {
174
99
let mut ptr = std:: ptr:: null_mut ( ) ;
175
100
try_unsafe ! ( ov_preprocess_prepostprocessor_get_output_info_by_name(
176
101
self . ptr,
177
102
cstr!( name) ,
178
103
std:: ptr:: addr_of_mut!( ptr)
179
104
) ) ?;
180
- Ok ( PreProcessOutputInfo { ptr } )
105
+ Ok ( OutputInfo { ptr } )
181
106
}
182
107
183
108
/// Retrieves the output information by index.
184
- pub fn get_output_info_by_index ( & self , index : usize ) -> Result < PreProcessOutputInfo > {
109
+ pub fn get_output_info_by_index ( & self , index : usize ) -> Result < OutputInfo > {
185
110
let mut ptr = std:: ptr:: null_mut ( ) ;
186
111
try_unsafe ! ( ov_preprocess_prepostprocessor_get_output_info_by_index(
187
112
self . ptr,
188
113
index,
189
114
std:: ptr:: addr_of_mut!( ptr)
190
115
) ) ?;
191
116
192
- Ok ( PreProcessOutputInfo { ptr } )
117
+ Ok ( OutputInfo { ptr } )
193
118
}
194
119
195
120
/// Retrieves the input information.
196
121
///
197
122
/// # Panics
198
123
///
199
124
/// Panics if the returned input info is null.
200
- pub fn get_input_info ( & self ) -> Result < PreProcessInputInfo > {
125
+ pub fn get_input_info ( & self ) -> Result < InputInfo > {
201
126
let mut ptr = std:: ptr:: null_mut ( ) ;
202
127
try_unsafe ! ( ov_preprocess_prepostprocessor_get_input_info(
203
128
self . ptr,
204
129
std:: ptr:: addr_of_mut!( ptr)
205
130
) ) ?;
206
131
assert ! ( !ptr. is_null( ) ) ;
207
- Ok ( PreProcessInputInfo { ptr } )
132
+ Ok ( InputInfo { ptr } )
208
133
}
209
134
210
135
/// Builds a new model with all steps from pre/postprocessing.
@@ -218,88 +143,141 @@ impl PrePostProcess {
218
143
}
219
144
}
220
145
221
- impl PreProcessSteps {
222
- /// Resizes data in tensor.
223
- pub fn preprocess_steps_resize ( & mut self , resize_algo : ResizeAlgorithm ) -> Result < ( ) > {
224
- try_unsafe ! ( ov_preprocess_preprocess_steps_resize(
146
+ /// See [`PreProcessInputInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__input__info__t.html).
147
+ pub struct InputInfo {
148
+ ptr : * mut ov_preprocess_input_info_t ,
149
+ }
150
+ drop_using_function ! ( InputInfo , ov_preprocess_input_info_free) ;
151
+
152
+ impl InputInfo {
153
+ /// Retrieves the preprocessing model input information.
154
+ pub fn get_model_info ( & self ) -> Result < InputModelInfo > {
155
+ let mut ptr = std:: ptr:: null_mut ( ) ;
156
+ try_unsafe ! ( ov_preprocess_input_info_get_model_info(
225
157
self . ptr,
226
- resize_algo as u32 ,
158
+ std :: ptr :: addr_of_mut! ( ptr )
227
159
) ) ?;
228
-
229
- Ok ( ( ) )
160
+ Ok ( InputModelInfo { ptr } )
230
161
}
231
162
232
- /// Converts the layout of data in tensor.
233
- pub fn preprocess_convert_layout ( & self , layout : & Layout ) -> Result < ( ) > {
234
- try_unsafe ! ( ov_preprocess_preprocess_steps_convert_layout(
163
+ /// Retrieves the input tensor information.
164
+ pub fn get_tensor_info ( & self ) -> Result < InputTensorInfo > {
165
+ let mut ptr: * mut ov_preprocess_input_tensor_info_t = std:: ptr:: null_mut ( ) ;
166
+ try_unsafe ! ( ov_preprocess_input_info_get_tensor_info(
235
167
self . ptr,
236
- layout . as_ptr ( ) ,
168
+ std :: ptr :: addr_of_mut! ( ptr )
237
169
) ) ?;
170
+ Ok ( InputTensorInfo { ptr } )
171
+ }
238
172
239
- Ok ( ( ) )
173
+ /// Retrieves the preprocessing steps.
174
+ pub fn get_steps ( & self ) -> Result < Steps > {
175
+ let mut ptr = std:: ptr:: null_mut ( ) ;
176
+ try_unsafe ! ( ov_preprocess_input_info_get_preprocess_steps(
177
+ self . ptr,
178
+ std:: ptr:: addr_of_mut!( ptr)
179
+ ) ) ?;
180
+ Ok ( Steps { ptr } )
240
181
}
182
+ }
241
183
242
- /// Converts the element type of data in tensor.
243
- pub fn preprocess_convert_element_type ( & self , element_type : ElementType ) -> Result < ( ) > {
244
- try_unsafe ! ( ov_preprocess_preprocess_steps_convert_element_type(
184
+ /// See [`PreprocessOutputInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__output__info__t.html).
185
+ pub struct OutputInfo {
186
+ ptr : * mut ov_preprocess_output_info_t ,
187
+ }
188
+ drop_using_function ! ( OutputInfo , ov_preprocess_output_info_free) ;
189
+ impl OutputInfo {
190
+ /// Retrieves preprocess output tensor information.
191
+ pub fn get_tensor_info ( & self ) -> Result < OutputTensorInfo > {
192
+ let mut ptr: * mut ov_preprocess_output_tensor_info_t = std:: ptr:: null_mut ( ) ;
193
+ try_unsafe ! ( ov_preprocess_output_info_get_tensor_info(
245
194
self . ptr,
246
- element_type as u32
195
+ std :: ptr :: addr_of_mut! ( ptr )
247
196
) ) ?;
197
+ Ok ( OutputTensorInfo { ptr } )
198
+ }
199
+ }
248
200
249
- Ok ( ( ) )
201
+ /// See [`PreprocessInputModelInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__input__model__info__t.html).
202
+ pub struct InputModelInfo {
203
+ ptr : * mut ov_preprocess_input_model_info_t ,
204
+ }
205
+ drop_using_function ! ( InputModelInfo , ov_preprocess_input_model_info_free) ;
206
+ impl InputModelInfo {
207
+ /// Sets the layout for the model information obj.
208
+ pub fn set_layout ( & mut self , layout : & Layout ) -> Result < ( ) > {
209
+ try_unsafe ! ( ov_preprocess_input_model_info_set_layout(
210
+ self . ptr,
211
+ layout. as_ptr( )
212
+ ) )
250
213
}
251
214
}
252
215
253
- impl PreProcessOutputTensorInfo {
254
- /// Sets the element type for output tensor info.
255
- pub fn preprocess_set_element_type ( & self , element_type : ElementType ) -> Result < ( ) > {
256
- try_unsafe ! ( ov_preprocess_output_set_element_type(
216
+ /// See [`PreprocessInputTensorInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__input__tensor__info__t.html).
217
+ pub struct InputTensorInfo {
218
+ ptr : * mut ov_preprocess_input_tensor_info_t ,
219
+ }
220
+ drop_using_function ! ( InputTensorInfo , ov_preprocess_input_tensor_info_free) ;
221
+ impl InputTensorInfo {
222
+ /// Sets the [`Layout`] for the input tensor.
223
+ pub fn set_layout ( & mut self , layout : & Layout ) -> Result < ( ) > {
224
+ try_unsafe ! ( ov_preprocess_input_tensor_info_set_layout(
257
225
self . ptr,
258
- element_type as u32
226
+ layout. as_ptr( )
227
+ ) )
228
+ }
229
+
230
+ /// Sets the input tensor info from an existing tensor.
231
+ pub fn set_from ( & mut self , tensor : & Tensor ) -> Result < ( ) > {
232
+ try_unsafe ! ( ov_preprocess_input_tensor_info_set_from(
233
+ self . ptr,
234
+ tensor. as_ptr( )
259
235
) )
260
236
}
261
237
}
262
238
263
- impl PreProcessOutputInfo {
264
- /// Retrieves preprocess output tensor information.
265
- pub fn get_output_info_get_tensor_info ( & self ) -> Result < PreProcessOutputTensorInfo > {
266
- let mut ptr: * mut ov_preprocess_output_tensor_info_t = std:: ptr:: null_mut ( ) ;
267
- try_unsafe ! ( ov_preprocess_output_info_get_tensor_info(
239
+ /// See [`PreprocessOutputTensorInfo`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__output__tensor__info__t.html).
240
+ pub struct OutputTensorInfo {
241
+ ptr : * mut ov_preprocess_output_tensor_info_t ,
242
+ }
243
+ drop_using_function ! ( OutputTensorInfo , ov_preprocess_output_tensor_info_free) ;
244
+ impl OutputTensorInfo {
245
+ /// Sets the element type for output tensor info.
246
+ pub fn set_element_type ( & mut self , element_type : ElementType ) -> Result < ( ) > {
247
+ try_unsafe ! ( ov_preprocess_output_set_element_type(
268
248
self . ptr,
269
- std:: ptr:: addr_of_mut!( ptr)
270
- ) ) ?;
271
- Ok ( PreProcessOutputTensorInfo { ptr } )
249
+ element_type as u32
250
+ ) )
272
251
}
273
252
}
274
253
275
- impl PreProcessInputInfo {
276
- /// Retrieves the preprocessing model input information.
277
- pub fn get_model_info ( & self ) -> Result < PreProcessInputModelInfo > {
278
- let mut ptr = std:: ptr:: null_mut ( ) ;
279
- try_unsafe ! ( ov_preprocess_input_info_get_model_info(
254
+ /// See [`PreprocessSteps`](https://docs.openvino.ai/2023.3/api/c_cpp_api/structov__preprocess__preprocess__steps__t.html).
255
+ pub struct Steps {
256
+ ptr : * mut ov_preprocess_preprocess_steps_t ,
257
+ }
258
+ drop_using_function ! ( Steps , ov_preprocess_preprocess_steps_free) ;
259
+ impl Steps {
260
+ /// Resizes the data in a [`Tensor`].
261
+ pub fn resize ( & mut self , resize_algo : ResizeAlgorithm ) -> Result < ( ) > {
262
+ try_unsafe ! ( ov_preprocess_preprocess_steps_resize(
280
263
self . ptr,
281
- std:: ptr:: addr_of_mut!( ptr)
282
- ) ) ?;
283
- Ok ( PreProcessInputModelInfo { ptr } )
264
+ resize_algo as u32
265
+ ) )
284
266
}
285
267
286
- /// Retrieves the input tensor information.
287
- pub fn preprocess_input_info_get_tensor_info ( & self ) -> Result < PreProcessInputTensorInfo > {
288
- let mut ptr: * mut ov_preprocess_input_tensor_info_t = std:: ptr:: null_mut ( ) ;
289
- try_unsafe ! ( ov_preprocess_input_info_get_tensor_info(
268
+ /// Converts the [`Layout`] of the data in a [`Tensor`].
269
+ pub fn convert_layout ( & mut self , new_layout : & Layout ) -> Result < ( ) > {
270
+ try_unsafe ! ( ov_preprocess_preprocess_steps_convert_layout(
290
271
self . ptr,
291
- std:: ptr:: addr_of_mut!( ptr)
292
- ) ) ?;
293
- Ok ( PreProcessInputTensorInfo { ptr } )
272
+ new_layout. as_ptr( ) ,
273
+ ) )
294
274
}
295
275
296
- /// Retrieves preprocessing steps object.
297
- pub fn get_preprocess_steps ( & self ) -> Result < PreProcessSteps > {
298
- let mut ptr = std:: ptr:: null_mut ( ) ;
299
- try_unsafe ! ( ov_preprocess_input_info_get_preprocess_steps(
276
+ /// Converts the element type of data in tensor.
277
+ pub fn convert_element_type ( & mut self , new_element_type : ElementType ) -> Result < ( ) > {
278
+ try_unsafe ! ( ov_preprocess_preprocess_steps_convert_element_type(
300
279
self . ptr,
301
- std:: ptr:: addr_of_mut!( ptr)
302
- ) ) ?;
303
- Ok ( PreProcessSteps { ptr } )
280
+ new_element_type as u32
281
+ ) )
304
282
}
305
283
}
0 commit comments