本次部署使用的框架基于ultralytics, 并且已经集成最新版本的YOLOv8框架:
一键环境配置
pip install ultralytics
基础使用
- 训练
from ultralytics import YOLOWorld
model = YOLOWorld('yolov8x-worldv2.pt')
results = model.train(data='coco8.yaml', epochs=100, imgsz=640)
results = model('path/to/bus.jpg')
- 推理
from ultralytics import YOLOWorld
model = YOLOWorld('yolov8s-worldv2.pt')
results = model.predict('path/to/image.jpg')
results[0].show()
- 自定义标签
- 我们定义了保温杯,马克杯,纸杯,抽纸,笔记本,屏幕等标签。均不被包含在COCO类别定义中。
model = YOLOWorld('yolov8s-worldv2.pt')
model.set_classes(['Insulated_cup','mug','paper_cup','drawing_paper','laptop', 'screen', 'woman', 'wirless_earphone', 'Shoulder_bag'])
model.save("elephant_camel_world.pt")
- 导出onnx
model = YOLO('cup_mug_world.pt')
model.export(format="onnx", opset=11)
实时推理
简单修改代码,使得opencv调用网络摄像头并介入yolo-world。代码如下:
from ultralytics import YOLOWorld
import cv2
import numpy as np
model = YOLOWorld('yolov8x-worldv2.pt')
model.set_classes(['Insulated_cup','mug','paper_cup','drawing_paper','laptop', 'screen', 'woman', 'wirless_earphone', 'Shoulder_bag'])
model.save("elephant_camel_world.pt")
capture = cv2.VideoCapture(2)
# while True:
ret, frame = capture.read()
# cv2.imshow("result", frame)
# cv2.waitKey(0)
while True:
ret, frame = capture.read()
results = model.predict(np.array(frame))
cv2.imshow("result", results[0].plot(False))
cv2.waitKey(1)
具体效果如下。从图中我们可以看到,yolo-world确实可以扩展类别到一些细分、甚至是类似的其他类别。但是存在类别识别错误,以及同物体重识别的情况。