使用transformer.js在浏览器中运行yolov9模型
安装
npm i @xenova/transformers代码
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
// Load model
const model = await AutoModel.from_pretrained('Xenova/yolov9-c', {
// quantized: false, // (Optional) Use unquantized version.
})
// Load processor
const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-c');
// processor.feature_extractor.do_resize = false; // (Optional) Disable resizing
// processor.feature_extractor.size = { width: 128, height: 128 } // (Optional) Update resize value
// Read image and run processor
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
const image = await RawImage.read(url);
const { pixel_values } = await processor(image);
// Run object detection
const { outputs } = await model({ images: pixel_values })
const predictions = outputs.tolist();
for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
const bbox = [xmin, ymin, xmax, ymax].map(x => x.toFixed(2)).join(', ')
console.log(`Found "${model.config.id2label[id]}" at [${bbox}] with score ${score.toFixed(2)}.`)
}
// Found "car" at [176.86, 335.53, 399.82, 418.13] with score 0.94.
// Found "car" at [447.50, 378.46, 639.81, 477.57] with score 0.93.
// Found "bicycle" at [351.90, 527.82, 463.50, 587.76] with score 0.90.
// Found "person" at [472.44, 430.52, 533.74, 533.30] with score 0.89.
// Found "bicycle" at [448.97, 477.34, 555.42, 537.63] with score 0.88.
// Found "bicycle" at [0.59, 518.69, 109.53, 584.31] with score 0.88.
// Found "traffic light" at [208.55, 55.80, 233.99, 101.63] with score 0.86.
// Found "person" at [550.75, 260.98, 591.90, 331.24] with score 0.86.
// ...在线例子:https://xenova-yolov9-web.static.hf.space/index.html
https://huggingface.co/Xenova/yolov9-c
https://github.com/WongKinYiu/yolov9
网友回复
ai能写出比黑客还厉害的零日漏洞等攻击工具攻击任意软件系统工程?
js如何获取浏览器的音频上下文指纹、Canvas指纹、WebGL渲染特征?
为啥ai开始抛弃markdown文本,重新偏好html文本了?
网站有没有办法鉴别访问请求是由ai操控chrome-devtools-mcp发出的?
有没有python自动操作浏览器让网站无法鉴别是机器行为?
为啥最新由Meta / 斯坦福 / 哈佛出的ProgramBench基准GPT-5.4、Claude Opus 4.7、Gemini 3.1 Pro 等全部 0% 通过率?
有没有免费的api查询域名是否完成icp工信部备案?
codex用HyperFrames与 Remotion自动做视频那个更好?
claude code中Skill MCP CLI SubAgent Hooks Plugin区别?
浏览器webrtc点对点通讯如何才能走系统代理?


