Fix boxes that miss their objects after resizing
Track resize, padding, color order, and coordinate transforms through an inference pipeline.
Opening the Python editor…
Requirements and learning goals
Before you start
- The Wendy starter project from the YOLO walkthrough
- Basic Python and arrays
You will be able to
- Describe the input contract
- Undo letterbox coordinates
- Catch color and scaling errors
The browser exercise above works without extra setup. The project brief below assumes the listed prerequisites and gives you a task and checks to perform. Need a guided first project?
Preprocessing converts a camera image into the size and numeric format a model expects. A wrong color order can hurt recognition; a wrong resize can send your robot toward the wrong pixel. Here you will undo image padding and scaling so detections line up with the original photograph.
Compare preprocessing against frames from the actual camera stream, not only resized sample images from your laptop.
Preserve the input contract
In the previous YOLO walkthrough, result.boxes.xyxy already contains coordinates in the original scene.jpg image. Keep those JSON coordinates unchanged. Ultralytics performs the inverse mapping in its detection predictor. Applying the inverse below to those boxes would distort them a second time.
Record width, height, color order, numeric range, data type, and tensor layout. High-level predictors often perform preprocessing internally; adding another normalization can corrupt the input.
The following is a separate arithmetic exercise for outputs still expressed in the model's padded image. Save it as letterbox_check.py beside detect.py in the Wendy starter project. Suppose a 1280×720 image is letterboxed into 640×640. The scale is 0.5, leaving 140 pixels of padding above and below:
width, height, target = 1280, 720, 640
scale = min(target / width, target / height)
pad_x = (target - width * scale) / 2
pad_y = (target - height * scale) / 2
def to_original(x, y):
return ((x - pad_x) / scale, (y - pad_y) / scale)
assert to_original(320, 320) == (640, 360)
print(to_original(320, 320)) # (640.0, 360.0)Stop the previous attached run with Ctrl+C, then deploy this exercise to the same target:
wendy run --env LESSON_SCRIPT=letterbox_check.pyExpect (640.0, 360.0) in the Wendy logs and in run.log on the results page. The runner selects this script instead of the detector; it uses the same environment.
This example uses exact symmetric padding. Store the actual runtime's resize and rounding parameters. If its output boxes already use original-image coordinates, do not apply this inverse again.
Check geometry separately
Create an image with colored corner squares and a central cross. Pass it through preprocessing and your inverse coordinate mapping. The cross should return to its original position; corners should not swap.
are expressed in pixels. Resizing or cropping changes the pixel-to-ray mapping. Either return detections to the calibrated original frame or update intrinsics consistently. See OpenCV's projection model.
Common snag: boxes look plausible while RGB/BGR channels are reversed. Inspect pixel values as well as the overlay. A fixed box offset suggests padding; an error that grows away from the center suggests scaling.
Optional extension: repeat with a different aspect ratio and verify the same coordinate invariants before using detections for .
Before you move on
Can you do each of these?
- Describe the input contract
- Undo letterbox coordinates
- Catch color and scaling errors
0 of 2 checkpoints solved. Try checkpoint 1