{"id":31769243,"url":"https://github.com/second-state/rust-wasm-ai-demo","last_synced_at":"2025-10-18T09:17:24.138Z","repository":{"id":45036785,"uuid":"262669850","full_name":"second-state/rust-wasm-ai-demo","owner":"second-state","description":"Rust functions for Tensorflow inference in Node.js. Rust's performance, WebAssembly's security and portability, and Javascript's ease-of-use.","archived":false,"fork":false,"pushed_at":"2020-08-07T07:24:31.000Z","size":27154,"stargazers_count":55,"open_issues_count":1,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-07T18:57:34.615Z","etag":null,"topics":["getting-started","node-js","nodejs","rust","rust-lang","ssvm","ssvmup","tensorflow","wasm","webassembly"],"latest_commit_sha":null,"homepage":"https://www.secondstate.io/articles/artificial-intelligence/","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/second-state.png","metadata":{"files":{"readme":"README-ZH.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-09T22:26:11.000Z","updated_at":"2025-01-15T08:13:17.000Z","dependencies_parsed_at":"2022-09-12T10:51:34.975Z","dependency_job_id":null,"html_url":"https://github.com/second-state/rust-wasm-ai-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/second-state/rust-wasm-ai-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Frust-wasm-ai-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Frust-wasm-ai-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Frust-wasm-ai-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Frust-wasm-ai-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/second-state","download_url":"https://codeload.github.com/second-state/rust-wasm-ai-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Frust-wasm-ai-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002541,"owners_count":26083403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["getting-started","node-js","nodejs","rust","rust-lang","ssvm","ssvmup","tensorflow","wasm","webassembly"],"created_at":"2025-10-10T02:42:16.742Z","updated_at":"2025-10-10T02:42:24.633Z","avatar_url":"https://github.com/second-state.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tensorflow 图像识别案例\n\n\u003cp\u003e\n    \u003ca href=\"https://online.visualstudio.com/environments/new?name=AIaaS%20with%20Rust%20and%20WebAssembly\u0026repo=second-state/rust-wasm-ai-demo\"\u003e\n        \u003cimg src=\"https://img.shields.io/endpoint?style=social\u0026url=https%3A%2F%2Faka.ms%2Fvso-badge\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n在这个例子中，我们演示了如何在 Node.js 中实现高性能的 AI 推理。 计算密集型的 tensorflow 代码是用 Rust 编写的，并在 WebAssembly 中执行。 使用图像识别的面向用户的应用程序是用 JavaScript 编写的，并运行在 Node.js 中。\n\n![AI as a service](https://camo.githubusercontent.com/7ee97417b1f6fd51660ba64ec64a7dc70f51e851/68747470733a2f2f626c6f672e7365636f6e6473746174652e696f2f696d616765732f414961617325323033307365636f6e64732e676966)\n\n\u003e 你可以在 VSCode 或者 VS Codespaces 中 [fork](https://github.com/second-state/csdn-ai-demo/fork) 并打开这个 git 库。 使用 VS Codespaces，您可以在浏览器中编写代码、编译、运行和调试应用程序，而无需安装任何软件。 参见 https://github.com/second-state/ssvm-nodejs-starter/\n\n## 设置\n\n```\n$ sudo apt-get update\n$ sudo apt-get -y upgrade\n$ sudo apt install build-essential curl wget git vim libboost-all-dev\n\n$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\n$ source $HOME/.cargo/env\n\n$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash\n$ [ -s \"$NVM_DIR/nvm.sh\" ] \u0026\u0026 \\. \"$NVM_DIR/nvm.sh\"\n$ [ -s \"$NVM_DIR/bash_completion\" ] \u0026\u0026 \\. \"$NVM_DIR/bash_completion\"\n\n$ nvm install v10.19.0\n$ nvm use v10.19.0\n\n$ npm install -g ssvmup # Append --unsafe-perm if permission denied\n$ npm install ssvm\n```\n\n## 创建新项目\n\n```\n$ cargo new --lib tensorflow\n$ cd tensorflow\n```\n\n## 修改cargo config 文件\n\nThe [Cargo.toml](Cargo.toml) 文件显示依赖项.\n\n* The `wasm-bindgen` crate is required for invoking Rust functions from JavaScript. \n* The `serde` and `serde_json` crates allow us to work with JSON strings to represent complex data types. \n* The `images` crate only enables features that are compatible with WebAssembly.\n\n## 写 Rust 代码\n\n [src/lib.rs](src/lib.rs) 文件包含 Rust 函数从文件中读取 tensorflow 模型，读取并调整图像大小，然后根据图像运行模型来识别图像主题。 结果作为 JSON 数组返回，其中包含识别对象的 ImageNet 类别 ID 和此预测的置信度。[了解更多](https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet) 有关这个案例。\n\n## 创建 WASM bytecode\n\n```\n$ ssvmup build\n```\n\n## 创建一个node app\n\n[node/app.js](node/app.js) app 展示了如何从 JavaScript 中调用 Rust 函数。它使用了一个[预先训练的 tensorflow 模型](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz) 来识别两个图像。\n\n## 测试\n\n```\n$ node app.js\n```\n\n第一个任务是识别计算机科学家格蕾丝 · 霍珀的图像，识别这个图像需要0.9秒。\n\n\n```\nModel: \"mobilenet_v2_1.4_224_frozen.pb\"\nImage: \"grace_hopper.jpg\"\nInference: 131.783ms Model loaded\nInference: 367.625ms Plan loaded\nInference: 391.095ms Image loaded\nInference: 427.137ms Image resized\nInference: 1322.184ms Model applied\nInference: 1322.637ms\nDetected object id 654 with probability 0.3256046\n```\n\nCategory ID `654` 可以在 [imagenet_slim_labels.txt](imagenet_slim_labels.txt) 找到。 行数 `654`.\n\n```\n654 军装\n```\n\n第二个任务是识别一张猫的图像，识别这张图像需要0.8秒。\n\n```\nModel: \"mobilenet_v2_1.4_224_frozen.pb\"\nImage: \"cat.png\"\nInference: 86.587ms Model loaded\nInference: 314.308ms Plan loaded\nInference: 842.836ms Image loaded\nInference: 1166.115ms Image resized\nInference: 2014.337ms Model applied\nInference: 2014.602ms\nDetected object id 284 with probability 0.27039126\n```\n\nCategory ID `284` 可以在这里找到 [imagenet_slim_labels.txt](imagenet_slim_labels.txt). 行数 `284`.\n\n```\n284 虎猫\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsecond-state%2Frust-wasm-ai-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsecond-state%2Frust-wasm-ai-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsecond-state%2Frust-wasm-ai-demo/lists"}