(
#[data] state: State,
img: Image,
extra_data: String,
)
| 52 | #[post("/analyze/{extra_data}")] |
| 53 | #[openapi(summary = "analyze an image")] |
| 54 | async fn analyze( |
| 55 | #[data] state: State, |
| 56 | img: Image, |
| 57 | extra_data: String, |
| 58 | ) -> Result<Either<Image, impl Reply>, Rejection> { |
| 59 | let img = img.into_bgr8(); |
| 60 | let id = state.id(); |
| 61 | |
| 62 | let pyobject: PyObject = Python::with_gil(|py| -> PyResult<_> { |
| 63 | let data = img.as_raw(); |
| 64 | let ndarray = |
| 65 | data.to_pyarray(py) |
| 66 | .reshape([img.height() as usize, img.width() as usize, 3])?; |
| 67 | |
| 68 | Ok([ |
| 69 | ("data", ndarray.to_object(py)), |
| 70 | ("extra_data", extra_data.to_object(py)), |
| 71 | ] |
| 72 | .into_py_dict(py) |
| 73 | .into()) |
| 74 | }) |
| 75 | .map_err(reject_cause)?; |
| 76 | |
| 77 | let r = { |
| 78 | let (s, r) = oneshot::channel(); |
| 79 | state.mapping.lock().await.insert(id, s); |
| 80 | let envelope = Envelope::with_info( |
| 81 | pyobject, |
| 82 | EnvelopeInfo { |
| 83 | partial_id: Some(id), |
| 84 | ..Default::default() |
| 85 | }, |
| 86 | ); |
| 87 | state.out.send(envelope).await.ok(); |
| 88 | r |
| 89 | }; |
| 90 | let message = r.await.map_err(reject_cause)?; |
| 91 | match state.ty { |
| 92 | RespTy::Image => Python::with_gil(|py| -> PyResult<_> { |
| 93 | let dict: &PyDict = message.extract(py)?; |
| 94 | flow_rs::helper::uget_slice(py, dict.get_item("data").expect("error key<data>")) |
| 95 | }) |
| 96 | .map_err(reject_cause) |
| 97 | .map(|data| Image::from_raw(img.width(), img.height(), &data).unwrap()) |
| 98 | .map(Either::Left), |
| 99 | RespTy::Json => Python::with_gil(|py| message.extract::<String>(py)) |
| 100 | .map_err(reject_cause) |
| 101 | .map(BareJson::new) |
| 102 | .map(Either::Right), |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl ImageServer { |
| 107 | fn new(_: String, args: &Table) -> ImageServer { |
no test coverage detected