Views
No views yet
npm i @huggingface/transformers1import { Sam3TrackerModel, AutoProcessor, RawImage } from "@huggingface/transformers";
2
3// Load model and processor
4const model_id = "onnx-community/sam3-tracker-ONNX";
5const model = await Sam3TrackerModel.from_pretrained(model_id);
6const processor = await AutoProcessor.from_pretrained(model_id);
7
8// Prepare image and input points/boxes
9const img_url = "https://huggingface.co/datasets/hf-internal-testing/sam2-fixtures/resolve/main/truck.jpg";
10const raw_image = await RawImage.read(img_url);
11
12const input_points = [[[[500, 375]]]];
13const input_labels = [[[1]]];
14const input_boxes = undefined; // e.g., [[[75, 275, 1725, 850]]];
15
16// Process inputs and perform mask generation
17const inputs = await processor(raw_image, { input_points, input_labels, input_boxes });
18const outputs = await model(inputs);
19
20// Post-process masks
21const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes);
22// Tensor {
23// data: Uint8Array(6480000) [ 0, 0, 0, ... ],
24// type: 'bool',
25// dims: [ 1, 3, 1200, 1800 ],
26// size: 6480000
27// }
28
29const scores = outputs.iou_scores;
30// Tensor {
31// data: Float32Array(3) [ 0.9313147068023682, 0.037515610456466675, 0.5128555297851562 ],
32// type: 'float32',
33// dims: [ 1, 1, 3 ],
34// size: 3
35// }
36
37// Visualize masks
38const image = RawImage.fromTensor(masks[0][0].mul(255));
39image.save("mask.png");