Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(camera-effects): add Linux ONNX Runtime dynamic loading

Richie McIlroy6535b57b59c9
diff --git a/crates/camera-effects/Cargo.toml b/crates/camera-effects/Cargo.toml
index c32c55a54..b2d999d42 100644
--- a/crates/camera-effects/Cargo.toml
+++ b/crates/camera-effects/Cargo.toml
@@ -13,7 +13,10 @@ tracing.workspace = true
bytemuck = { version = "1.7", features = ["derive"] }
ndarray = "0.16"
-[target.'cfg(not(any(target_os = "macos", target_os = "windows")))'.dependencies]
+[target.'cfg(target_os = "linux")'.dependencies]
+ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "load-dynamic", "api-23"] }
+
+[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))'.dependencies]
ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "download-binaries", "copy-dylibs", "tls-native"] }
[target.'cfg(target_os = "macos")'.dependencies]
diff --git a/crates/camera-effects/src/segmentation.rs b/crates/camera-effects/src/segmentation.rs
index e2d56601f..bf8a7ca56 100644
--- a/crates/camera-effects/src/segmentation.rs
+++ b/crates/camera-effects/src/segmentation.rs
@@ -1,9 +1,14 @@
use anyhow::Context;
use ort::session::Session;
use ort::value::Value;
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", target_os = "linux"))]
use std::path::PathBuf;
+#[cfg(target_os = "macos")]
+const ORT_LIBRARY_NAME: &str = "libonnxruntime.dylib";
+#[cfg(target_os = "linux")]
+const ORT_LIBRARY_NAME: &str = "libonnxruntime.so";
+
const MODEL_BYTES: &[u8] = include_bytes!("../assets/selfie_segmentation.onnx");
const MODEL_INPUT_SIZE: usize = 256;
@@ -85,7 +90,7 @@ fn create_session() -> anyhow::Result<Session> {
Ok(session)
}
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", target_os = "linux"))]
fn init_runtime() -> anyhow::Result<()> {
let path = std::env::var_os("ORT_DYLIB_PATH")
.map(PathBuf::from)
@@ -94,7 +99,7 @@ fn init_runtime() -> anyhow::Result<()> {
.into_iter()
.find(|path| path.exists())
})
- .context("Failed to find macOS ONNX Runtime dylib")?;
+ .context("Failed to find ONNX Runtime library")?;
let _ = ort::init_from(&path)
.with_context(|| format!("Failed to load ONNX Runtime from {}", path.display()))?
@@ -103,19 +108,19 @@ fn init_runtime() -> anyhow::Result<()> {
Ok(())
}
-#[cfg(not(target_os = "macos"))]
+#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn init_runtime() -> anyhow::Result<()> {
Ok(())
}
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", target_os = "linux"))]
fn onnx_runtime_candidates() -> Vec<PathBuf> {
let mut candidates = Vec::new();
if let Ok(exe) = std::env::current_exe()
&& let Some(exe_dir) = exe.parent()
{
- candidates.push(exe_dir.join("libonnxruntime.dylib"));
+ candidates.push(exe_dir.join(ORT_LIBRARY_NAME));
if let Some(contents_dir) = exe_dir.parent() {
candidates.push(
@@ -123,14 +128,15 @@ fn onnx_runtime_candidates() -> Vec<PathBuf> {
.join("Resources")
.join("onnxruntime")
.join("lib")
- .join("libonnxruntime.dylib"),
+ .join(ORT_LIBRARY_NAME),
);
}
}
candidates.push(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
- .join("../../target/native-deps/onnxruntime/lib/libonnxruntime.dylib"),
+ .join("../../target/native-deps/onnxruntime/lib")
+ .join(ORT_LIBRARY_NAME),
);
candidates