Repository navigation
Fix the crashes in Prediction.save - #560
Open
iurisilvio wants to merge 2 commits into
Open
iurisilvio wants to merge 2 commits into
iurisilvio wants to merge 2 commits into
Conversation
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>
…int-coords # Conflicts: # roboflow/util/prediction.py
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 hostedhttps://images. Commit db50306 ("fixed save method + predict for hosted images") fixed most of these inPredictionGroup.save(), but they were never applied toPrediction.save(). This PR applies them:roboflow/util/prediction.py): the label background's corners were floats (x - width / 2, ...).cv2.rectanglerejects 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 inrequirements.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 positionPredictionGroup.save()uses; before, it was drawn at the box's vertical center."Confidence: " + self["confidence"]raisedTypeErroron the float confidence the API returns. It is nowstr(self["confidence"]), as inPredictionGroup.save().Prediction.__load_imagedownloaded onlyhttp://URLs. Anhttps://image went tocv2.imread, came back empty, and failed incv2.imwrite(!_img.empty()). It now downloads both, asPredictionGroup.__load_imagedoes.roboflow/util/image_utils.py):mask_imagedecoded the mask withnp.fromstring. NumPy 2.3 removed its binary mode (ValueError: The binary mode of fromstring is removed, use frombuffer instead). It now usesnp.frombuffer, which also fixesPredictionGroup.save()for semantic segmentation.Instance segmentation already worked and is unchanged.
Related Issue(s): none.
Type of Change
Testing
Test details:
tests/util/test_prediction.pysaves aPredictionfortests/images/rabbit.JPGto a PNG and reads it back. It covers object detection, keypoint, classification, semantic segmentation, and a hostedhttps://image (with the download mocked). The object-detection test also checks that the label text is drawn on its background. Each test fails onmain; the semantic-segmentation one fails only on NumPy 2.3 or later.python -m unittestgives 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_qualitypasses.cv2.imdecodealso decodes the read-only array fromnp.frombufferon OpenCV 4.10 with NumPy 1.26.Checklist
Additional Context
urlopencall right below thehttps://check inPrediction.__load_image, so whichever of the two merges second gets a small conflict there. Keep both changes.🤖 Generated with Claude Code