Python implements object detection based on Yolov8 + CPU

Table of contents

1. Development environment

2. Install Python object detection related library based on Yolov8

2.1 Open the command prompt (cmd) or terminal and install the library 

2.2 Problems encountered during the installation process of related libraries 

3. Implementation of object detection code based on Yolov8 (complete)

3.1 Yolov8 complete object detection code

3.2 Solution to the slow download of the yolov8 model when the code is run for the first time

4. Yolov8 + CPU object detection effect display 


1. Development environment

1. PyCharm [ Click to download ]

2. Python3.9 [ Click to download ]

Note: The latest version is Python 3.11.5, you can download it according to the actual situation.

2. Install Python object detection related library based on Yolov8

ultralytics==8.0.26 

opencv-python==4.5.4.60

cvzone==1.5.6

math

time

2.1 Open the command prompt (cmd) or terminal and install the library 

1. Enter the following command to install the ultralytics library: 

pip install ultralytics==8.0.26

2. Enter the following command to install the cv2 library (OpenCV):

pip install opencv-python==4.5.4.60 

3. Enter the following command to install the cvzone library:

pip install cvzone==1.5.6 

4. Enter the following command to install the math library (Python built-in library, no additional installation required):

pip install math 

5. The time library is a built-in library of Python and does not require additional installation. 

2.2 Problems encountered during the installation process of related libraries 

Problem description 1:  When installing the ultralytics library, an error message appears: ERROR: Operation canceled by user

Cause analysis:  The reason for these errors is that the network environment is not good, and the download speed of the library is very slow and may be disconnected midway, causing the download to fail.

Solution:  The solution is to change the download environment to a better one. If the environment cannot be changed, keep retrying the installation until it succeeds: pip install ultralytics==8.0.26 

Problem description 2:  Related libraries such as ultralytics have been installed successfully, but Pycharm cannot detect them.

Cause Analysis:

  1. It may be that the Python interpreter used by your PyCharm and cmd is different;
  2. PyCharm uses the same Python interpreter as cmd, but the associated libraries are not added to the PyCharm environment.

Solution:

1. First make sure that your PyCharm and cmd use the same Python interpreter:

2. Add the associated library to the PyCharm environment:

3. Implementation of object detection code based on Yolov8 ( complete )

3.1 Yolov8 complete object detection code

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 Solution to the slow download of the yolov8 model when the code is run for the first time

1. The Yolov8 model used for object detection in this chapter is implemented based on yolov8n.pt;

2. When the code is run for the first time, the relevant model will be downloaded from Github to the local;

3. If the network environment is not good, the download speed may be very slow;

4. Therefore, it is recommended to stop running the code first, and then manually download the model from Github.

yolov8n.pt model [ click to download ]

4. Yolov8 + CPU object detection effect display 

Guess you like

Origin blog.csdn.net/m0_37383484/article/details/133387751