赞
踩
for
下面是一个使用 OpenCV () 和 YOLOv8 对视频帧运行推理的 Python 脚本。此脚本假定您已经安装了必要的软件包( 和 )。cv2opencv-pythonultralytics
- import cv2
- from ultralytics import YOLO
-
- # Load the YOLOv8 model
- model = YOLO('yolov8n.pt')
-
- # Open the video file
- video_path = "path/to/your/video/file.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 inference on the frame
- results = model(frame)
-
- # Visualize the results on the frame
- annotated_frame = results[0].plot()
-
- # Display the annotated frame
- cv2.imshow("YOLOv8 Inference", 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()

此脚本将对视频的每一帧运行预测,可视化结果,并将其显示在窗口中。可以通过按“q”退出循环。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。