Skip to content

Commit

Permalink
Enhance clarity in results.to_ function examples. (ultralytics#18957)
Browse files Browse the repository at this point in the history
  • Loading branch information
RizwanMunawar authored Jan 31, 2025
1 parent 616fd57 commit df6572a
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions ultralytics/engine/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ def plot(
Examples:
>>> results = model("image.jpg")
>>> for result in results:
... im = result.plot()
... im.show()
>>> im = result.plot()
>>> im.show()
"""
assert color_mode in {"instance", "class"}, f"Expected color_mode='instance' or 'class', not {color_mode}."
if img is None and isinstance(self.orig_img, torch.Tensor):
Expand Down Expand Up @@ -600,7 +600,7 @@ def show(self, *args, **kwargs):
>>> results = model("path/to/image.jpg")
>>> results[0].show() # Display the first result
>>> for result in results:
... result.show() # Display all results
>>> result.show() # Display all results
"""
self.plot(show=True, *args, **kwargs)

Expand All @@ -620,10 +620,10 @@ def save(self, filename=None, *args, **kwargs):
Examples:
>>> results = model("path/to/image.jpg")
>>> for result in results:
... result.save("annotated_image.jpg")
>>> result.save("annotated_image.jpg")
>>> # Or with custom plot arguments
>>> for result in results:
... result.save("annotated_image.jpg", conf=False, line_width=2)
>>> result.save("annotated_image.jpg", conf=False, line_width=2)
"""
if not filename:
filename = f"results_{Path(self.path).name}"
Expand All @@ -644,7 +644,7 @@ def verbose(self):
Examples:
>>> results = model("path/to/image.jpg")
>>> for result in results:
... print(result.verbose())
>>> print(result.verbose())
2 persons, 1 car, 3 traffic lights,
dog 0.92, cat 0.78, horse 0.64,
Expand Down Expand Up @@ -681,7 +681,7 @@ def save_txt(self, txt_file, save_conf=False):
>>> model = YOLO("yolo11n.pt")
>>> results = model("path/to/image.jpg")
>>> for result in results:
... result.save_txt("output.txt")
>>> result.save_txt("output.txt")
Notes:
- The file will contain one line per detection or classification with the following structure:
Expand Down Expand Up @@ -740,7 +740,7 @@ def save_crop(self, save_dir, file_name=Path("im.jpg")):
Examples:
>>> results = model("path/to/image.jpg")
>>> for result in results:
... result.save_crop(save_dir="path/to/crops", file_name="detection")
>>> result.save_crop(save_dir="path/to/crops", file_name="detection")
"""
if self.probs is not None:
LOGGER.warning("WARNING ⚠️ Classify task do not support `save_crop`.")
Expand Down Expand Up @@ -776,8 +776,9 @@ def summary(self, normalize=False, decimals=5):
Examples:
>>> results = model("image.jpg")
>>> summary = results[0].summary()
>>> print(summary)
>>> for result in results:
>>> summary = result.summary()
>>> print(summary)
"""
# Create list of detection dictionaries
results = []
Expand Down Expand Up @@ -839,8 +840,9 @@ def to_df(self, normalize=False, decimals=5):
Examples:
>>> results = model("path/to/image.jpg")
>>> df_result = results[0].to_df()
>>> print(df_result)
>>> for result in results:
>>> df_result = result.to_df()
>>> print(df_result)
"""
import pandas as pd # scope for faster 'import ultralytics'

Expand All @@ -867,8 +869,9 @@ def to_csv(self, normalize=False, decimals=5, *args, **kwargs):
Examples:
>>> results = model("path/to/image.jpg")
>>> csv_result = results[0].to_csv()
>>> print(csv_result)
>>> for result in results:
>>> csv_result = result.to_csv()
>>> print(csv_result)
"""
return self.to_df(normalize=normalize, decimals=decimals).to_csv(*args, **kwargs)

Expand All @@ -892,8 +895,9 @@ def to_xml(self, normalize=False, decimals=5, *args, **kwargs):
Examples:
>>> results = model("path/to/image.jpg")
>>> xml_result = results[0].to_xml()
>>> print(xml_result)
>>> for result in results:
>>> xml_result = result.to_xml()
>>> print(xml_result)
"""
check_requirements("lxml")
df = self.to_df(normalize=normalize, decimals=decimals)
Expand Down Expand Up @@ -922,8 +926,9 @@ def to_json(self, normalize=False, decimals=5):
Examples:
>>> results = model("path/to/image.jpg")
>>> json_result = results[0].to_json()
>>> print(json_result)
>>> for result in results:
>>> json_result = result.to_json()
>>> print(json_result)
Notes:
- For classification tasks, the JSON will contain class probabilities instead of bounding boxes.
Expand Down Expand Up @@ -954,8 +959,8 @@ def to_sql(self, table_name="results", normalize=False, decimals=5, db_path="res
Examples:
>>> results = model("path/to/image.jpg")
>>> results[0].to_sql()
>>> print("SQL data written successfully.")
>>> for result in results:
>>> result.to_sql()
"""
import json
import sqlite3
Expand Down

0 comments on commit df6572a

Please sign in to comment.