Skip to content

Commit

Permalink
Update detect.py (Fix save-csv: Ensure header is written to CSV) (#13472
Browse files Browse the repository at this point in the history
)

This commit resolves an issue where the save-csv command did not write the CSV header. The code now correctly saves the header in the CSV file.

Signed-off-by: Ali Ghanbari <alighanbari446@gmail.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
aligh993 and glenn-jocher authored Jan 1, 2025
1 parent 915ce21 commit f003c3d
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ def run(
def write_to_csv(image_name, prediction, confidence):
"""Writes prediction data for an image to a CSV file, appending if the file exists."""
data = {"Image Name": image_name, "Prediction": prediction, "Confidence": confidence}
file_exists = os.path.isfile(csv_path)
with open(csv_path, mode="a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=data.keys())
if not csv_path.is_file():
if not file_exists:
writer.writeheader()
writer.writerow(data)

Expand Down

0 comments on commit f003c3d

Please sign in to comment.