若该文为原创文章,转载请注明原文出处。
原本是为部署RK3568而先熟悉yolov10流程的,采用自己的数据集,网上很多,检测竹签,并计数。
1、环境搭建
1.1 官方下载源码
官网地址:YOLOv10 gitbub官网源码
利用魔法进入GitHub官网之后点击下载源码压缩包(这里针对小白使用download,当然也可以使用git clone命令)
1.2 配置环境
使用的是AutoDL平台
创建虚拟环境
创建虚拟环境
conda create -n yolov10 python=3.9
初始化
source activate
激活
conda activate yolov10
安装前需要修改requirements.txt文件,把文件里的onnxruntime-gpu==1.18.0改成onnx
runtime-gpu==1.16.0
开始安装
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
2、测试
测试直接使用官方模型测试
模型下载命令
wget https://github.com/THU-MIG/yolov10/releases/download/v1.1/yolov10{n/s/m/b/l/x}.pt
这里使用的是 yolov10s.pt。
测试指令
yolo predict model=yolov10s.pt source=ultralytics/assets/bus.jpg
或是使用代码测试
from ultralytics import YOLOv10
# Load a pretrained YOLOv10n model
model = YOLOv10("./weights/zhuqian.pt")
# Perform object detection on an image
results = model.predict("./test.jpg")
# Display the results
results[0].show()
results[0].save()
3、训练
3.1下载数据集
训练的是自己的数据集,想实现的是数竹签,需要数据集自行下载。
链接:https://pan.baidu.com/s/1paB9rDH8PUBNinw8DzLPiQ?pwd=1234
提取码:1234 复制这段内容后打开百度网盘手机App,操作更方便哦
直接把数据集解压到yolov10工程目录下。
文件结构如下
3.2 yaml文件
进入yolov10\ultralytics\cfg\datasets目录,拷贝coco128.yaml文件一份为mycoco128.yaml。
修改mycoco128.yaml, 修改后的文件,修改数据集路径和修改标签。
# Ultralytics YOLO 🚀, AGPL-3.0 license
# COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
# Documentation: https://docs.ultralytics.com/datasets/detect/coco/
# Example usage: yolo train data=coco128.yaml
# parent
# ├── ultralytics
# └── datasets
# └── coco128 ← downloads here (7 MB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: G:/资料/嵌入式/【正点原子】RK3568开发板资料(A盘)-基础资料/01、AI例程/Ai_Code/40_yolov10/yolov10/dataset # dataset root dir
train: images/train # train images (relative to 'path') 128 images
val: images/val # val images (relative to 'path') 128 images
test: # test images (optional)
# Classes
names:
0: skewer
# Download script/URL (optional)
#download: https://ultralytics.com/assets/coco128.zip
3.3 训练
使用下面命令训练。
yolo detect train data=/root/yolov10/ultralytics/cfg/datasets/mycoco128.yaml model=yolov10s.pt epochs=100 batch=16 imgsz=640
或代码训练
#coding:utf-8
from ultralytics import YOLOv10
# 模型配置文件
model_yaml_path = "ultralytics/cfg/models/v10/yolov10s.yaml"
#数据集配置文件
data_yaml_path = '/root/yolov10/ultralytics/cfg/datasets/coco128_zhuqian.yaml '
#预训练模型
pre_model_name = 'yolov10s.pt'
if __name__ == '__main__':
#加载预训练模型
model = YOLOv10(model_yaml_path).load(pre_model_name)
#训练模型
results = model.train(data=data_yaml_path,
epochs=150,
batch=16,
name='train_v10')
这里有个疑问,训练时好像不是使用yolov10s.pt模型,而是会下载yolov8n.pt模型。
4 结果测试
命令测试
yolo predict model=weights/zhuqian_no.pt source=test.py
命令测试比较方便,但如果想自己写,还是要使用API。
主要还是要学习怎么使用API
import cv2
from ultralytics import YOLOv10
import os
model = YOLOv10(model="./weigths/best_zq.pt")
def predict(chosen_model, img, classes=[], conf=0.5):
if classes:
results = chosen_model.predict(img, classes=classes, conf=conf)
else:
results = chosen_model.predict(img, conf=conf)
return results
def predict_and_detect(chosen_model, img, classes=[], conf=0.5, rectangle_thickness=2, text_thickness=1):
results = predict(chosen_model, img, classes, conf=conf)
count = 0
for result in results:
for box in result.boxes:
cv2.rectangle(img, (int(box.xyxy[0][0]), int(box.xyxy[0][1])),
(int(box.xyxy[0][2]), int(box.xyxy[0][3])), (255, 0, 0), rectangle_thickness)
#cv2.putText(img, f"{result.names[int(box.cls[0])]}",
# (int(box.xyxy[0][0]), int(box.xyxy[0][1]) - 10),
# cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), text_thickness)
count += 1
# 在图像上显示计数
font = cv2.FONT_HERSHEY_SIMPLEX
text = f"Count: {count}"
cv2.putText(img, text, (10, 160), font, 5, (00, 00, 255), 4, cv2.LINE_AA)
return img, results
image_folder = './dataset/images/train/' # 图片文件夹路径
for image_name in os.listdir(image_folder):
if image_name.endswith('.jpg') or image_name.endswith('.png'):
image_path = os.path.join(image_folder, image_name)
# read the image
image = cv2.imread(image_path)
result_img, _ = predict_and_detect(model, image, classes=[], conf=0.4)
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.moveWindow("Image", 400, 100)
cv2.imshow("Image", result_img)
#cv2.imwrite("result.jpg", result_img)
cv2.waitKey(240) # 每张图片显示500毫秒
cv2.waitKey(0)
"""
output_filename = "YourFilename"
writer = create_video_writer(cap, output_filename)
video_path = r"YourVideoPath"
cap = cv2.VideoCapture(video_path)
while True:
success, img = cap.read()
if not success:
break
result_img, _ = predict_and_detect(model, img, classes=[], conf=0.5)
writer.write(result_img)
cv2.imshow("Image", result_img)
cv2.waitKey(1)
writer.release()
"""
代码比较简单,基本和yolo其他版本差不多。
测试结果