Skip to content

Commit

Permalink
fix windows uft8 and gbk load error alibaba/MNN#3032
Browse files Browse the repository at this point in the history
  • Loading branch information
Baiyuetribe committed Sep 13, 2024
1 parent f11186c commit 26d7e3f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mnnrs"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
authors = ["baiyue"]
description = " Rust implementation of mnn, a lightweight neural network inference framework, with separated static library for cross-platform compilation."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() -> anyhow::Result<()> {
let mut out0 = net.get_output_tensor(&ex, "output")?; // 输出

// 预处理
let data = [1.460; 1 * 512 * 512 * 1]; // 假设有一些数据
let data = vec![1.460; 1 * 512 * 512 * 1]; // 假设有一些数据
in1.set_data(&data);

// 推理
Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ fn main() {
.allowlist_function("MNN.*")
.allowlist_var("MNN.*")
.allowlist_type("MNN.*")
// 忽略无用类型
.blocklist_type("_Base")
// 生成 Rust 代码中的构造函数
// .opaque_type("std::.*")
.opaque_type("std::vector")
Expand Down
15 changes: 4 additions & 11 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ impl Net {
}
// 模型加载
pub fn load_model(&mut self, path: &str) -> anyhow::Result<()> {
let c_str = {
#[cfg(target_os = "windows")]
{
let (gbk_bytes, _, _) = encoding_rs::GB18030.encode(path);
CString::new(gbk_bytes)?
}
#[cfg(not(target_os = "windows"))]
{
CString::new(path)?
}
};
let c_str = CString::new(path)?; // mnn底层支持utf8编码,Windows无需特殊转换
unsafe {
self.ptr = MNN_Interpreter_createFromFile(c_str.as_ptr());
if self.ptr.is_null() {
anyhow::bail!("Failed to load model");
}
}
Ok(())
}
Expand Down

0 comments on commit 26d7e3f

Please sign in to comment.