Ultralytics找不到源文件
我在尝试运行一个简单的脚本,想测试一下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里也出现了同样的错误。
1 个回答
0
如果这个错误在脚本运行一开始就出现,并且在成功处理任何视频帧之前就出现了,那可能是因为视频路径不正确。你可以试着使用完整的路径,而不是相对路径,甚至可以测试不同的视频,看看错误是否还存在。
如果错误发生在视频文件处理的最后,问题可能出在while循环的逻辑上:即使条件是false,而且capture.read()
没有返回任何帧(比如在视频结束时),你的代码仍然会尝试跟踪这个不存在的帧,直到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()