yolov8预测

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.项目结构

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 15:12:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 15:12:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 15:12:01       58 阅读
  4. Python语言-面向对象

    2024-07-15 15:12:01       69 阅读

热门阅读

  1. php file_get_contents https 请求 伪造user_agent

    2024-07-15 15:12:01       20 阅读
  2. HTML5 Input 验证身份证

    2024-07-15 15:12:01       23 阅读
  3. 使用jsencrypt在web前端对字符串进行Ras加密

    2024-07-15 15:12:01       22 阅读
  4. c# Bitmap

    2024-07-15 15:12:01       20 阅读
  5. C++ 分析一个链表是不是回文,有什么思路么

    2024-07-15 15:12:01       25 阅读