-
Notifications
You must be signed in to change notification settings - Fork 757
Create prediction_visualizer.py #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||||||
| # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| # RF-DETR | ||||||||||||||||||||||||||||||||
| # Copyright (c) 2025 Roboflow. All Rights Reserved. | ||||||||||||||||||||||||||||||||
| # Licensed under the Apache License, Version 2.0 [see LICENSE for details] | ||||||||||||||||||||||||||||||||
| # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||
| from typing import List | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import cv2 | ||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||
| import supervision as sv | ||||||||||||||||||||||||||||||||
| from PIL import Image | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+15
|
||||||||||||||||||||||||||||||||
| from rfdetr import RFDETRBase | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def parse_args(): | ||||||||||||||||||||||||||||||||
| parser = argparse.ArgumentParser(description="Visualize RF-DETR predictions on sample images.") | ||||||||||||||||||||||||||||||||
| parser.add_argument("--weights", type=str, required=True, help="Path to pre-trained RF-DETR model weights.") | ||||||||||||||||||||||||||||||||
| parser.add_argument("--input-dir", type=str, required=True, help="Directory containing input images.") | ||||||||||||||||||||||||||||||||
| parser.add_argument("--output-dir", type=str, default="output", help="Directory to save annotated images.") | ||||||||||||||||||||||||||||||||
| parser.add_argument("--confidence", type=float, default=0.5, help="Confidence threshold for predictions.") | ||||||||||||||||||||||||||||||||
| return parser.parse_args() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def load_images(input_dir: str) -> List[Image.Image]: | ||||||||||||||||||||||||||||||||
| supported_extensions = (".jpg", ".jpeg", ".png") | ||||||||||||||||||||||||||||||||
| return [ | ||||||||||||||||||||||||||||||||
| Image.open(os.path.join(input_dir, f)) | ||||||||||||||||||||||||||||||||
| for f in os.listdir(input_dir) | ||||||||||||||||||||||||||||||||
| if f.lower().endswith(supported_extensions) | ||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+34
|
||||||||||||||||||||||||||||||||
| return [ | |
| Image.open(os.path.join(input_dir, f)) | |
| for f in os.listdir(input_dir) | |
| if f.lower().endswith(supported_extensions) | |
| ] | |
| images: List[Image.Image] = [] | |
| for filename in os.listdir(input_dir): | |
| if not filename.lower().endswith(supported_extensions): | |
| continue | |
| image_path = os.path.join(input_dir, filename) | |
| with Image.open(image_path) as img: | |
| rgb_image = img.convert("RGB") | |
| rgb_image.load() | |
| images.append(rgb_image) | |
| return images |
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sv.BoxAnnotator().annotate(annotated_image, detections) and LabelAnnotator().annotate(annotated_image, detections, labels) are called positionally here, but in the codebase the supervision annotators are used with keyword arguments (scene=..., detections=..., labels=...). Using keywords avoids accidental argument-order mismatches across supervision versions and improves readability.
| annotated_image = sv.BoxAnnotator().annotate(annotated_image, detections) | |
| labels = [f"{pred.class_name} {pred.confidence:.2f}" for pred in detections] | |
| annotated_image = sv.LabelAnnotator().annotate(annotated_image, detections, labels) | |
| annotated_image = sv.BoxAnnotator().annotate(scene=annotated_image, detections=detections) | |
| labels = [f"{pred.class_name} {pred.confidence:.2f}" for pred in detections] | |
| annotated_image = sv.LabelAnnotator().annotate(scene=annotated_image, detections=detections, labels=labels) |
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
labels = [f"{pred.class_name} {pred.confidence:.2f}" for pred in detections] is unlikely to work with sv.Detections returned by RFDETR.predict(). That method constructs sv.Detections with class_id and confidence arrays (no class_name field), so this will raise at runtime or produce incorrect labels. Build labels from detections.class_id/detections.confidence and map class IDs via model.class_names if you want human-readable names.
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several new functions are missing return type hints and docstrings (parse_args, save_annotated_image, main). The repository generally uses full typing + docstrings for functions; adding them here will keep this consistent and help downstream users understand expected types/behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is added under the top-level
rfdetr/directory, but the package is configured as asrc/layout ([tool.setuptools.packages.find] where = ["src"]), sorfdetr/util/prediction_visualizer.pywill not be included in the installed distribution or importable viapython -m rfdetr. If this is meant to be part of the library, it should live undersrc/rfdetr/...; if it’s meant to be a runnable helper script (as the PR description suggests), consider placing it in ascripts/(or similar) directory and documenting that path instead. Also note the PR description referencesvisualize_predictions.py, but this change introducesprediction_visualizer.py.