yoloV8 官方地址 预测 -Ultralytics YOLO 文档
1.图片预测
from ultralytics import YOLO
#### 图片预测1
### https://www.youtube.com/watch?v=neBZ6huolkg
### https://github.com/ultralytics/ultralytics
### https://github.com/abdullahtarek/football_analysismode = YOLO("yolov8x.pt")
##将视频数据放入到模型中预测
#result = mode.predict('input_videos/08fd33_4.mp4', save=True)
results = mode.predict('input_image/111.png', save=True)##print("================")
# for box in result[0].boxes:
# print(box)#https://docs.ultralytics.com/modes/predict/#key-features-of-predict-mode
for result in results:boxes = result.boxes # Boxes object for bounding box outputsmasks = result.masks # Masks object for segmentation masks outputskeypoints = result.keypoints # Keypoints object for pose outputsprobs = result.probs # Probs object for classification outputsobb = result.obb # Oriented boxes object for OBB outputsresult.show() # display to screenresult.save(filename="result.png") # save to disk
2.视频预测
import cv2from ultralytics import YOLO# Load the YOLOv8 model
model = YOLO("../yolov8n.pt")# Open the video file
video_path = "D:/workspace/ultralytics/input_videos/08fd33_4.mp4"
cap = cv2.VideoCapture(video_path)if not cap.isOpened():print("打开摄视屏失败!")# Loop through the video frames
while cap.isOpened():# Read a frame from the videosuccess, frame = cap.read()if success:print("视频读帧成功!")# Run YOLOv8 inference on the frameresults = model(frame)# Visualize the results on the frameannotated_frame = results[0].plot()# Display the annotated framecv2.imshow("YOLOv8 Inference", annotated_frame)# Break the loop if 'q' is pressedif cv2.waitKey(1) & 0xFF == ord("q"):breakelse:print("视频读帧失败!")# Break the loop if the end of the video is reachedbreak# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
3.项目结构