# pt 文件,pytorch 运行
import sys
from time import time
from ultralytics import YOLO
import cv2
# Load a model
model = YOLO("yolov8n.pt") # load an official model
model = model.to("cuda")
video_path = "C:/Users/username/Desktop/学姐向你发出了约会邀请❤怦然心动❤.1542979357.mp4"
cap = cv2.VideoCapture(video_path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
total_time = 0
while True:
ret, frame = cap.read()
if not ret:
break # Exit the loop if no more frames are available
# frame = cv2.resize(frame, (640, 640))
start_time = time()
results = model(frame, verbose=False, classes=[0])
total_time += time() - start_time
cv2.imshow("frame", results[0].plot())
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 打印进度
finished_frames = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
sys.stdout.write("\r 进度: %.2f %%" % (finished_frames / total_frames * 100))
sys.stdout.flush()
# results = model(frame, verbose=False,classes=[0]) CPU FPS: 14.687764775223318
# results = model(frame, verbose=False,classes=[0]) FPS: 64.14847574515913
# results = model(frame, verbose=False) FPS: 60.23244793078058
# 2024-05-22 23:32:05 FPS: 60.21048703460454 CUDA
print("\nFPS: ", total_frames / total_time)
这段代码在 22 号还是正常的,输出就是“进度:”滚动,然后最后输出一行 FPS ,cv2.imshow 那三行还没写。
刚才去洗了个澡,加上了 cv2.imshow 那三行,又跑了一次,输出变成了
python yolo-torch.py
C:\Users\username\Documents\code\project\.venv\Lib\site-packages\torch\nn\modules\conv.py:456: UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at ..\aten\src\ATen\native\cudnn\Conv_v8.cpp:919.)
return F.conv2d(input, weight, bias, self.stride,
进度: 99.85 %
FPS: 75.04395604987724
进度: 99.85 %
FPS: 21.148407567206448
终端先是滚动一边进度,非常快,然后打印了一行 FPS ,但是这个时候 cv2 的 imshow 没有展示出来,
在第一个 FPS 打印完之后,又开始滚动输出“进度:”,这次 cv2.imshow 会正常显示,然后最后输出一个 FPS 。
但是这俩个 FPS 都和 22 号的值差了不少,22 号的时候一直稳定 60 上下
然后我注释了 # 打印进度下面的三行,输出变成了
python yolo-torch.py
C:\Users\username\Documents\code\project\.venv\Lib\site-packages\torch\nn\modules\conv.py:456: UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at ..\aten\src\ATen\native\cudnn\Conv_v8.cpp:919.)
return F.conv2d(input, weight, bias, self.stride,
进度: 99.85 %
FPS: 72.13356492043005
FPS: 21.089225686164987
为什么啊。我代码一行一行看过去,也看不出来为什么 while 循环会运行俩次,第一次还是我昨天的版本?