|
1 | 1 | use log::{error, warn}; |
2 | | -use pyo3::prelude::*; |
| 2 | +use pyo3::{PyObject, prelude::*}; |
3 | 3 | use numpy::{self, ToPyArray}; |
4 | 4 |
|
5 | | -use crate::{data::{RgbFrame, Frame}, stages::{hook::{HookTrait, HookType}, stage::ContextTrait}, stages::stage::Context, kvs::store}; |
| 5 | +use crate::{data::{Frame, RgbFrame, UserData}, stages::{hook::{HookTrait, HookType}, stage::ContextTrait}, stages::stage::Context, kvs::store}; |
6 | 6 |
|
7 | 7 | /// Allows a Frame to be converted from Rust to Python |
8 | 8 | impl IntoPy<Py<PyAny>> for Frame { |
@@ -41,6 +41,7 @@ impl IntoPy<Py<PyAny>> for RgbFrame { |
41 | 41 | dict.set_item("inference_input", self.get_inference_input().to_pyarray(py)).unwrap(); |
42 | 42 | dict.set_item("inference_output", self.get_inference_output().to_pyarray(py)).unwrap(); |
43 | 43 | dict.set_item("pipeline_id", self.get_pipeline_id().to_string()).unwrap(); |
| 44 | + dict.set_item("user_data", self.get_user_data()).unwrap(); |
44 | 45 | dict.into() |
45 | 46 | } |
46 | 47 | } |
@@ -82,18 +83,84 @@ impl<'source> FromPyObject<'source> for RgbFrame { |
82 | 83 | let inference_input = inference_input_ndarray; |
83 | 84 | let inference_output =inference_output_ndarray; |
84 | 85 | let pipeline_id = ob.get_item("pipeline_id").unwrap().extract()?; |
| 86 | + let user_data = ob.get_item("user_data").unwrap().extract()?; |
85 | 87 |
|
86 | 88 | let frame = RgbFrame::from_values( |
87 | 89 | uuid, original, modified, width, height, |
88 | 90 | pts, dts, duration, fps, input_ts, |
89 | 91 | inference_input, inference_output, |
90 | | - pipeline_id, |
| 92 | + pipeline_id, user_data |
91 | 93 | ); |
92 | 94 |
|
93 | 95 | Ok(frame) |
94 | 96 | } |
95 | 97 | } |
96 | 98 |
|
| 99 | +/// Allows to pass the user data to python and back |
| 100 | +impl ToPyObject for UserData { |
| 101 | + fn to_object(&self, py: Python<'_>) -> PyObject { |
| 102 | + match self { |
| 103 | + UserData::Empty => py.None(), |
| 104 | + UserData::Integer(i) => i.into_py(py), |
| 105 | + UserData::Float(f) => f.into_py(py), |
| 106 | + UserData::String(s) => s.into_py(py), |
| 107 | + UserData::Array(arr) => { |
| 108 | + let list = pyo3::types::PyList::empty(py); |
| 109 | + for item in arr { |
| 110 | + list.append(item.to_object(py)).unwrap(); |
| 111 | + } |
| 112 | + list.into_py(py) |
| 113 | + } |
| 114 | + UserData::Dictionary(dict) => { |
| 115 | + let py_dict = pyo3::types::PyDict::new(py); |
| 116 | + for (key, value) in dict { |
| 117 | + py_dict.set_item(key, value.to_object(py)).unwrap(); |
| 118 | + } |
| 119 | + py_dict.into_py(py) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/// Allows to pass the user data to python and back |
| 126 | +impl<'source> FromPyObject<'source> for UserData { |
| 127 | + fn extract(obj: &'source PyAny) -> PyResult<Self> { |
| 128 | + if let Ok(integer) = obj.extract::<i32>() { |
| 129 | + Ok(UserData::Integer(integer)) |
| 130 | + } else if let Ok(float) = obj.extract::<f64>() { |
| 131 | + Ok(UserData::Float(float)) |
| 132 | + } else if let Ok(string) = obj.extract::<String>() { |
| 133 | + Ok(UserData::String(string)) |
| 134 | + } else if obj.is_instance_of::<pyo3::types::PyList>() { |
| 135 | + let array = obj.downcast::<pyo3::types::PyList>()?; |
| 136 | + let array_data = array.into_iter() |
| 137 | + .map(|elem| UserData::extract(elem)) |
| 138 | + .collect::<PyResult<Vec<UserData>>>()?; |
| 139 | + Ok(UserData::Array(array_data)) |
| 140 | + } else if obj.is_instance_of::<pyo3::types::PyDict>() { |
| 141 | + let dict = obj.downcast::<pyo3::types::PyDict>()?; |
| 142 | + let dict_keys = dict.keys(); |
| 143 | + let mut dict_items = Vec::new(); |
| 144 | + for key in dict_keys { |
| 145 | + let key_str = key.extract::<String>()?; |
| 146 | + let value = dict.get_item(key)?; |
| 147 | + match value { |
| 148 | + Some(v) => { |
| 149 | + let value_data = UserData::extract(v)?; |
| 150 | + dict_items.push((key_str, value_data)); |
| 151 | + }, |
| 152 | + None => { dict_items.push((key_str, UserData::Empty)); }, |
| 153 | + } |
| 154 | + } |
| 155 | + Ok(UserData::Dictionary(dict_items)) |
| 156 | + } else if obj.is_none() { |
| 157 | + Ok(UserData::Empty) |
| 158 | + } else { |
| 159 | + Err(pyo3::exceptions::PyTypeError::new_err("Unsupported data type assigned to 'user_data'. Please check in the Pipeless the supported types.")) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
97 | 164 | /// Python context to maintain within a stage |
98 | 165 | pub struct PythonStageContext { |
99 | 166 | context: Py<pyo3::types::PyDict>, |
|
0 commit comments