Fix build errors: ort error Send/Sync bound, ndarray version unification, inputs! macro

Verified with cargo check.
This commit is contained in:
2026-08-28 17:09:15 +00:00
parent 17f8e4b487
commit 94b827613d
4 changed files with 19 additions and 24 deletions
+2 -1
View File
@@ -13,7 +13,7 @@ path = "src/main.rs"
tokio = { version = "1.40", features = ["full"] } tokio = { version = "1.40", features = ["full"] }
redis = { version = "0.27", features = ["tokio-comp", "streams"] } redis = { version = "0.27", features = ["tokio-comp", "streams"] }
ort = { version = "2.0.0-rc.9", features = ["ndarray", "download-binaries"] } ort = { version = "2.0.0-rc.9", features = ["ndarray", "download-binaries"] }
ndarray = "0.16" ndarray = "0.17"
image = "0.25" image = "0.25"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
@@ -25,6 +25,7 @@ base64 = "0.22"
sqlx = { version = "0.8", features = ["runtime-tokio", "postgres", "chrono", "uuid"] } sqlx = { version = "0.8", features = ["runtime-tokio", "postgres", "chrono", "uuid"] }
uuid = { version = "1", features = ["v4", "serde"] } uuid = { version = "1", features = ["v4", "serde"] }
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
dotenvy = "0.15"
[profile.release] [profile.release]
opt-level = 3 opt-level = 3
+13 -5
View File
@@ -31,10 +31,18 @@ pub struct FailureDetector {
impl FailureDetector { impl FailureDetector {
pub fn load(model_path: &str, input_size: u32, confidence_threshold: f32, intra_threads: usize) -> anyhow::Result<Arc<Self>> { pub fn load(model_path: &str, input_size: u32, confidence_threshold: f32, intra_threads: usize) -> anyhow::Result<Arc<Self>> {
let session = Session::builder()? // ort's builder error types carry raw FFI pointers that aren't
.with_optimization_level(GraphOptimizationLevel::Level3)? // Send/Sync, so they can't flow through `?` into `anyhow::Result`
.with_intra_threads(intra_threads)? // directly (anyhow requires Send + Sync + 'static). Stringify at
.commit_from_file(model_path)?; // the boundary instead.
let session = Session::builder()
.map_err(|e| anyhow::anyhow!("failed to create ONNX Runtime session builder: {e}"))?
.with_optimization_level(GraphOptimizationLevel::Level3)
.map_err(|e| anyhow::anyhow!("failed to set graph optimization level: {e}"))?
.with_intra_threads(intra_threads)
.map_err(|e| anyhow::anyhow!("failed to set intra-op thread count: {e}"))?
.commit_from_file(model_path)
.map_err(|e| anyhow::anyhow!("failed to load ONNX model at {model_path}: {e}"))?;
Ok(Arc::new(Self { Ok(Arc::new(Self {
session: Mutex::new(session), session: Mutex::new(session),
@@ -50,7 +58,7 @@ impl FailureDetector {
let input = Value::from_array(tensor)?; let input = Value::from_array(tensor)?;
let mut session = self.session.lock().await; let mut session = self.session.lock().await;
let outputs = session.run(ort::inputs!["images" => input]?)?; let outputs = session.run(ort::inputs!["images" => input])?;
// Exported with NMS baked into the graph: output0 is [1, N, 6] // Exported with NMS baked into the graph: output0 is [1, N, 6]
// rows of (x1, y1, x2, y2, confidence, class_id) in input-pixel space. // rows of (x1, y1, x2, y2, confidence, class_id) in input-pixel space.
+1 -5
View File
@@ -1,4 +1,4 @@
use image::{DynamicImage, GenericImageView}; use image::DynamicImage;
use ndarray::Array4; use ndarray::Array4;
/// Resizes an RGB frame to a square `size x size` input tensor in CHW layout, /// Resizes an RGB frame to a square `size x size` input tensor in CHW layout,
@@ -18,7 +18,3 @@ pub fn to_input_tensor(image: &DynamicImage, size: u32) -> Array4<f32> {
tensor tensor
} }
pub fn frame_dimensions(image: &DynamicImage) -> (u32, u32) {
image.dimensions()
}
+3 -13
View File
@@ -132,18 +132,8 @@ async fn process_entry(
Ok(()) Ok(())
} }
/// Loads `.env` if present; a missing file is fine (env vars may already be
/// set by the process supervisor), so the error is deliberately ignored.
fn dotenvy_load() { fn dotenvy_load() {
if let Ok(contents) = std::fs::read_to_string(".env") { let _ = dotenvy::dotenv();
for line in contents.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some((key, value)) = line.split_once('=') {
if std::env::var(key).is_err() {
std::env::set_var(key, value);
}
}
}
}
} }