最近需要实例分割完成一些任务,一直用的SAM(segment anything)速度慢,找一个轻量分割模型。
1. YOLO-v8-seg使用
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics
vim run.py
from ultralytics import YOLO# Load a model
model = YOLO('yolov8l-seg.pt') # load an official model# Predict with the model
results = model('test.jpg') # predict on an image
就这么几行,这就行了?代码量也太少了。
2 效果
效果也还不错
速度:
3. 对结果results的解析
可以看官方详细介绍:预测 -Ultralytics YOLOv8 文档
img = cv2.imread(img_path)
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.shape)
for result in results:boxes = result.boxes # Boxes object for bounding box outputsmasks = result.masks # Masks object for segmentation masks outputsfor mask in masks:print(mask.xy)print(type(mask.xy))points = np.array(mask.xy, dtype=np.int32)cv2.polylines(img, [points], isClosed=True, color=(0, 255, 0), thickness=2)keypoints = result.keypoints # Keypoints object for pose outputsprobs = result.probs # Probs object for classification outputs#result.show() # display to screenresult.save(filename='result.jpg') # save to disk
cv2.imwrite('test.jpg',img)
很好用,希望对你有帮助!
THE END!