如何修复从YOLO v8内存中提取数据的错误

问题描述 投票:0回答:1

我与YOLO8合作 我的任务是确定汽车的轮廓(即将它们显示为矩形),然后进行计数(为了计数:轮廓必须穿过框架的中间 对于不同交通的两条车道:迎面驶来和超车 最后统计一下汽车总数 每次循环并请求下一帧,并且应该单独完成工作 目前,我正在尝试绘制轮廓,但视频上没有任何内容。 在输出中,我得到如下内容: `0: 384x640 27 辆汽车、1 辆卡车、1 台电视、93.0ms 速度:形状 (1, 3, 384, 640) 下每张图像的预处理时间为 1.9 毫秒,推理时间为 93.0 毫秒,后处理时间为 0.4 毫秒

0:384x640 26辆汽车,2辆卡车,88.1ms 速度:形状 (1, 3, 384, 640) 下每张图像的预处理时间为 1.7 毫秒,推理时间为 88.1 毫秒,后处理时间为 0.5 毫秒

0:384x640 26 辆汽车,2 辆卡车,104.2 毫秒 速度:形状 (1, 3, 384, 640) 的每个图像 1.5 毫秒预处理,104.2 毫秒推理,0.6 毫秒后处理`

我的代码:

import cv2
from ultralytics import YOLO

model = YOLO('yolov8s.pt')

video_path = 'output2.avi'
video = cv2.VideoCapture(video_path)

if not video.isOpened():
    print("Ошибка: Не удается открыть видео.")
    exit()

current_frame = 0

while True:
    ret, frame = video.read()
    if not ret:
        break

    current_frame += 1

    results = model(frame)

    car_results = [detection for detection in results if detection[-1] in [2, 5, 7]]

    for result in car_results:
        x1, y1, x2, y2, confidence, class_id = result.xyxy[0]
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)

    cv2.imshow('Video', frame)

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

video.release()
cv2.destroyAllWindows()

我尝试了各种方法来解决这个问题,但所有的尝试都不成功

python yolo yolov8
1个回答
0
投票

对 Yolov8 Results 对象是什么存在误解。替换脚本中的这些行:

results = model(frame)

car_results = [detection for detection in results if detection[-1] in [2, 5, 7]]
for result in car_results:
    x1, y1, x2, y2, confidence, class_id = result.xyxy[0]

以下代码:

results = model(frame)

for result in results:
    for box in result.boxes:
        class_id = int(box.cls.item())
        if class_id in [2, 5, 7]:
            x1, y1, x2, y2 = box.xyxy.tolist()[0]

类似问题的更详细答案:https://stackoverflow.com/a/77733979/16071964

© www.soinside.com 2019 - 2024. All rights reserved.