What's the ideal way to convert `opencv::cv::Mat` from rust to `numpy.ndarray` in python?
Nessuno ha ancora preso questa issue.
- Lingua principale
- Rust
- Stelle
- 1.4k
- Fork
- 141
- Merge medio
- 16m
- PR unite (30g)
- 3
Descrizione
I have a cv::Mat in Rust, but want to convey the data to Python, I have checked the doc and gone to PyArray3, but it requires a Vec<Vec<Vec<T>>>, which means I have to make up the Vec manually with one copy, and from PyArray3::from_vec3 it might clone another time, that's quite inefficient, what's the proper way to do that? Is there a way to prevent data clone?
#[test]
fn mat_2_numpy() -> PyResult<()> {
Python::with_gil(|py| {
println!("in gil");
let func: Py<PyAny> = PyModule::from_code(
py,
c_str!(
"import numpy as np
def call_np(arr):
print(\"Shape:\", arr.shape)
"
),
c_str!(""),
c_str!(""),
)?
.getattr("call_np")?
.into();
let img = opencv::imgcodecs::imread("/some/path/sample.jpg", 0).unwrap();
let shape = (img.rows(), img.cols(), img.channels());
// it requires Vec<Vec<Vec<T>>>
let array = PyArray3::from_vec3(py, v)?;
// pass object with Rust tuple of positional arguments
let args = (array,);
let engine_obj = func.call1(py, args)?;
Ok(())
})
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la chiamata PyArray3::from_vec3 e l’esempio opencv::imgcodecs::imread nell’issue. Determina se la conversione richiesta da Rust cv::Mat a numpy.ndarray può evitare il nesting di Vec e le copie descritte; il lavoro sarà considerato completato quando sarà disponibile un approccio supportato, concreto e documentato, oppure una limitazione chiarita.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- numpy, opencv, rust
- Ambito
- api
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100