feat(yolo): 添加多种YOLO模型应用示例- 添加图像分类、实例分割、姿态估计和旋转目标检测示例

- 实现目标模糊处理功能
- 更新模型加载路径和相关配置
-重命名部分示例文件编号- 添加moviepy依赖项
- 移除不必要的numpy导入和类别文件
This commit is contained in:
mshe 2025-09-26 00:37:24 +08:00
parent 73dd4373cb
commit b388083559
30 changed files with 72 additions and 11 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@
.idea
yolo
runs
models/yolov8x.pt
models/

View File

@ -1,5 +1,5 @@
from ultralytics import YOLO
# 文档https://docs.ultralytics.com/zh/
# model = YOLO('models/yolov8n.pt')
model = YOLO('models/yolov8x.pt')
# model('./resources/01.png',show=True,save=True)

View File

@ -1,5 +1,8 @@
from ultralytics import YOLO
# 加载预训练模型
model = YOLO('models/yolov8x.pt')
model.train(data='train.yaml', epochs=200, imgsz=640, batch=32, device='cpu')
model = YOLO('models/yolov8n-cls.pt')
results = model('./resources/图像分类.png',save=True)
for result in results:
probs = result.probs
print(probs)

8
04.实例分割.py Normal file
View File

@ -0,0 +1,8 @@
from ultralytics import YOLO
# 加载预训练模型
model = YOLO('models/yolov8n-seg.pt')
results = model('./resources/图像分类.png',save= True)
for result in results:
masks = result.masks
print(masks)

8
05.姿态估计.py Normal file
View File

@ -0,0 +1,8 @@
from ultralytics import YOLO
# 加载预训练模型
model = YOLO('models/yolo11n-pose.pt')
results = model('./resources/姿态估计',save= True)
for result in results:
keypoints = result.keypoints
print(keypoints)

8
06.旋转目标检测.py Normal file
View File

@ -0,0 +1,8 @@
from ultralytics import YOLO
# 加载预训练模型
model = YOLO('models/yolo11n-obb.pt')
results = model('./resources/图像分类.png',save= True)
for result in results:
obb = result.obb
print(obb)

38
07.目标模糊处理.py Normal file
View File

@ -0,0 +1,38 @@
import cv2
from ultralytics import YOLO
# 加载预训练的YOLOv8模型
model = YOLO('models/yolo11n.pt')
# 打印模型能够识别的所有物体类别名称
print(model.names)
# 初始化摄像头参数1表示使用默认的第二个摄像头如果只有一个摄像头通常使用0
cap = cv2.VideoCapture(1)
# 循环读取摄像头帧,直到摄像头关闭
while cap.isOpened():
# 读取一帧图像
ret, frame = cap.read()
# 如果没有成功读取帧,则跳出循环
if not ret:
break
# 使用YOLO模型对当前帧进行目标检测
results = model.predict(frame)
# 从检测结果中提取边界框坐标xyxy格式并转换为列表
# 注意:这里原代码有重复赋值的错误,已修正
boxes = results[0].boxes.xyxy.cpu().tolist()
# 遍历所有检测到的边界框
for box in boxes:
# 从frame中截取边界框区域的图像
# 格式为[y1:y2, x1:x2]其中box[0]是x1, box[1]是y1, box[2]是x2, box[3]是y2
obj = frame[int(box[1]):int(box[3]), int(box[0]):int(box[2])]
# 对截取的图像区域进行高斯模糊处理模糊核大小为70x70
# 然后将模糊后的图像重新赋值给原位置,实现目标模糊效果
frame[int(box[1]):int(box[3]), int(box[0]):int(box[2])] = cv2.blur(obj, (70, 70))
# 显示处理后的图像窗口
cv2.imshow("YOLOv8 Inference", frame)
# 等待键盘输入1毫秒如果按下'q'键则退出循环
if cv2.waitKey(1) == ord('q'):
break
# 释放摄像头资源
cap.release()
# 关闭所有OpenCV创建的窗口
cv2.destroyAllWindows()

View File

@ -8,7 +8,7 @@ import numpy as np
# 加载训练好的模型
# best.pt: 最佳模型,适用于生产
# last.pt: 最后一轮训练的模型,适用于继续训练
yolo = YOLO('runs/detect/train6/weights/best.pt')
yolo = YOLO('runs/detect/train7/weights/best.pt')
# 指定屏幕范围
# x,y,width,height 全屏None

View File

@ -1,5 +1,4 @@
import cv2
import numpy as np
from ultralytics import YOLO
from moviepy import VideoFileClip # 用于处理音频(仅视频文件需要)

View File

@ -1 +0,0 @@
logo

Binary file not shown.

Before

Width:  |  Height:  |  Size: 390 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 454 KiB

Binary file not shown.

View File

@ -1 +0,0 @@
0 0.267786 0.495737 0.158273 0.103739

View File

@ -1,2 +0,0 @@
0 0.268585 0.198019 0.163070 0.034106
0 0.499600 0.501421 0.001599 0.001421

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 390 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 454 KiB

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,5 @@
ultralytics~=8.3.198
opencv-python~=4.12.0.88
numpy~=2.0.2
pyautogui
pyautogui
moviepy

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

Binary file not shown.