Python 基于 Yolov8 实现物体检测

目录

一、开发环境

二、安装 Python 基于 Yolov8 物体检测关联库

2.1 打开命令提示符(cmd)或终端,安装库 

2.2 关联库安装过程遇到的问题 

三、基于 Yolov8 物体检测代码实现(完整)

3.1 Yolov8 物体检测完整代码

3.2 代码首次运行下载 yolov8 模型很慢解决方法

四、Yolov8 物体检测效果展示 


一、开发环境

1. PyCharm 【点击下载

2. Python3.9 【点击下载

注:最新版本是 Pyhton 3.11.5,大家根据实际情况下载即可。

二、安装 Python 基于 Yolov8 物体检测关联库

ultralytics==8.0.26 

opencv-python==4.5.4.60

cvzone==1.5.6

math

time

2.1 打开命令提示符(cmd)或终端,安装库 

1. 输入以下命令来安装 ultralytics 库: 

pip install ultralytics==8.0.26

2. 输入以下命令来安装 cv2 库(OpenCV):

pip install opencv-python==4.5.4.60 

3. 输入以下命令来安装 cvzone 库:

pip install cvzone==1.5.6 

4. 输入以下命令来安装math库(Python内置库,无需额外安装):

pip install math 

5. time 库是 python 内置库,无需额外安装。 

2.2 关联库安装过程遇到的问题 

问题描述1: 安装 ultralytics 库提示错误:ERROR: Operation cancelled by user

原因分析: 提示这些错误原因是网络环境不好,下载库的速度很慢中途可能断开了,而导致下载失败。

解决方法: 解决方法就是换好一点的环境下载,如果环境无法更换,就不断的重试安装直到成功为止:pip install ultralytics==8.0.26 

问题描述2: ultralytics 等关联库已经安装成功,但是 Pycharm 无法检测得到。

原因分析:

  1. 可能是你的 PyCharm 与 cmd 使用的 Python 解释器不相同;
  2. PyCharm 与 cmd 使用的 Python 解释器相同,但是关联的库并没有添加到 PyCharm 环境里。

解决方法:

1. 首先确保你的 PyCharm 与 cmd 使用的 Python 解释器相同:

2. 将关联的库添加到 PyCharm 环境里:

三、基于 Yolov8 物体检测代码实现(完整

3.1 Yolov8 物体检测完整代码

from ultralytics import YOLO
import cv2
import cvzone
import math
import time

cap = cv2.VideoCapture("motorbikes.mp4")  # For Video

model = YOLO("yolov8n.pt")

classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"
              ]

prev_frame_time = 0
new_frame_time = 0

while True:
    new_frame_time = time.time()
    success, img = cap.read()
    results = model(img, stream=True)
    for r in results:
        boxes = r.boxes
        for box in boxes:
            # Bounding Box
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            w, h = x2 - x1, y2 - y1
            cvzone.cornerRect(img, (x1, y1, w, h))
            # Confidence
            conf = math.ceil((box.conf[0] * 100)) / 100
            # Class Name
            cls = int(box.cls[0])

            cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1)

    fps = 1 / (new_frame_time - prev_frame_time)
    prev_frame_time = new_frame_time
    print(fps)

    cv2.imshow("Image", img)
    cv2.waitKey(1)

3.2 代码首次运行下载 yolov8 模型很慢解决方法

1. 本章物体检测使用的 Yolov8 模型是基于 yolov8n.pt 实现;

2. 代码在首次运行时,会从 Github 上下载相关模型到本地;

3. 如果网络环境不好的情况下,下载速度可能很慢;

4. 因此建议先停止运行代码,然后手动从 Github 将模型下载下来。

yolov8n.pt 模型点击下载

四、Yolov8 物体检测效果展示 

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于YOLOv8的车辆+车牌检测系统源码(部署教程+训练好的模型+各项评估指标曲线).zip 平均准确率:0.96 类别:car、plate 【资源介绍】 1、ultralytics-main ultralytics-main为YOLOv8源代码,里面涵盖基于yolov8分类、目标检测额、姿态估计、图像分割四部分代码,我们使用的是detect部分,也就是目标检测代码 2、搭建环境 安装anaconda 和 pycharm windows系统、mac系统、Linux系统都适配 在anaconda中新建一个新的envs虚拟空间(可以参考博客来),命令窗口执行:conda create -n YOLOv8 python==3.8 创建完YOLOv8-GUI虚拟空间后,命令窗口执行:source activate YOLOv8 激活虚拟空间 然后就在YOLOv8虚拟空间内安装requirements.txt中的所有安装包,命令窗口执行:pip install -r requirements.txt 使用清华源安装更快 3、训练模型过程 进入到\ultralytics-main\ultralytics\yolo\v8\detect\文件夹下,datasets即为我们需要准备好的数据集,训练其他模型同理。 data文件夹下的bicycle.yaml文件为数据集配置文件,该文件为本人训练自行车检测模型时创建,训练其他模型,可自行创建。博文有介绍https://blog.csdn.net/DeepLearning_?spm=1011.2415.3001.5343 train.py中238行,修改为data = cfg.data or './bicycle.yaml' # or yolo.ClassificationDataset("mnist") 237行修改自己使用的预训练模型 若自己有显卡,修改239行,如我有四张显卡,即改成args = dict(model=model, data=data, device=”0,1,2,3“) 以上配置完成后运行train.py开始训练模型,训练完毕后会在runs/detect/文件夹下生成train*文件夹,里面包含模型和评估指标等 4、推理测试 训练好模型,打开predict.py,修改87行,model = cfg.model or 'yolov8n.pt',把yolov8n.pt换成我们刚才训练完生成的模型路径(在\ultralytics-main\ultralytics\yolo\v8\detect\runs\detect文件夹下),待测试的图片或者视频存放于ultralytics\ultralytics\assets文件夹, 运行predict.py即可,检测结果会在runs/detect/train文件夹下生成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积步千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值