Skip to content

Fix the crashes in Prediction.save - #560

Open
iurisilvio wants to merge 2 commits into
mainfrom
iuri/prediction-save-int-coords
Open

iurisilvio wants to merge 2 commits into
mainfrom
iuri/prediction-save-int-coords

Conversation

@iurisilvio

Copy link
Copy Markdown
Contributor

What does this PR do?

Prediction.save() draws a single prediction on its image and writes it to a file. It raises before writing anything for object-detection, keypoint, classification and semantic-segmentation predictions, and for hosted https:// images. Commit db50306 ("fixed save method + predict for hosted images") fixed most of these in PredictionGroup.save(), but they were never applied to Prediction.save(). This PR applies them:

  • Object detection and keypoint (roboflow/util/prediction.py): the label background's corners were floats (x - width / 2, ...). cv2.rectangle rejects float points (cv2.error: ... Can't parse 'pt1'. Sequence item with index 0 has a wrong type) on every supported OpenCV: checked on 4.10, the minimum in requirements.txt, and on 5.0. The corners are now ints. The label text now sits on that background at the top of the box, the same position PredictionGroup.save() uses; before, it was drawn at the box's vertical center.
  • Classification: "Confidence: " + self["confidence"] raised TypeError on the float confidence the API returns. It is now str(self["confidence"]), as in PredictionGroup.save().
  • Hosted images: Prediction.__load_image downloaded only http:// URLs. An https:// image went to cv2.imread, came back empty, and failed in cv2.imwrite (!_img.empty()). It now downloads both, as PredictionGroup.__load_image does.
  • Semantic segmentation (roboflow/util/image_utils.py): mask_image decoded the mask with np.fromstring. NumPy 2.3 removed its binary mode (ValueError: The binary mode of fromstring is removed, use frombuffer instead). It now uses np.frombuffer, which also fixes PredictionGroup.save() for semantic segmentation.

Instance segmentation already worked and is unchanged.

Related Issue(s): none.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

  • I have tested this change locally
  • I have added/updated tests for this change

Test details:

  • The new tests/util/test_prediction.py saves a Prediction for tests/images/rabbit.JPG to a PNG and reads it back. It covers object detection, keypoint, classification, semantic segmentation, and a hosted https:// image (with the download mocked). The object-detection test also checks that the label text is drawn on its background. Each test fails on main; the semantic-segmentation one fails only on NumPy 2.3 or later.
  • python -m unittest gives 1238 tests OK, 1 skipped, on Python 3.12 (NumPy 2.5.4, OpenCV 5.0.0) and on Python 3.10 (NumPy 2.2.6, OpenCV 5.0.0). make check_code_quality passes. cv2.imdecode also decodes the read-only array from np.frombuffer on OpenCV 4.10 with NumPy 1.26.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in hard-to-understand areas
  • My changes generate no new warnings or errors
  • I have updated the documentation accordingly (if applicable): not applicable

Additional Context

🤖 Generated with Claude Code

Prediction.save() raised before writing the file for most predictions:

- Object detection and keypoint: the label background got float corners
  (x - width / 2, ...), which cv2.rectangle rejects on every supported
  OpenCV. The corners are now ints, and the label text sits on that
  background at the top of the box, where PredictionGroup.save puts it.
- Classification: "Confidence: " + confidence failed on the float the API
  returns, so the confidence goes through str() first.
- Hosted images: only http:// URLs were downloaded, so an https:// image
  went to cv2.imread and came back empty. Both are downloaded now.
- Semantic segmentation: mask_image decoded the mask with np.fromstring,
  whose binary mode NumPy 2.3 removed. It uses np.frombuffer now, which
  fixes PredictionGroup.save for semantic segmentation too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iurisilvio iurisilvio self-assigned this Oct 19, 2026
…int-coords

# Conflicts:
#	roboflow/util/prediction.py

This branch has not been deployed

No deployments
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