@rosepetal/node-red-contrib-image-tools 1.4.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node-red-contrib-image-tools/lib/cpp-bridge.js +4 -4
- package/node-red-contrib-image-tools/lib/inference-transform.js +74 -12
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +4 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.html +1 -1
- package/package.json +1 -1
- package/rosepetal-image-engine/src/add-bbs.cpp +37 -18
|
@@ -173,14 +173,14 @@ function tryBuildFromSource() {
|
|
|
173
173
|
function loadAddon() {
|
|
174
174
|
const platformId = detectPlatform();
|
|
175
175
|
|
|
176
|
-
// 1. Try
|
|
177
|
-
let addon =
|
|
176
|
+
// 1. Try local build first (development / install.sh)
|
|
177
|
+
let addon = tryLoadFromLocalBuild();
|
|
178
178
|
if (addon) {
|
|
179
179
|
return addon;
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
// 2. Try
|
|
183
|
-
addon =
|
|
182
|
+
// 2. Try platform-specific package (npm install without build tools)
|
|
183
|
+
addon = tryLoadFromPackage(platformId);
|
|
184
184
|
if (addon) {
|
|
185
185
|
return addon;
|
|
186
186
|
}
|
|
@@ -38,6 +38,55 @@ function shoelaceArea(polygon) {
|
|
|
38
38
|
return Math.abs(a) / 2;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
// ─── Box Format Conversion ──────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
/** xywhr (top-left origin, rotation in degrees) → 4 corners [TL, TR, BR, BL] */
|
|
44
|
+
function xywhrToCorners(x, y, w, h, r) {
|
|
45
|
+
const rad = r * Math.PI / 180;
|
|
46
|
+
const cos = Math.cos(rad), sin = Math.sin(rad);
|
|
47
|
+
return [
|
|
48
|
+
[x, y],
|
|
49
|
+
[x + w * cos, y + w * sin],
|
|
50
|
+
[x + w * cos - h * sin, y + w * sin + h * cos],
|
|
51
|
+
[x - h * sin, y + h * cos]
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** cwh (center origin, rotation in degrees) → 4 corners [TL, TR, BR, BL] */
|
|
56
|
+
function cwhToCorners(cx, cy, w, h, r) {
|
|
57
|
+
const rad = r * Math.PI / 180;
|
|
58
|
+
const cos = Math.cos(rad), sin = Math.sin(rad);
|
|
59
|
+
const hw = w / 2, hh = h / 2;
|
|
60
|
+
return [
|
|
61
|
+
[cx - hw * cos + hh * sin, cy - hw * sin - hh * cos],
|
|
62
|
+
[cx + hw * cos + hh * sin, cy + hw * sin - hh * cos],
|
|
63
|
+
[cx + hw * cos - hh * sin, cy + hw * sin + hh * cos],
|
|
64
|
+
[cx - hw * cos - hh * sin, cy - hw * sin + hh * cos]
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Ensure a detection object has `raw_boxes` by converting from whichever
|
|
70
|
+
* format is available. Priority: raw_boxes > 4points > xywhr > cwh > 2points.
|
|
71
|
+
*/
|
|
72
|
+
function ensureRawBoxes(det) {
|
|
73
|
+
if (det.raw_boxes && Array.isArray(det.raw_boxes) && det.raw_boxes.length === 4) return;
|
|
74
|
+
|
|
75
|
+
if (det['4points']) {
|
|
76
|
+
const p = det['4points'];
|
|
77
|
+
det.raw_boxes = [[p.x0, p.y0], [p.x1, p.y1], [p.x2, p.y2], [p.x3, p.y3]];
|
|
78
|
+
} else if (det.xywhr) {
|
|
79
|
+
const { x, y, w, h, r } = det.xywhr;
|
|
80
|
+
det.raw_boxes = xywhrToCorners(x, y, w, h, r);
|
|
81
|
+
} else if (det.cwh) {
|
|
82
|
+
const { x, y, w, h, r } = det.cwh;
|
|
83
|
+
det.raw_boxes = cwhToCorners(x, y, w, h, r);
|
|
84
|
+
} else if (det['2points']) {
|
|
85
|
+
const { xmin, ymin, xmax, ymax } = det['2points'];
|
|
86
|
+
det.raw_boxes = [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
41
90
|
/**
|
|
42
91
|
* Re-sort 4 corner points into canonical clockwise order: TL, TR, BR, BL.
|
|
43
92
|
* After geometric transforms (rotation, etc.) the point order may change.
|
|
@@ -63,17 +112,24 @@ function reorderBoxPoints(points) {
|
|
|
63
112
|
* box formats that were present on the original detection.
|
|
64
113
|
*/
|
|
65
114
|
function recalculateBoxFormats(det) {
|
|
66
|
-
//
|
|
67
|
-
|
|
115
|
+
// Do NOT reorder points — edge topology must be preserved for oriented boxes.
|
|
116
|
+
// Point 0→1 is always the width edge, point 0→3 is the height edge.
|
|
68
117
|
const rb = det.raw_boxes; // [[x0,y0],[x1,y1],[x2,y2],[x3,y3]]
|
|
69
118
|
|
|
70
|
-
// 2points — axis-aligned
|
|
119
|
+
// 2points — only valid for axis-aligned boxes; remove if box is oriented
|
|
71
120
|
if (det.hasOwnProperty('2points')) {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
121
|
+
const dx = rb[1][0] - rb[0][0];
|
|
122
|
+
const dy = rb[1][1] - rb[0][1];
|
|
123
|
+
const isAxisAligned = Math.abs(dy) < 1e-6 && Math.abs(rb[0][1] - rb[1][1]) < 1e-6;
|
|
124
|
+
if (isAxisAligned) {
|
|
125
|
+
const xs = rb.map(p => p[0]), ys = rb.map(p => p[1]);
|
|
126
|
+
det['2points'] = {
|
|
127
|
+
xmin: Math.min(...xs), ymin: Math.min(...ys),
|
|
128
|
+
xmax: Math.max(...xs), ymax: Math.max(...ys)
|
|
129
|
+
};
|
|
130
|
+
} else {
|
|
131
|
+
delete det['2points'];
|
|
132
|
+
}
|
|
77
133
|
}
|
|
78
134
|
|
|
79
135
|
// 4points — flat
|
|
@@ -142,6 +198,9 @@ function overlapFraction(rawBoxes, cropRegion) {
|
|
|
142
198
|
* @returns {object|null} transformed detection or null if filtered out
|
|
143
199
|
*/
|
|
144
200
|
function transformDetection(det, pointFn, opts = {}) {
|
|
201
|
+
// Convert any input format to raw_boxes if not already present
|
|
202
|
+
ensureRawBoxes(det);
|
|
203
|
+
|
|
145
204
|
// Check overlap before cloning (optimization)
|
|
146
205
|
if (opts.cropRegion) {
|
|
147
206
|
const frac = overlapFraction(det.raw_boxes, opts.cropRegion);
|
|
@@ -234,9 +293,9 @@ function makeRotateTransform(params) {
|
|
|
234
293
|
return { pointFn: (x, y) => [x, y], opts: {}, maskTransformFn: null };
|
|
235
294
|
}
|
|
236
295
|
if (near(90)) {
|
|
237
|
-
//
|
|
296
|
+
// C++ uses cv::ROTATE_90_COUNTERCLOCKWISE for angle=90
|
|
238
297
|
return {
|
|
239
|
-
pointFn: (x, y) => [1 -
|
|
298
|
+
pointFn: (x, y) => [y, 1 - x],
|
|
240
299
|
opts: {},
|
|
241
300
|
maskTransformFn: async (mask, cppBridge) => {
|
|
242
301
|
const { image } = await cppBridge.rotate(mask, 90, '#00000000', 'raw', 90, false);
|
|
@@ -255,9 +314,9 @@ function makeRotateTransform(params) {
|
|
|
255
314
|
};
|
|
256
315
|
}
|
|
257
316
|
if (near(270)) {
|
|
258
|
-
//
|
|
317
|
+
// C++ uses cv::ROTATE_90_CLOCKWISE for angle=270
|
|
259
318
|
return {
|
|
260
|
-
pointFn: (x, y) => [
|
|
319
|
+
pointFn: (x, y) => [1 - y, x],
|
|
261
320
|
opts: {},
|
|
262
321
|
maskTransformFn: async (mask, cppBridge) => {
|
|
263
322
|
const { image } = await cppBridge.rotate(mask, 270, '#00000000', 'raw', 90, false);
|
|
@@ -606,6 +665,9 @@ module.exports = {
|
|
|
606
665
|
reorderBoxPoints,
|
|
607
666
|
recalculateBoxFormats,
|
|
608
667
|
overlapFraction,
|
|
668
|
+
ensureRawBoxes,
|
|
669
|
+
xywhrToCorners,
|
|
670
|
+
cwhToCorners,
|
|
609
671
|
|
|
610
672
|
// Core transforms
|
|
611
673
|
transformDetection,
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
const { performance } = require('perf_hooks');
|
|
11
11
|
const Cpp = require('../../lib/cpp-bridge.js');
|
|
12
|
+
const InfTx = require('../../lib/inference-transform.js');
|
|
12
13
|
|
|
13
14
|
module.exports = function (RED) {
|
|
14
15
|
const NodeUtils = require('../../lib/node-utils.js')(RED);
|
|
@@ -123,6 +124,9 @@ module.exports = function (RED) {
|
|
|
123
124
|
);
|
|
124
125
|
}
|
|
125
126
|
|
|
127
|
+
// Convert any box format (xywhr, cwh, 4points, 2points) to raw_boxes
|
|
128
|
+
InfTx.ensureRawBoxes(boxObj);
|
|
129
|
+
|
|
126
130
|
if (!boxObj.hasOwnProperty('raw_boxes') || !Array.isArray(boxObj.raw_boxes)) {
|
|
127
131
|
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
128
132
|
node, `Invalid box object at index ${boxIndex}: 'raw_boxes' property must be an array`, msg, send, done,
|
package/package.json
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
|
|
13
13
|
// Structure for bounding box information
|
|
14
14
|
struct BBoxInfo {
|
|
15
|
-
cv::Rect boundingRect;
|
|
15
|
+
cv::Rect boundingRect; // Axis-aligned bounding rect (for label positioning)
|
|
16
|
+
std::vector<cv::Point> cornersPx; // 4 oriented corner points in pixels (TL, TR, BR, BL)
|
|
16
17
|
std::string className;
|
|
17
18
|
float confidence;
|
|
18
19
|
cv::Scalar bgrColor; // Pre-computed BGR color (0-255)
|
|
@@ -197,22 +198,29 @@ public:
|
|
|
197
198
|
color[2] * 255.0f // R
|
|
198
199
|
);
|
|
199
200
|
|
|
200
|
-
// Convert normalized
|
|
201
|
+
// Convert normalized corners to pixel coordinates
|
|
202
|
+
std::vector<cv::Point> pixelCorners;
|
|
203
|
+
pixelCorners.reserve(4);
|
|
201
204
|
float minX = corners[0].x, maxX = corners[0].x;
|
|
202
205
|
float minY = corners[0].y, maxY = corners[0].y;
|
|
203
|
-
for (int i =
|
|
206
|
+
for (int i = 0; i < 4; i++) {
|
|
207
|
+
int px = static_cast<int>(corners[i].x * imageMat.cols);
|
|
208
|
+
int py = static_cast<int>(corners[i].y * imageMat.rows);
|
|
209
|
+
px = std::max(0, std::min(imageMat.cols - 1, px));
|
|
210
|
+
py = std::max(0, std::min(imageMat.rows - 1, py));
|
|
211
|
+
pixelCorners.emplace_back(px, py);
|
|
204
212
|
minX = std::min(minX, corners[i].x);
|
|
205
213
|
maxX = std::max(maxX, corners[i].x);
|
|
206
214
|
minY = std::min(minY, corners[i].y);
|
|
207
215
|
maxY = std::max(maxY, corners[i].y);
|
|
208
216
|
}
|
|
217
|
+
|
|
218
|
+
// Axis-aligned bounding rect (for label positioning)
|
|
209
219
|
cv::Rect bbox;
|
|
210
220
|
bbox.x = static_cast<int>(minX * imageMat.cols);
|
|
211
221
|
bbox.y = static_cast<int>(minY * imageMat.rows);
|
|
212
222
|
bbox.width = static_cast<int>((maxX - minX) * imageMat.cols);
|
|
213
223
|
bbox.height = static_cast<int>((maxY - minY) * imageMat.rows);
|
|
214
|
-
|
|
215
|
-
// Clamp to image bounds
|
|
216
224
|
bbox.x = std::max(0, std::min(imageMat.cols - 1, bbox.x));
|
|
217
225
|
bbox.y = std::max(0, std::min(imageMat.rows - 1, bbox.y));
|
|
218
226
|
bbox.width = std::min(bbox.width, imageMat.cols - bbox.x);
|
|
@@ -234,6 +242,7 @@ public:
|
|
|
234
242
|
// Store bbox info with pre-computed values
|
|
235
243
|
BBoxInfo info;
|
|
236
244
|
info.boundingRect = bbox;
|
|
245
|
+
info.cornersPx = pixelCorners;
|
|
237
246
|
info.className = className;
|
|
238
247
|
info.confidence = confidence;
|
|
239
248
|
info.bgrColor = bgrColor;
|
|
@@ -308,29 +317,39 @@ protected:
|
|
|
308
317
|
|
|
309
318
|
// Process each bounding box
|
|
310
319
|
for (const auto& bbox : bboxInfos) {
|
|
311
|
-
// Draw bounding box
|
|
312
|
-
cv::
|
|
320
|
+
// Draw oriented bounding box as a closed polygon
|
|
321
|
+
const cv::Point* pts = bbox.cornersPx.data();
|
|
322
|
+
const int npts = static_cast<int>(bbox.cornersPx.size());
|
|
323
|
+
cv::polylines(result, &pts, &npts, 1, true, bbox.bgrColor, actualBoxThickness, cv::LINE_4);
|
|
313
324
|
|
|
314
325
|
// Draw label if there's text
|
|
315
326
|
if (!bbox.labelText.empty()) {
|
|
316
327
|
|
|
317
|
-
//
|
|
318
|
-
|
|
319
|
-
|
|
328
|
+
// Find the topmost corner (smallest Y) for label anchor
|
|
329
|
+
int topIdx = 0;
|
|
330
|
+
for (int i = 1; i < npts; i++) {
|
|
331
|
+
if (bbox.cornersPx[i].y < bbox.cornersPx[topIdx].y ||
|
|
332
|
+
(bbox.cornersPx[i].y == bbox.cornersPx[topIdx].y &&
|
|
333
|
+
bbox.cornersPx[i].x < bbox.cornersPx[topIdx].x)) {
|
|
334
|
+
topIdx = i;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const int anchorX = bbox.cornersPx[topIdx].x;
|
|
338
|
+
const int anchorY = bbox.cornersPx[topIdx].y;
|
|
339
|
+
|
|
340
|
+
const int textX = anchorX + 2;
|
|
341
|
+
const int textY = anchorY - 3;
|
|
320
342
|
|
|
321
|
-
//
|
|
343
|
+
// Draw axis-aligned label background above the topmost corner
|
|
322
344
|
if (labelBackground) {
|
|
323
|
-
|
|
324
|
-
const int
|
|
325
|
-
const int
|
|
326
|
-
const int
|
|
327
|
-
const int bgY2 = std::min(result.rows - 1, bbox.boundingRect.y - 1);
|
|
345
|
+
const int bgX1 = std::max(0, anchorX);
|
|
346
|
+
const int bgY1 = std::max(0, anchorY - bbox.textSize.height - 6);
|
|
347
|
+
const int bgX2 = std::min(result.cols - 1, anchorX + bbox.textSize.width + 4);
|
|
348
|
+
const int bgY2 = std::min(result.rows - 1, anchorY - 1);
|
|
328
349
|
|
|
329
|
-
// Full opacity background with bbox color
|
|
330
350
|
cv::rectangle(result, cv::Point(bgX1, bgY1), cv::Point(bgX2, bgY2), bbox.bgrColor, cv::FILLED);
|
|
331
351
|
}
|
|
332
352
|
|
|
333
|
-
// Draw text with faster LINE_8 instead of LINE_AA
|
|
334
353
|
cv::Scalar textColor = labelBackground ? cv::Scalar(0, 0, 0) : bbox.bgrColor;
|
|
335
354
|
cv::putText(result, bbox.labelText, cv::Point(textX, textY), fontFace, fontScale, textColor, fontThickness, cv::LINE_8);
|
|
336
355
|
}
|