YOLO v8 on mac m1/classes.txt
person
bicycle
car
motorbike
airplane
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
YOLO v8 on mac m1/main.py
# yolov8快速使用说明
# python -m venv venv
# source venv/Scripts/activate
# pip install ultralytics
# python main.py
import cv2 # 导入cv2库
from ultralytics import YOLO # 导入ultralytics库中的YOLO模块
import numpy as np # 导入numpy库
cap = cv2.VideoCapture("street.mp4") # 创建一个视频捕获对象,读取"street.mp4"文件
model = YOLO("yolov8m.pt") # 加载预训练好的YOLOv8模型
while True: # 循环地处理每一帧图像
ret, frame = cap.read() # 从视频捕获对象中读取一帧图像
if not ret: # 如果没有读取到图像,说明视频已经结束,跳出循环
break
# 配置使用mac book 的gpu加速(mps)
results = model(frame, device="mps") # 将图像输入到YOLOv8模型中,得到检测结果
result = results[0] # 取出第一个检测结果(如果有多个结果,表示有多个图像输入)
bboxes = np.array(result.boxes.xyxy.cpu(), dtype="int") # 将检测结果中的边界框坐标转换为numpy数组,并转换为整数类型
classes = np.array(result.boxes.cls.cpu(), dtype="int") # 将检测结果中的类别编号转换为numpy数组,并转换为整数类型
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) # 在原图像上绘制红色的类别标签
cv2.imshow("Img", frame) # 显示处理后的图像
key = cv2.waitKey(1) # 等待用户按键
if key == 27: # 如果用户按下ESC键,退出程序
break
cap.release() # 释放视频捕获对象
cv2.destroyAllWindows() # 销毁所有窗口