| 43 | async fn initialize(&mut self, _: ResourceCollection) {} |
| 44 | async fn finalize(&mut self) {} |
| 45 | async fn exec(&mut self, _: &Context) -> Result<()> { |
| 46 | let out = std::mem::take(&mut self.out); |
| 47 | let urls = std::mem::take(&mut self.urls); |
| 48 | let decode = rt::task::spawn(async move { |
| 49 | let mut partial_id = 0u64; |
| 50 | for path in &urls { |
| 51 | if Path::new(&path).is_dir() { |
| 52 | match fs::read_dir(&path) { |
| 53 | Err(why) => { |
| 54 | println!("{:?}", why.kind()); |
| 55 | break; |
| 56 | } |
| 57 | Ok(dirs) => { |
| 58 | for dir in dirs { |
| 59 | let filepath = &dir.unwrap().path(); |
| 60 | if Path::new(&filepath).is_file() { |
| 61 | // skip sub directory |
| 62 | let decode_img = ImageReader::open(&filepath) |
| 63 | .unwrap() |
| 64 | .decode() |
| 65 | .expect("decode image fail"); |
| 66 | let img = decode_img.into_bgr8(); |
| 67 | |
| 68 | let pyobject: PyObject = |
| 69 | Python::with_gil(|py| -> PyResult<_> { |
| 70 | let data = img.as_raw(); |
| 71 | let ndarray = data.to_pyarray(py).reshape([ |
| 72 | img.height() as usize, |
| 73 | img.width() as usize, |
| 74 | 3, |
| 75 | ])?; |
| 76 | |
| 77 | Ok([ |
| 78 | ("data", ndarray.to_object(py)), |
| 79 | ( |
| 80 | "extra_data", |
| 81 | Path::new(&filepath).file_stem().to_object(py), |
| 82 | ), |
| 83 | ] |
| 84 | .into_py_dict(py) |
| 85 | .into()) |
| 86 | }) |
| 87 | .unwrap(); |
| 88 | |
| 89 | let mut envelope = Envelope::new(pyobject); |
| 90 | envelope.info_mut().partial_id = Some(partial_id); |
| 91 | partial_id += 1; |
| 92 | out.send(envelope).await.ok(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | let inp = std::mem::take(&mut self.inp); |
| 102 | let receiver = |