ultralytics / yolov5

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

How to sort detected object with priority #7395

Closed Chuttyboy closed 2 years ago

Chuttyboy commented 2 years ago

Search before asking

Question

Detecting object using detect.py and its working fine. After that i decide to save results in seperate text file so altered few line in detect.py

        # Print time (inference-only)

        with open("output.txt", "a") as f:
            now = datetime.datetime.now().strftime("%a, %d %B %I:%M:%S")
            print(f'{s}Done. date ({t3 - t2:.3f}s)', now ,file=f)

       # LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)') #0: 480x640 Done. (0.532s) output of this line 

After this modification results are saved in output.txt file. result are in output.txt file in this format

0: 480x640 2 persons, 1 tv, 1 laptop, 1 clock, 1: 480x640 5 persons, 1 bottle, 3 chairs, 2 tvs, 1 cell phone, 1 oven, Done. (0.759s) Mon, 04 April 11:39:48
0: 480x640 2 persons, 1 tv, 1 laptop, 2 clocks, 1: 480x640 4 persons, 1 car, 3 chairs, 2 tvs, 1 laptop, 1 oven, Done. (0.763s) Mon, 04 April 11:39:50

With this data i want to give priority to detected object likeHigh , Medium , Low . If person and tv detected give status High to that , if person and laptop detected give medium status , How can i do the same way to give priority to detected object.

Thanks in advance

Additional

No response

glenn-jocher commented 2 years ago

@Chuttyboy 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py. This should give you the most flexibility to sort the result Tensor or Pandas dataframe however you want.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, etc.
# model = torch.hub.load('ultralytics/yolov5', 'custom', 'path/to/best.pt')  # custom trained model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

results.xyxy[0]  # im predictions (tensor)
results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck 🍀 and let us know if you have any other questions!