-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_boxes.js
140 lines (120 loc) · 4.17 KB
/
draw_boxes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import axios from "axios";
import sharp from "sharp";
import { SamModel, AutoProcessor, RawImage } from "@xenova/transformers";
async function fetchModelOutput(url, params) {
try {
const response = await axios.get(url, { params });
if (response.status === 200) {
return response.data;
} else {
console.error(
`Request failed with status code ${response.status}: ${response.data}`
);
return null;
}
} catch (error) {
console.error("An error occurred during the GET request:", error);
return null;
}
}
const doSegment = async (boxes, image_uri) => {
try {
const image = await RawImage.read(image_uri);
// Load SAM model and processor
const model = await SamModel.from_pretrained("Xenova/sam-vit-base");
const processor = await AutoProcessor.from_pretrained(
"Xenova/sam-vit-base"
);
const masks = [];
// Process each bounding box
for (let i = 0; i < boxes.length; i++) {
const [xmin, ymin, xmax, ymax] = boxes[i];
const centerX = Math.floor((xmin + xmax) / 2);
const centerY = Math.floor((ymin + ymax) / 2);
const input_points = [[[centerX, centerY]]];
const inputs = await processor(image, input_points);
const outputs = await model(inputs);
const processedMasks = await processor.post_process_masks(
outputs.pred_masks,
inputs.original_sizes,
inputs.reshaped_input_sizes
);
// Store mask data for canvas rendering
masks.push({
mask: processedMasks[0][0],
box: { xmin, ymin, xmax, ymax },
});
//save the mask to a file
const masked_image = RawImage.fromTensor(processedMasks[0][0].mul(255));
masked_image.save(`mask_${i}.png`);
}
return masks;
} catch (error) {
console.error("Error in segmentation:", error);
}
};
async function drawBoxes() {
// URL of the model endpoint
const url = "http://localhost:3001/";
const params = {
model_name: "object-detection",
text: "tree",
image_uri:
"https://content.satimagingcorp.com/static/galleryimages/Satellite-Image-Paris-Pont-des-Arts-bridge.jpg",
};
// Fetch the model output
const modelOutput = await fetchModelOutput(url, params);
const boxex = modelOutput.map((detection) => {
return Object.values(detection.box);
});
await doSegment(boxex, params.image_uri);
if (modelOutput) {
console.log("Model output fetched successfully!");
try {
// Load the image from the provided URI
const response = await axios.get(params.image_uri, {
responseType: "arraybuffer",
});
const image = sharp(response.data);
// Get image metadata
const metadata = await image.metadata();
const { width = 0, height = 0 } = metadata;
// Create SVG with bounding boxes
const svgString = `
<svg width="${width}" height="${height}">
${modelOutput
.map((detection) => {
const { box, score = 0, label = "Object" } = detection;
const { xmin, ymin, xmax, ymax } = box;
return `
<rect x="${xmin}" y="${ymin}"
width="${xmax - xmin}" height="${ymax - ymin}"
fill="none" stroke="lime" stroke-width="2"/>
<text x="${xmin}" y="${ymin - 5}"
font-family="Arial" font-size="16" fill="lime">
${label} ${score.toFixed(2)}
</text>
`;
})
.join("")}
</svg>`;
// Composite the SVG onto the image
await image
.composite([
{
input: Buffer.from(svgString),
top: 0,
left: 0,
},
])
.toFile("output_with_boxes1.jpg");
console.log("Output saved to output_with_boxes.jpg");
} catch (error) {
console.error("Error processing image:", error);
}
} else {
console.log("Failed to retrieve model output.");
}
}
// Run the script
drawBoxes().catch(console.error);