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_analysis
mode = 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 outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
obb = result.obb # Oriented boxes object for OBB outputs
result.show() # display to screen
result.save(filename="result.png") # save to disk
2.视频预测
import cv2
from 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 video
success, frame = cap.read()
if success:
print("视频读帧成功!")
# 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:
print("视频读帧失败!")
# 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()
3.项目结构