yolov8快速使用指南
简单的做一个yolov8(cpu)的使用教程。第一步,python版本必须3.8以上(我用的是3.9)。
第二步,通过pip命令下载ultralytics,也可以直接通过pycharm的包管理工具来下载。哪个库安装失败就单独pip一下。
Requirement already satisfied: ultralytics in d:\pythonproject\venv\lib\site-packages (8.2.18)
Requirement already satisfied: matplotlib>=3.3.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (3.7.3)
Requirement already satisfied: opencv-python>=4.6.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (4.9.0.80)
Requirement already satisfied: pillow>=7.1.2 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (10.0.0)
Requirement already satisfied: pyyaml>=5.3.1 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (6.0.1)
Requirement already satisfied: requests>=2.23.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (2.31.0)
Requirement already satisfied: scipy>=1.4.1 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (1.11.2)
Requirement already satisfied: torch>=1.8.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (2.3.0)
Requirement already satisfied: torchvision>=0.9.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (0.18.0)
Requirement already satisfied: tqdm>=4.64.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (4.66.2)
Requirement already satisfied: psutil in d:\pythonproject\venv\lib\site-packages (from ultralytics) (5.9.8)
Requirement already satisfied: py-cpuinfo in d:\pythonproject\venv\lib\site-packages (from ultralytics) (9.0.0)
Requirement already satisfied: thop>=0.1.1 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (0.1.1.post2209072238)
Requirement already satisfied: pandas>=1.1.4 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (2.1.3)
Requirement already satisfied: seaborn>=0.11.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (0.13.2)
Requirement already satisfied: contourpy>=1.0.1 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (1.1.0)
Requirement already satisfied: cycler>=0.10 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (4.42.1)
Requirement already satisfied: kiwisolver>=1.0.1 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (1.4.5)
Requirement already satisfied: numpy<2,>=1.20 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (1.25.2)
Requirement already satisfied: packaging>=20.0 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (23.1)
Requirement already satisfied: pyparsing>=2.3.1 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (3.1.1)
Requirement already satisfied: python-dateutil>=2.7 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (2.8.2)
Requirement already satisfied: importlib-resources>=3.2.0 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (6.0.1)
Requirement already satisfied: pytz>=2020.1 in d:\pythonproject\venv\lib\site-packages (from pandas>=1.1.4->ultralytics) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in d:\pythonproject\venv\lib\site-packages (from pandas>=1.1.4->ultralytics) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (3.3.1)
Requirement already satisfied: idna<4,>=2.5 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (1.26.18)
Requirement already satisfied: certifi>=2017.4.17 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (2023.7.22)
Requirement already satisfied: filelock in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (3.13.1)
Requirement already satisfied: typing-extensions>=4.8.0 in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (4.8.0)
Requirement already satisfied: sympy in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (1.12)
Requirement already satisfied: networkx in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (3.2.1)
Requirement already satisfied: jinja2 in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (3.1.3)
Requirement already satisfied: fsspec in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (2023.12.2)
Requirement already satisfied: mkl<=2021.4.0,>=2021.1.1 in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (2021.4.0)
Requirement already satisfied: colorama in d:\pythonproject\venv\lib\site-packages (from tqdm>=4.64.0->ultralytics) (0.4.6)
Requirement already satisfied: zipp>=3.1.0 in d:\pythonproject\venv\lib\site-packages (from importlib-resources>=3.2.0->matplotlib>=3.3.0->ultralytics) (3.16.2)
Requirement already satisfied: intel-openmp==2021.* in d:\pythonproject\venv\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch>=1.8.0->ultralytics) (2021.4.0)
Requirement already satisfied: tbb==2021.* in d:\pythonproject\venv\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch>=1.8.0->ultralytics) (2021.12.0)
Requirement already satisfied: six>=1.5 in d:\pythonproject\venv\lib\site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->ultralytics) (1.16.0)
Requirement already satisfied: MarkupSafe>=2.0 in d:\pythonproject\venv\lib\site-packages (from jinja2->torch>=1.8.0->ultralytics) (2.1.3)
Requirement already satisfied: mpmath>=0.19 in d:\pythonproject\venv\lib\site-packages (from sympy->torch>=1.8.0->ultralytics) (1.3.0)
pip install ultralytics
第三步,前往github网站:https://github.com/ultralytics/ultralytics下载yolov8n.pt。
第四步,创建一个yolov8的项目,名称为yolo环境,详情如下:
yolo环境
--yolov8n.pt
--官方示例.py
第五步,向官方示例.py文件中添加内容如下:
from ultralytics import YOLO
# Create a new YOLO model from scratch
model = YOLO("yolov8n.yaml")
# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolov8n.pt")
# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=3)
# Evaluate the model's performance on the validation set
results = model.val()
# 百度上一个公交车的图片
results = model("https://i2.hdslb.com/bfs/archive/b4815f0dbfd250194b63789d87d66b6f2fd145b9.jpg")
# Export the model to ONNX format
success = model.export(format="onnx")
第六步,运行官方示例.py,完毕后会自动添加一些文件,结构如下:
yolo环境
--runs
----detect
------train
------train2
--名字太长就不写了.jpg
--yolov8n.pt
--官方示例.py
yolo
--datasets
----coco8
------images
--------train
--------val
------labels
--------train
--------val
这些文件自己看看就行,主要是先把官方示例给跑通了,中途可能有个报错onnx这个库没有,自己pip下载就行。
第七步,创建一个新的项目文件取名为yolov8,,把yolov8n.pt移到里面,在里面创建一个文件夹为dataset,在dataset里面创建两个文件夹分别是images和labels。
第八步,在百度上下载50张图片,图片上包含一个人和一个鸟,名字最好重命名一下,把图片放在在images文件夹里。
第九步,下载标注工具,pip install -U label-studio。可能会报错ERROR: Operation cancelled by user,以管理员身份运行pycharm就好了。这个安装的依赖挺多的,慢慢下载。
第十步,执行label-studio start,会提示Starting development server at http://0.0.0.0:8080/,打开链接。
第十一步,注册账号,登录。依次点击create project--Project Name(输入项目名称)--Description(输入描述)--点击Data Import--点击upload files(这里上传图片,把images里面的图片都全选)--全部上传完毕点击Labeling Setup--左边栏保持不变,中间选择第一行第三个飞机画框的那个点击一下--新页面中左边有一个add按钮,在上方输入person,点击add。输入bird,点击add,把按钮右侧无关标签删了。点击标签可以更换颜色。最后点击右上角save。转到新界面。--点击label all tasks--选择标签开始框选--点击框可以调节大小--框选完了点击submit,全部标注完成之后点击最上方的项目名称(这个是你自己创建的项目名),检查一下是否第三列都为1。点击右上角export。选择yolo,然后点击export。会自动下载。
第十二步,将下载到的文件分别替换为dataset里面的images和labels文件夹。在images和labels文件夹里各创建一个train和val文件夹。images的train里面留40个图片,val留10个图片,对应labels的train里面留40个txt,val留10个txt,记得名字要对应,不要乱分。
第十三步,在yolov8文件夹下面创建一个ceshi.yaml文件,内容如下
path: 'D:\pythonProject\yolov8\dataset\images'
train: 'train'
val: 'val'
nc: 2 #标签个数
names: [ 'person','bird' ] #添加标签的顺序要一致
第十三步,在yolov8文件夹下面创建一个训练.py,内容如下:
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
model.train(
data="ceshi.yaml",
epochs=100,#次数
imgsz=640,
device='cpu'
)
第十四步,运行训练.py,
第十五步,在yolov8文件夹下面创建一个检测.py,内容如下。同时把训练好的best.pt移动到yolov8文件下,下载一个图片命名为bus.jpg放到yolov8文件下
import cv2
from ultralytics import YOLO
model = YOLO("best.pt")
results = model.predict(
source="bus.jpg",# 被检测图片
device='cpu',
save=False,
conf=0.7,# 置信度>=0.7才显示出来
)
# 获取返回值中心坐标
def getRes(results):
res = {}
for r in results:
for i, detection in enumerate(r.boxes.xywh):
label = r.names)]
x = int(detection.item())
y = int(detection.item())
if label not in res:
res = []
res.append((x, y))
return res
# 将中心点坐标显示到图片中
res = getRes(results)
img = cv2.imread('bus.jpg')
for a in res:
for b in res:
img = cv2.circle(img, b, 5, (255, 0, 0), 5)
cv2.imshow('4556', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 框选
def result_show(res):
annotated_frame = res.plot()
cv2.imshow("YOLOv8 Inference", annotated_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
# result_show(results)
第十六步,运行检测.py。可以通过result_show(result)来看到被检测情况。 来几个图片啊 msn882 发表于 2024-5-23 16:21
来几个图片啊
公司电脑加密,发不了。会抽时间回家搞个视频教程,可以跟着视频学。 18834161486 发表于 2024-5-23 17:44
公司电脑加密,发不了。会抽时间回家搞个视频教程,可以跟着视频学。
提前谢谢大佬的教程 干什么用的?
burning 发表于 2024-5-23 21:37
提前谢谢大佬的教程
哔站搜索萌新本炘,刚出的的教程。 江男 发表于 2024-5-23 22:01
干什么用的?
目标检测,游戏方面的话主要是fps类游戏的自瞄。 感谢大佬分享 试LZ的方法,第6步时:
需要Downloading coco8.zip,100%后也不能通过。
AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n...
C:\ProgramData\miniconda3\envs\yolo\lib\site-packages\torch\nn\modules\conv.py:456: UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\cudnn\Conv_v8.cpp:919.) 18834161486 发表于 2024-5-23 17:44
公司电脑加密,发不了。会抽时间回家搞个视频教程,可以跟着视频学。
提前谢谢大佬的教程
页:
[1]
2