Hi,
I am a very beginner in Rust (coming from python) and while I can see how clean and robust rust requires the code to be I still have trouble to play with some of the basic concept.
I want to "transfer" an array from python (a 3D array using numpy with a shape (14, 484, 636)) to a Array3 in rust to do some manipulation onto it. It has been suggested to me to create a JSON file in python containing the information encoded in base64 and reopen and reconstruct my array in Rust.
I have been able to serialize my data (an image array of shape (25, 25) for a start) in a JSON file in python (I put the code down here for clarity/suggestions):
import json
import base64
base64_study = base64.b64encode(arr)
base64_string = base64_study.decode("utf-8")
json_dict = {
"height": arr.shape[1],
"width": arr.shape[2],
"nb_frames": arr.shape[0],
"image_data": base64_string,
}
with open("dummy_data.json", "w") as outfile:
json.dump(json_dict, outfile)
But then when I try to deserialize my data in Rust:
use base64;
use serde::{Deserialize, Serialize};
use serde_json::{from_reader, from_str};
#[derive(Debug, Deserialize, Serialize)]
struct Study {
height: u16,
width: u16,
image_data: String,
nb_frames: u16
}
fn main() {
let file = File::open("dummy_data.json").unwrap();
let study: Study = serde_json::from_reader(&file).expect("JSON was not well-formatted");
let img_data: Vec<u8> = base64::decode(study.image_data).unwrap();
println!("{:?}", img_data.len());
}
>> cargo run
>> 5000
But I get an immense array with way more 0s than expected (with a length 5000 instead of 625) ! I am not too sure what's happening.
Then the next question is to know how to reshape the current flatten array to the original shape (25 x 25).
Thank you very much for any help you can provide !