Skip to content

Commit 5fa83eb

Browse files
committed
Fix ONNX pose parser: handle end2end corner bbox format
End-to-end YOLO models (end2end=True) output bounding boxes as [x1, y1, x2, y2] (corner format) not [cx, cy, w, h] (center format). The parser was applying center-to-corner conversion on already-corner coords, producing garbage negative coordinates — so detections existed but boxes drew offscreen. Added format detection heuristic: if v2 > v0 and v3 > v1 and both within input dimensions, treat as corner format. Otherwise center. Also added debug logging to console for detection diagnostics.
1 parent d945a16 commit 5fa83eb

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

19-sports-player-id/onnx-engine.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,11 @@
302302
for (var i = 0; i < numDets; i++) {
303303
var offset = i * cols;
304304

305-
// Bbox (in model input pixel coordinates)
306-
var cx = data[offset + 0];
307-
var cy = data[offset + 1];
308-
var w = data[offset + 2];
309-
var h = data[offset + 3];
305+
// Bbox values from end2end model
306+
var v0 = data[offset + 0];
307+
var v1 = data[offset + 1];
308+
var v2 = data[offset + 2];
309+
var v3 = data[offset + 3];
310310

311311
// Confidence score
312312
var conf = data[offset + 4];
@@ -315,6 +315,24 @@
315315
// Class ID (end2end models output class index directly)
316316
var classId = Math.round(data[offset + 5]);
317317

318+
// Determine bbox format: end2end uses [x1, y1, x2, y2] (corner format)
319+
// Regular YOLO uses [cx, cy, w, h] (center format)
320+
// Heuristic: if v2 > inputWidth * 0.5 and v3 > inputHeight * 0.5, likely corner format
321+
var bx, by, bw, bh;
322+
if (v2 > v0 && v3 > v1 && v2 <= this.inputWidth + 1 && v3 <= this.inputHeight + 1) {
323+
// Corner format: [x1, y1, x2, y2]
324+
bx = v0 * scaleX;
325+
by = v1 * scaleY;
326+
bw = (v2 - v0) * scaleX;
327+
bh = (v3 - v1) * scaleY;
328+
} else {
329+
// Center format: [cx, cy, w, h]
330+
bx = (v0 - v2 / 2) * scaleX;
331+
by = (v1 - v3 / 2) * scaleY;
332+
bw = v2 * scaleX;
333+
bh = v3 * scaleY;
334+
}
335+
318336
// Keypoints: starting at offset 6, groups of 3 (x, y, conf)
319337
var keypoints = [];
320338
for (var k = 0; k < numKpts; k++) {
@@ -327,17 +345,24 @@
327345
}
328346

329347
detections.push({
330-
x: (cx - w / 2) * scaleX,
331-
y: (cy - h / 2) * scaleY,
332-
w: w * scaleX,
333-
h: h * scaleY,
348+
x: bx,
349+
y: by,
350+
w: bw,
351+
h: bh,
334352
confidence: conf,
335353
class: this.classNames[classId] || ('class_' + classId),
336354
classId: classId,
337355
keypoints: keypoints
338356
});
339357
}
340358

359+
if (detections.length > 0) {
360+
console.log('[ONNX] Detected ' + detections.length + ' objects. First:',
361+
JSON.stringify({cls: detections[0].class, conf: detections[0].confidence.toFixed(2), x: Math.round(detections[0].x), y: Math.round(detections[0].y), w: Math.round(detections[0].w), h: Math.round(detections[0].h)}));
362+
} else {
363+
console.log('[ONNX] No detections above threshold');
364+
}
365+
341366
return { detections: detections, keypoints: detections.length > 0 ? detections[0].keypoints : [] };
342367
};
343368

0 commit comments

Comments
 (0)