ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.43k stars 16.27k forks source link

How to get simple inference result as txt #6382

Open halzhen opened 2 years ago

halzhen commented 2 years ago

Search before asking

Question

I am trying to inference on a batch of images in a directory. The basic inference code is the one used , I have already read the documentation for the Detections return, is there any way to get the detected objects as .txt ? In a similar way to result.print() , it shows the objects and the times they appear.

I am aware that detect.py can save as .txt but couldn't do inference on batch of images with it.


model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
directory = 'keyframes/'

frames = [] # batch of images
for filename in os.listdir(directory):
    frame = Image.open('keyframes/'+filename)
    frames.append(frame)

# Inference
results = model(frames, size=640) 

# Results
results.print()
results.save()  ```

What I need is the objects detected in a .txt file without using detect.py, but if detect.py can be modified to work on batch of images from a directory that would also work. 

### Additional

_No response_
github-actions[bot] commented 2 years ago

👋 Hello @kzhenzhou, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

Requirements

Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 2 years ago

@kzhenzhou that's funny, you are right there is no --save-txt method for PyTorch Hub models. You could submit a PR to add such a method, i.e.:

results.save_txt()

https://github.com/ultralytics/yolov5/blob/bd815d48df18a23e2bb08d88e430183bfb48eb78/models/common.py#L551-L650

I'll add a TODO here since this seems like a useful feature to have, but we are quite busy so I can not provide an estimated implementation timeline if we do this ourselves.

TODO: results.save_txt() method.

SarperYurttas commented 2 years ago

@glenn-jocher I want to work on this if it's ok?

glenn-jocher commented 2 years ago

@SarperYurttas awesome, yes go for it!

Symbadian commented 1 year ago

@kzhenzhou that's funny, you are right there is no --save-txt method for PyTorch Hub models. You could submit a PR to add such a method, i.e.:

results.save_txt()

https://github.com/ultralytics/yolov5/blob/bd815d48df18a23e2bb08d88e430183bfb48eb78/models/common.py#L551-L650

I'll add a TODO here since this seems like a useful feature to have, but we are quite busy so I can not provide an estimated implementation timeline if we do this ourselves.

TODO: results.save_txt() method.

Hi @glenn-jocher,

is there anyway to get the heading of the result type saved as well in the output files?

for example:

epoch loss acc val_loss val_acc

Currently, in the save files the data is presented as the example below without headings in .txt format 0 1.64137483 0.27450982 0.8043378 0.46153846

I tried --save-txt with save --save-conf but none is presenting the headings for the processing results...

I am trying to do a basic decision process but these bits are not available!

Thanx for responding and acknowledging my digital presence, really appreciate this!

glenn-jocher commented 11 months ago

@Symbadian Thanks for reaching out! We appreciate your support and contribution to the YOLOv5 community. Currently, the --save-txt option does not include column headings in the saved .txt file. However, you can easily manipulate the .txt file after saving it to include headings using any text manipulation tool or a simple script. We appreciate your understanding and contributions. Good luck with your work!