import argparse import cv2 import numpy as np import onnxruntime as ort CLASSES = [...]…
- User
import argparse import cv2 import numpy as np import onnxruntime as ort
CLASSES = [...]
def detect(model_path, img_path, threshold=0.4):
session = ort.InferenceSession(model_path)
img = cv2.imread(img_path) img = cv2.resize(img, (608, 608))
img = img / 255.0
img_data = np.array(img, dtype=np.float32)Add batch axis
img_data = np.expand_dims(img_data, axis=0)
outputs = session.run(None, {'images': img_data})
class_counts = np.zeros(len(CLASSES))
for output in outputs:
# postprocess for i in indices: class_id = int(classes[i]) class_counts[class_id] += 1with open('tj.txt', 'w') as f: for i, count in enumerate(class_counts): f.write(f'{CLASSES[i]}: {count}\n')
return class_counts
if name == 'main':
parser = argparse.ArgumentParser() parser.add_argument('--model', type=str, default='YOLOv8x.onnx') parser.add_argument('--image', type=str, default='bus.jpg') args = parser.parse_args()
class_counts = detect(args.model, args.image)
- Assistant
print(class_counts)
- Assistant
To run the code, you need to provide the path to the ONNX model file and the path to the input image. You can do this by specifying the
--modeland--imagearguments when running the script. For example:python detect_objects.py --model YOLOv8x.onnx --image bus.jpgMake sure that the ONNX model file and the input image are located in the same directory as the script or provide the full paths to these files. The script will then detect objects in the image using the specified model and output the count of each class of object detected.