From f003c3df9ec2b7dd8825edf21c775c95f1f45a2b Mon Sep 17 00:00:00 2001 From: Ali Ghanbari Date: Thu, 2 Jan 2025 00:16:59 +0330 Subject: [PATCH] Update detect.py (Fix save-csv: Ensure header is written to CSV) (#13472) 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 Co-authored-by: Glenn Jocher --- detect.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/detect.py b/detect.py index f404a2503..ac18f3c36 100644 --- a/detect.py +++ b/detect.py @@ -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)