feat: improve Results.save() with pathlib and optional directory creation#23592
Conversation
|
👋 Hello @ShuaiLYU, thank you for submitting a -✅ Define a Purpose: Clearly explain the purpose of your fix or feature in your PR description, and link to any relevant issues. Ensure your commit messages are clear, concise, and adhere to the project's conventions. For more guidance, please refer to our Contributing Guide. Don't hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀 |
UltralyticsAssistant
left a comment
There was a problem hiding this comment.
🔍 PR Review
Made with ❤️ by Ultralytics Actions
Overall looks good and the new directory-creation behavior is helpful. The only concern is the return type now implicitly becoming Path due to the new conversion; consider keeping the return type consistent with the annotation/docstring.
💬 Posted 1 inline comment
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@Laughing-q Now, the "save function" will automatically create the parent folder (rather than skipping the image saving process). |
|
@ShuaiLYU this looks good, but we can skip the argument and create it always if needed. You can use |
You’re right, there is a 'exist_ok' argument for mkdir()! I’m removing the create_dir argument and using mkdir(exist_ok=True) to auto-handle the directory check (create if missing, skip if present). |
f2ff569 to
d43b49c
Compare
Results.save() with pathlib and optional directory creation
…tion
This pull request updates the
savemethod in theResultsclass to add more flexibility when saving annotated images. The main change is the introduction of acreate_dirparameter, allowing users to control whether the parent directory is created automatically when saving results.Enhancements to the
savemethod:create_dirboolean parameter (defaulting toTrue) to thesavemethod inultralytics/engine/results.py, allowing users to specify whether the parent directory should be created if it doesn't exist. [1] [2]create_dirisTrue.create_dirparameter and demonstrate its usage. [1] [2]🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Enhances
Results.save()by adoptingpathliband optionally creating output directories for smoother save workflows 🧰📊 Key Changes
-Adds a new
create_dir: bool = Trueargument toResults.save()-Converts
filenameto an absolutePathbefore saving-Automatically creates the parent directory (when enabled) via
mkdir(parents=True, exist_ok=True)-Updates docstring and examples to reflect the new behavior
🎯 Purpose & Impact
-Prevents save failures when targeting non-existent directories by creating them automatically 📁
-Improves path handling consistency and cross-platform robustness using
pathlib-Gives users control to disable directory creation (
create_dir=False) for stricter filesystem behavior