Skip to content
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

Add new --save-csv argument to detect.py #12042

Merged
merged 8 commits into from
Sep 4, 2023

Conversation

akashAD98
Copy link
Contributor

@akashAD98 akashAD98 commented Aug 27, 2023

feature #12041
added support for saving results in CSV, used for testing purposes

fixed: #9690,
#10189
Another issue and PR but this is for classification: #11654

added argument --save-in-csv ,then only it will save the result in CSV file

image image

why this feature is important?
if we want to do testing of our model,then doing this thing manually is very time take, We can simply get the CSV file & result, then we can add our remark there & get the best confidence score or other analysis using this sheet

example use case:
image

how to run it?

!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images --save-in-csv

the prediction.csv file will save inside exp/ folder

🤖 Generated by Copilot at 71114f4

Summary

✨💾📄

This file adds an option to export detection results to a CSV file. It uses a new save_in_csv argument and a write_to_csv function to handle the CSV output.

Oh we are the coders of the sea, me hearties
We write the scripts that make the detections
And when we want to save them in a file
We use the save_in_csv option

Walkthrough

  • Add functionality to save detection results in CSV format (--save-in-csv option) (link, link, link, link)
  • Import csv and pandas modules for CSV handling (link)

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Added CSV output support for object detection results in YOLOv5.

📊 Key Changes

  • Introduced CSV module import in detect.py.
  • Added save_csv argument to enable CSV output format.
  • Implemented CSV file writing functionality within the detection loop.
  • Detect.py now creates a CSV file predictions.csv and appends detection results with image names, predicted classes, and confidence scores.

🎯 Purpose & Impact

  • 🎯 Purpose: To provide users with an additional, structured output format that can be easily used for further data analysis and is compatible with spreadsheet software.
  • 💥 Impact: Users can now export their object detection results directly into a CSV file, enhancing the usability of YOLOv5 for tasks requiring post-processing or analysis of detection data. This makes YOLOv5 even more versatile in various research and production environments.

akashAD98 and others added 3 commits August 27, 2023 09:29
added support for saving result in csv,used for testing

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>
Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>
@akashAD98
Copy link
Contributor Author

@glenn-jocher @AyushExel can you please review it?

@glenn-jocher
Copy link
Member

@akashAD98 hi there! Thank you for your contribution to YOLOv5. Saving detection results in a CSV file is indeed a useful feature for testing and analysis purposes. It's great to see that you have added support for this feature and even provided examples of how it can be used.

We appreciate your effort in fixing other related issues as well and for providing the necessary command to run the feature. The ability to save the prediction.csv file inside the exp/ folder will make it more convenient for users to access and analyze the results.

Your contribution will definitely benefit the YOLOv5 community and we appreciate your dedication to improving the repository. If you have any further questions or need assistance with anything else, please let us know. Keep up the great work!

detect.py Outdated
@@ -229,6 +254,7 @@ def parse_opt():
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--view-img', action='store_true', help='show results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--save-in-csv', action='store_true', help='save results in CSV format')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call this --save-csv ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ill update it

changed save_in_csv to save_csv

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>
@akashAD98
Copy link
Contributor Author

@AyushExel please have look

@glenn-jocher
Copy link
Member

@akashAD98 hi there!

Thank you for bringing this issue to our attention. We understand the need for saving detection results in a CSV file for further analysis and testing purposes.

We appreciate your patience in waiting for a resolution. We will review the feature request and determine the best course of action. Our team is working diligently to address all issues and make improvements to YOLOv5.

If you have any additional information or suggestions regarding this feature, please feel free to share them. Your input is valuable to us.

Thank you for your support and contribution to YOLOv5. We will keep you updated on the progress.

Thank you,
The YOLOv5 Team

@akashAD98
Copy link
Contributor Author

@glenn-jocher can you please review this pr,seems i did all changes,let me know if anything is missing

@glenn-jocher
Copy link
Member

@akashAD98 thank you for your contribution to the YOLOv5 repository. We appreciate your effort in addressing the issue and making the necessary changes.
I will now review the pull request and provide feedback if anything is missing or needs further attention. Thank you for your patience.

@glenn-jocher
Copy link
Member

@akashAD98 I took a look at this and it looks good! One thing I noticed was that I don't think we need this if statement as the mode='a' part should work regardless of whether a file exists or not (if it's not there it will make it). Can you update the PR with this simplification and test the change?

@glenn-jocher
Copy link
Member

For example I think this will do the same:

PR

       def write_to_csv(image_name, prediction, confidence):
            data = {'Image Name': image_name, 'Prediction': prediction, 'Confidence': confidence}
            if not csv_path.is_file():
                with open(csv_path, mode='w', newline='') as f:
                    writer = csv.DictWriter(f, fieldnames=data.keys())
                    writer.writeheader()
            else:
                with open(csv_path, mode='a', newline='') as f:
                    writer = csv.DictWriter(f, fieldnames=data.keys())
                    writer.writerow(data)

Proposed change

       def write_to_csv(image_name, prediction, confidence):
            data = {'Image Name': image_name, 'Prediction': prediction, 'Confidence': confidence}
            is_file = csv_path.is_file()
            with open(csv_path, mode='a', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=data.keys())
                writer.writerow(data) if is_file else writer.writeheader()

@glenn-jocher
Copy link
Member

@akashAD98 pinging you about above comments

akashAD98 and others added 3 commits September 2, 2023 15:59
Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>
@akashAD98
Copy link
Contributor Author

@glenn-jocher Ohh i missed your message, I updated it,please have look

@glenn-jocher
Copy link
Member

Hi @akashAD98,

Thank you for updating the pull request. I appreciate your responsiveness. I reviewed the changes, and it looks good now. The simplification you made to the code is indeed an improvement.

I will go ahead and merge your pull request. Your contribution will be a valuable addition to the YOLOv5 repository. Keep up the good work!

Thanks,
Glenn

@akashAD98
Copy link
Contributor Author

@glenn-jocher waiting for pr to merge :)

@glenn-jocher
Copy link
Member

@akashAD98 thanks for your contribution! I'm glad to see that you've submitted a pull request. Our team will review it as soon as possible. We appreciate your patience in waiting for it to be merged. Keep up the good work!

@glenn-jocher glenn-jocher changed the title Feature: added support for saving result in csv file used for testing our model performance Add new --save-csv argument to detect.py Sep 4, 2023
@glenn-jocher glenn-jocher merged commit 8c30c58 into ultralytics:master Sep 4, 2023
8 checks passed
@glenn-jocher
Copy link
Member

@akashAD98 I've reviewed and tested the PR, all good, PR merged!

Thank you for your contributions to YOLO :)

@akashAD98
Copy link
Contributor Author

@glenn-jocher @AyushExel thanks guys

@glenn-jocher
Copy link
Member

@akashAD98 thank you for your kind words! We appreciate your support. If you have any questions or need any assistance, feel free to ask.

@akashAD98 akashAD98 deleted the feature/save_in_csv branch September 4, 2023 13:28
NagatoYuki0943 added a commit to NagatoYuki0943/yolov5-ultralytics that referenced this pull request Sep 5, 2023
Add new `--save-csv` argument to detect.py (ultralytics#12042)
pleb631 pushed a commit to pleb631/yolov5 that referenced this pull request Jan 6, 2024
* Update detect.py

added support for saving result in csv,used for testing

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update detect.py

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>

* Update detect.py

changed save_in_csv to save_csv

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>

* Update detect.py

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Akash A Desai <62583018+akashAD98@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How do I save the detected class names to a text file?
3 participants