tfledge/
lib.rs

1//!
2//! # TFLedge
3//!
4//! Safe wrapper around TensorFlow Lite and libedgetpu
5//!
6//! This is used to interact with Coral edge TPUs and do ***\****machine learning***\**** and ***\****AI***\****.
7//!
8//! ```
9//! #use std::{fs::File, io::Read, time::Instant};
10//!
11//! #use tfledge::{list_devices, Error, Interpreter, Model};
12//!
13//! #fn main() -> Result<(), Error> {
14//!     // Load the TFLite model from a file
15//!     let m = Model::from_file("Note_Detector.tflite")?;
16//!
17//!     // Get the first edge TPU device
18//!     let d = list_devices().next().unwrap();
19//!     // Build an interpreter
20//!     let mut int = Interpreter::new(m, d).unwrap();
21//!
22//!     // Get a handle to input tensor 0
23//!     let mut input = int.input_tensor::<f32>(0);
24//!
25//!     assert_eq!(input.num_dims().unwrap(), 4);
26//!
27//!     let mut buf: Vec<u8> = Vec::new();
28//!     File::open("test.rgb")
29//!         .unwrap()
30//!         .read_to_end(&mut buf)
31//!         .unwrap();
32//!     input.write(&buf).unwrap();
33//!
34//!     println!("{}", input.num_dims().unwrap());
35//!     for dim in 0..input.num_dims().unwrap() {
36//!         println!("- {}", input.dim(dim));
37//!     }
38//!
39//!     for _ in 0..100_000 {
40//!         let st = Instant::now();
41//!         int.invoke()?;
42//!
43//!         let boxes = int.output_tensor::<f32>(1).read::<4>();
44//!         let classes = int.output_tensor::<f32>(3).read::<1>();
45//!         let scores = int.output_tensor::<f32>(0).read::<1>();
46//!
47//!         for aaa in boxes {
48//!             println!("{aaa:?}");
49//!         }
50//!
51//!         for aaa in classes {
52//!             println!("{aaa:?}");
53//!         }
54//!
55//!         for aaa in scores {
56//!             println!("{aaa:?}");
57//!         }
58//!
59//!         /*
60//!         for (label, output, chunksz) in [
61//!             ("boxes", int.output_tensor(1), 4),
62//!             ("classes", int.output_tensor(3), 1),
63//!             ("scores", int.output_tensor(0), 1),
64//!         ] {
65//!             println!("[{label}] ({:?})", output.kind());
66//!             println!("{}", output.num_dims());
67//!             for dim in 0..output.num_dims() {
68//!                 println!("- {}", output.dim(dim));
69//!             }
70//!
71//!             for aaa in output.read::<chunksz>() {
72//!                 println!("{aaa:?}");
73//!             }
74//!         }
75//!         */
76//!         println!("{:?}", st.elapsed());
77//!     }
78//!
79//!     Ok(())
80//! }
81//! ```
82//!
83
84#![no_std]
85#![allow(private_bounds)]
86
87extern crate alloc;
88extern crate core;
89
90#[macro_use]
91extern crate log;
92
93#[allow(nonstandard_style, dead_code)]
94pub(crate) mod ffi {
95    include!("gen.rs");
96}
97
98mod device;
99mod error;
100mod interpreter;
101mod model;
102mod tensor;
103
104pub use device::{list_devices, CoralDevice, CoralDeviceKind, CoralDeviceList};
105pub use error::Error;
106pub use interpreter::Interpreter;
107pub use model::Model;
108pub use tensor::{Input, Output, Tensor, TensorData};