yolov8 下载及使用
YOLOV8下载及使用
- 1. YOLOV8环境配置
- 1.1 conda创建新的虚拟环境
- 1.2 查看创建的虚拟环境
- 1.3 激活虚拟环境
- 1.4 更新pip
- 1.5 清除缓存
- 1.6 安装ultralytics
- 2.使用
1. YOLOV8环境配置
1.1 conda创建新的虚拟环境
conda creat -name YOLOv8 python=3.9
1.2 查看创建的虚拟环境
conda env list
1.3 激活虚拟环境
conda activate YOLOv8
1.4 更新pip
防止下载版本过低而无法正常下载,此步骤也可以省略。
pip install --upgrade pip pip install setuptools wheel
1.5 清除缓存
这一步可以大大提过下载速度,防止安装OpenCV时过卡。
pip cache purge
1.6 安装ultralytics
其中,–verbose用来显示安装进度
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple --verbose
2.使用
from collections import defaultdictimport cv2
import numpy as npfrom ultralytics import YOLO# Load the YOLOv8 model
model = YOLO("yolov8n.pt")# Open the video file
video_path = "trafic.mp4"
cap = cv2.VideoCapture(video_path)# Store the track history
track_history = defaultdict(lambda: [])while cap.isOpened():# Read a frame from the videosuccess, frame = cap.read()if success:# Run YOLOv8 tracking on the frame, persisting tracks between framesresults = model.track(frame, persist=True)# Visualize the results on the frameannotated_frame = results[0].plot()# Display the annotated framecv2.imshow("YOLOv8 Tracking", annotated_frame)# Break the loop if 'q' is pressedif cv2.waitKey(1) & 0xFF == ord("q"):breakelse:# 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()