1.首先安装Anaconda
,由于我的设备是Mac M1
还不是那么容易,之前一直安装官网最新的包,结果报什么兼容的错误,然后找到了这个blog.csdn.net/weixin_4550… ,下老版本的包就能安装上了
2.然后再在Anaconda新建个yolo
的环境,从终端启动,安装对应的工具就可以了
3.直接使用自带的模型
import torch
import cv2
from PIL import Image
# 加载模型
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# 加载图片
image_path = '/Users/mac/Desktop/AAA.jpeg'
image = Image.open(image_path)
# 进行检测
results = model(image)
# 获取人类对象
human_results = results.pred[0][results.pred[0][:, 5] == 0]
# 打印检测结果
for idx, det in enumerate(human_results):
bbox = det[:4].cpu().numpy()
confidence = det[4].item()
x, y, w, h = bbox
print(f'Person {idx+1}: X={x}, Y={y}, Width={w}, Height={h}, Confidence={confidence}')
# 可视化结果
results.show()