如何使用 OpenCV 从 Yolo-NAS 中提取实时预测结果(标签、bbox、cof)

问题描述 投票:0回答:0
import cv2
import time
import torch
from ultralytics.yolo.utils.plotting import Annotator

yolo_nas_l = models.get("yolo_nas_m", pretrained_weights="coco")

cap = cv2.VideoCapture(input_video_path)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps_v = cap.get(cv2.CAP_PROP_FPS)

video_writer = cv2.VideoWriter(
                "Save.mp4",cv2.VideoWriter_fourcc(*"mp4v"),fps_v, (int(width), int(height)),)



device = 'cuda' if torch.cuda.is_available() else "cpu"
model=yolo_nas_l.to(device)

prevTime=0
while True:

    _, frame = cap.read()
    if not _:
        break
    annotator = Annotator(frame)
    images_predictions = model.predict(frame)
    #image_prediction = next(iter(images_predictions))
    for image_prediction in images_predictions:
      image = image_prediction.image
      labels = image_prediction.prediction.labels
      confidence = image_prediction.prediction.confidence
      bboxes = image_prediction.prediction.bboxes_xyxy

      for i in range(len(labels)):
          conf=str(round(confidence[i],2))
          lab=labels[i]

          annotator.box_label(bboxes[i],f"{lab} {conf}")

    currTime = time.time()
    fps = 1 / (currTime - prevTime)
    prevTime = currTime
    cv2.line(frame, (20, 25), (127, 25), [85, 45, 255], 30)
    cv2.putText(frame, f'FPS: {int(fps)}', (11, 35), 0, 1, [225, 255, 255], thickness=2, lineType=cv2.LINE_AA)

    video_writer.write(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

检查一下。 Yolo-NAS(最先进的对象检测模型)与 OpenCV 用于实时预测。与 YoloV8 模型相比,预测结果令人惊叹,具有更高的推理速度和预测精度。

cv2.VideoCapture(0)

使用网络摄像头进行实时预测

查看此链接了解更多详情文本

opencv real-time resultset yolo
© www.soinside.com 2019 - 2024. All rights reserved.