https://youtube.com/watch?v=kEcWUZ8unmc%3Fenablejsapi%3D1%26autoplay%3D0%26cc_load_policy%3D0%26iv_load_policy%3D1%26loop%3D0%26modestbranding%3D0%26fs%3D1%26playsinline%3D0%26controls%3D1%26color%3Dred%26cc_lang_pref%3D%26rel%3D1%26autohide%3D2%26theme%3Ddark%26

In this tutorial, we will look at installing YOLO v8 on Mac M1, how to write the code from scratch, and how to run it on a video. We will also see how to manage the graphics card for the best possible performance. We will use YOLO v8 from ultralyticsc for object detection.

Installation Of YOLO V8 On Mac M1

To be able to use the YOLO v8 on Mac M1 object detection algorithm we have to download and install Yolo v8 first. Open Mac’s terminal and write

pip install ultralytics

if the installation gives no errors you are ready for the next step

Terminal Mac M1 Yolo v8

Let’s Start With OpenCV

We can start writing code on our Mac M1. We decided to apply object detection with Yolo v8 on a video so let’s start with processing the video. With OpenCV the video is processed as a sequence of images, so we import the video and prepare the loop

import cv2

cap = cv2.VideoCapture(“dogs.mp4”)

while True:

ret, frame = cap.read()

if not ret:

break

cv2.imshow(“Img”, frame)

key = cv2.waitKey(1)

if key == 27:

break

cap.release()

cv2.destroyAllWindows()

This is the result of the first test, the video starts regularly so you can proceed to the next step.

OpenCV Video

Import YOLO V8 And The Object Detection Model

We import Yolo v8 from ultralytics with the template yolov8m.pt and thus obtain the coordinates of the bounding box related to the two dogs in the video.

from ultralytics import YOLO

import numpy as np

cap = cv2.VideoCapture(“dogs.mp4”)

model = YOLO(“yolov8m.pt”)

results = model(frame)

result = results[0]

bboxes = result.boxes.xyxy

print(boxes)

Running the code will extract the coordinates from the first frame so we can immediately assess how it is progressing and if there are any errors

Mac M1 Yolo V8 tensor coordinates

As you can see the extracted coordinates are tensor and cannot be used inside Opencv with this format so we import the numpy library and proceed with the conversion.

bboxes = np.array(result.boxes.xyxy.cpu(), dtype=”int”)

classes = np.array(result.boxes.cls.cpu(), dtype=”int”)

Let’s transfer this information to the image

for cls, bbox in zip(classes, bboxes):

(x, y, x2, y2) = bbox

cv2.rectangle(frame, (x, y), (x2, y2), (0, 0, 225), 2)

cv2.putText(frame, str(cls), (x, y – 5), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 225), 2)

And this is the result. You can see the bounding box on both dogs and in the upper left corner the number 16. This number corresponds to the position of the class (dog) in the list of 80 classes in the pre-train model with coco dataset

Speed Tests

Now we come to the most interesting point of this tutorial, the test of YOLO v8 on Mac M1 with CPU and graphics card usage and comparison with Nvidia graphics card on windows.

1. YOLO V8 On Mac M1 On CPU

Using the basic setting and applying no changes to speed it up 482.2 ms per frame, does not seem to be an outstanding result

Mac m1 CPU speed test

2. YOLO V8 On Mac M1 On Graphic Card

To use these functions we need to verify that mps backend on pytorch is enabled.

import torch

print(torch.backends.mps.is.available())

In my case it’s active and here are the test results

mps backend Mac M1

If you also display True then insert this line on the code and you will take full advantage of the speed of your Mac M1 with Yolo v8

results = model(frame, device=”mps”)

Running the code you can see that the video is processed virtually in real-time. Checking also the reference speed we also see a marked improvement, in fact in this case it goes to 42.2 ms per frame

Mac M1 GPU and YOLO v8

3. YOLO V8 On Nvidia RTX 3060

Let’s do the test on Windows on an Nvidia RTX 3060 Graphics Card. We use the same code but we only need to change this line

results = model(frame, device=”0″)

As you can see in the image below the test resulted in about 18 ms per frame. It should be pointed out that the code could still be optimized for this GPU because its utilization is at 50%

YOLO v8 on Nvidia RTX 3060

ที่มา: https://pysource.com/2023/03/28/object-detection-with-yolo-v8-on-mac-m1/

ใส่ความเห็น