Ultralytics 找不到来源

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

所以我试图运行一个简单的脚本来测试 ultralytics 并看看它的 yolov8 模型是如何工作的。 我按照该视频中的步骤操作,但是当我运行此脚本时:

from ultralytics import YOLO
import cv2

model = YOLO('yolov8n.pt')

video = './Vine_Golvo.mp4'
capture = cv2.VideoCapture(video)

condition = True

# read frames
while condition:
    condition, frame = capture.read()   # condition is updated to true or false, depending on wether or not 
                                        # a frame was succesfully retrieved (will be false after the last frame of the video)
    # frame saves the data of the current frame
    
    # track objects
    results = model.track(frame, persist=True)
    
    # plot results
    frame_ = results[0].plot()
    
    # visualize
    cv2.imshow('Paul', frame_)
    if cv2.waitKey(25) & 0xFF == ord('q'):  # the second thing says that we close the program when you press 'q'
        break

弹出此错误:

C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Scripts\\python.exe "C:\\Users\\Costin\\Desktop\\Programare\\Materiale limbaje de programare\\Python\\Image recognition\\Icyu\\main.py"
WARNING ⚠️ 'source' is missing. Using 'source=C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\assets'.

image 1/2 C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\assets\\bus.jpg: 640x480 3 persons, 1 bus, 63.0ms
Traceback (most recent call last):
File "C:\\Users\\Costin\\Desktop\\Programare\\Materiale limbaje de programare\\Python\\Image recognition\\Icyu\\main.py", line 18, in \<module\>
results = model.track(frame, persist=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\engine\\model.py", line 481, in track
return self.predict(source=source, stream=stream, \*\*kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\engine\\model.py", line 441, in predict
return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\engine\\predictor.py", line 168, in __call__
return list(self.stream_inference(source, model, \*args, \*\*kwargs))  # merge list of Result into one
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\torch\\utils_contextlib.py", line 56, in generator_context
response = gen.send(request)
^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\engine\\predictor.py", line 256, in stream_inference
self.run_callbacks("on_predict_postprocess_end")
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\engine\\predictor.py", line 393, in run_callbacks
callback(self)
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\trackers\\track.py", line 69, in on_predict_postprocess_end
tracks = tracker.update(det, im0s\[i\])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\trackers\\byte_tracker.py", line 293, in update
warp = self.gmc.apply(img, dets)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\trackers\\utils\\gmc.py", line 102, in apply
return self.applySparseOptFlow(raw_frame)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\Costin\\Desktop\\Virtualenvs\\yolov8\\Lib\\site-packages\\ultralytics\\trackers\\utils\\gmc.py", line 329, in applySparseOptFlow
matchedKeypoints, status, \_ = cv2.calcOpticalFlowPyrLK(self.prevFrame, frame, self.prevKeyPoints, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.9.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\video\\src\\lkpyramid.cpp:1394: error: (-215:Assertion failed) prevPyr\[level \* lvlStep1\].size() == nextPyr\[level \* lvlStep2\].size() in function 'cv::\`anonymous-namespace'::SparsePyrLKOpticalFlowImpl::calc'

Process finished with exit code 1

我最初在 VsCode 中运行这个,但由于教程使用了 PyCharm(而且我过去在使用 VsCode 时也遇到过问题),我想可能是编辑器和环境出了问题。但事实并非如此,使用虚拟环境时,PyCharm 中也会弹出相同的错误。

python object-detection yolo yolov8 ultralytics
1个回答
0
投票

问题是,即使 conditionfalse 并且 capture 没有返回任何帧(如视频结尾处),您的代码仍会尝试在 while 条件之前跟踪此不存在的帧将完成其工作:

while condition:
    condition, frame = capture.read() 
    results = model.track(frame, persist=True)

因此,我建议使用 Ultralytics 文档中的使用示例:https://docs.ultralytics.com/modes/track/#persisting-tracks-loop。此代码将在两种情况下结束该过程:当视频结束时或按下“q”时,并且不会尝试处理视频结束后的帧。

import cv2
from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO('yolov8n.pt')

# Open the video file
video_path = "./Vine_Golvo.mp4"
cap = cv2.VideoCapture(video_path)

# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()

    if success:
        # Run YOLOv8 tracking on the frame, persisting tracks between frames
        results = model.track(frame, persist=True)
        # Visualize the results on the frame
        annotated_frame = results[0].plot()
        # Display the annotated frame
        cv2.imshow("Paul", annotated_frame)
        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
      break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.