Skip to content

cv-server: return GroundingDINO boxes as corners, per the documented contract - #26

Merged
mrhegemon merged 2 commits into
mainfrom
fix/scan-boxes
Nov 14, 2026
Merged

mrhegemon merged 2 commits into
mainfrom
fix/scan-boxes

Conversation

@mrhegemon

Copy link
Copy Markdown
Member

Summary

The CV server put every GroundingDINO box in the wrong place. GroundingDINO's predict() returns (cx, cy, w, h): the centre and size as fractions of the image. Both detection pipelines scaled those numbers as if they were corners. As a result, /v1/detect, /v1/segment and WS /v1/stream returned [cx*W, cy*H, w*W, h*H] in pixels, and SAM 2 was prompted with the same wrong rectangle. The Quest app sorts the corners before use, which hid the inverted boxes but not the wrong positions.

This PR converts the boxes in one place and documents one format: [x1, y1, x2, y2] corners as fractions of the image size.

  • Two commits on main @ 2147965. They change cv-server/ and one PlayMode test.

What changed

  • One conversion. cxcywh_to_xyxy in app/utils/boxes.py turns GroundingDINO's boxes into clipped [x1, y1, x2, y2]: top-left and bottom-right corners, origin at the top-left, as fractions from 0 to 1 of the image width and height.
    • Both pipelines use it, and SAM 2 gets the same corners in pixels.
    • The Quest app's BoundingBoxUtils.TryNormalize and the web client already read this format.
    • Fractions stay the same when max_side shrinks the image.
  • Documentation. The cv-server README ("Boxes") and the OpenAPI schema document the format. image_size from /v1/segment is documented as the [height, width] that the masks cover.
  • Server tests. tests/test_box_contract.py runs the endpoints with stand-in models that return known boxes, so it needs no weights or GPU. tests/test_boxes.py covers the conversion.
  • Unity test. PerImageModelTests (PlayMode) accepted pixel or fraction boxes. It now requires fractions with x1 ≤ x2 and y1 ≤ y2, as PipelineIntegrationTests already did.

Verification

  • New tests. On main, the 6 endpoint tests fail: a box comes back as [50, 50, 40, 40] instead of [0.15, 0.30, 0.35, 0.70]. test_boxes.py fails to import there. On this branch, all 17 pass.

  • Full suite. On Python 3.11 without model weights, 62 passed and 5 skipped. The 45 older tests give the same result on main. In the release-candidate image (transformers 4.40.1) with model weights, 66 passed and 1 skipped.

  • Real models. The release-candidate image ran this branch's code on a GPU, on three 1280×1024 test images:

    Image Boxes Old boxes inverted as sent Old boxes with IoU < 0.5 after the app's sort SAM 2 mask-to-box IoU, median: new (old)
    Closet 25 24 25 0.956 (0.000)
    Pantry 29 27 29 0.972 (0.000)
    Laundry room 11 11 11 0.966 (0.000)

    The new boxes match GroundingDINO's own centre-to-corner conversion to within 9e-8.

  • HTTP. This branch's app ran in the same image.

    • On the closet image, /v1/detect and /v1/segment returned 200 with 25 boxes, all ordered fractions in [0, 1].
    • main's app returned 25 pixel boxes. The first was [177.6, 160.7, 177.4, 141.7], inverted on both axes.
    • With max_side=640 (masks at 640×512), the median mask-to-box IoU is 0.933.
  • Image. The image built from this branch starts with lazy model loading. /v1/health answers 401 without the token and 200 with it.

  • C#. A syntax check of the edited PlayMode test found no errors.

With fix/scan-pins: merge that first

  • No conflicts. This branch and fix/scan-pins merge without conflicts in either order. Both edit cv-server/README.md, in different sections.
  • Merged tests. The merged tree was tested in the fix/scan-pins image. With the model files, 93 tests passed and 1 was skipped. Without them, 89 passed and 5 were skipped.
  • Merge fix/scan-pins first. On main, transformers 5.10.1 (Bump transformers from 4.40.1 to 5.10.1 in /cv-server in the pip group across 1 directory #24) cannot import GroundingDINO's BERT or Florence-2 on torch 2.5.1. An image built with this branch alone therefore cannot detect anything: /v1/detect and /v1/segment return 500. With CV_SERVER_LOAD_ALL=true, the compose default, the container exits at startup.
  • Neither works alone. fix/scan-pins alone serves the misplaced boxes. Only the two together give correct boxes from a built image.

Not verified

  • Unity tests. The PlayMode tests (PerImageModelTests, PipelineIntegrationTests) need Unity and a live server and were not run. This PR's Unity Tests check runs main's workflow, whose Unity activation ci/release-signing replaces. If that check fails at activation, the cause is not this change.
  • Merged image. No real POST /v1/detect has been sent to an image built from the merged tree. The box fix was checked with real models on transformers 4.40.1.
  • Headset. Pin placement has not been checked on a headset.
  • Doc follow-ups.
    • The README's "Boxes" section also names /v1/florence. Only Florence's bboxes are fractions: they are divided by the image size but not clipped or reordered. Its quad_boxes and polygons are still pixels of the processed image.
    • roadmap.md still describes pixel boxes.

Owner actions after merge

  1. Check the rebuilt image. With this PR and fix/scan-pins both on main, rebuild the CV server image and start it as docker-compose.yml does. Send one real POST /v1/detect and one /v1/segment with a test image. Every box should be an ordered fraction in [0, 1].
  2. Test in Unity and on a headset. Run the Unity PlayMode tests against that server, and check pin placement on a headset.
  3. Update other clients. Any other client that read the old pixel values must now read fractions. The Quest app and the web client already do.

🤖 Generated with Claude Code

mrhegemon and others added 2 commits November 13, 2026 13:47
GroundingDINO's predict() returns boxes as (cx, cy, w, h), fractions of the
image size. Both detection pipelines (/v1/detect and /v1/segment, and
WS /v1/stream through them) scaled those four numbers as if they were
corners, so a box came back as [cx*W, cy*H, w*W, h*H], and SAM 2 was
prompted with the same wrong rectangle. The Quest client sorts the corners
before use, which hid the inverted boxes but not the wrong positions.

One conversion in app/utils/boxes.py now serves both pipelines. Every box
the API returns is [x1, y1, x2, y2]: top-left and bottom-right corners,
origin at the top-left, as fractions of the image width and height. That is
what BoundingBoxUtils.TryNormalize in the Quest client reads, what the web
client and PipelineIntegrationTests expect, and what /v1/florence already
returned; it also stays correct when max_side shrinks the image. SAM 2 gets
the same corners in pixels. The format is documented in the README
("Boxes") and in the OpenAPI schema.

tests/test_box_contract.py runs the endpoints with stand-in models that
return known boxes, so it needs no weights or GPU; it fails on the previous
code and passes now. tests/test_boxes.py covers the conversion itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PerImageModelTests accepted pixel or normalised boxes, while
PipelineIntegrationTests requires fractions in [0, 1]. The cv-server now
documents one format, [x1, y1, x2, y2] corners as fractions of the image
size, so GDino_Detect_AllImages_BboxesValid checks it: values in [0, 1],
x1 <= x2 and y1 <= y2. The corner check would have caught the
(cx, cy, w, h) mix-up, which TryNormalize silently reordered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mrhegemon
mrhegemon merged commit 714feb8 into main Nov 14, 2026
1 check failed
@mrhegemon
mrhegemon deleted the fix/scan-boxes branch November 14, 2026 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant