当前位置:网站首页 > 更多 > 编程开发 > 正文

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

作者:CC下载站 日期:2023-10-11 19:17:34 浏览:30 分类:编程开发

一、开发环境

1. PyCharm 【点击下载

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

2. Python3.9 【点击下载

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

注:最新版本是 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

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

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


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

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

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

原因分析:

  1. 可能是你的 PyCharm 与 cmd 使用的 Python 解释器不相同;

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


解决方法:

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

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

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

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

三、基于 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 模型点击下载

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

四、Yolov8 + CPU 物体检测效果展示 

[编程相关] Python 基于 Yolov8 + CPU 实现物体检测

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯